Apply image styling to images rendered from separate nodes on Drupal site? - php

I have a page on my website that filters content from a separate page using the entityFieldQuery method. The problem is that the images I am filtering on to the new page need a different image style applied to them so that they load into smaller images. I have picture mapping set up on the site but I have only applied it via the Drupal workbench in the image fields where you can choose an image style from a dropdown.
The code below is my entityfieldquery and the conditional statement that creates the content that is being filtered from another page on to the current page. Is there any way I can revise this code to apply a different image style to the images that are being generated?
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->propertyCondition('status', NODE_PUBLISHED)
->propertyCondition('type', 'Blog')
->propertyCondition('created', array($old_date, $current_date),'BETWEEN')
->propertyOrderBy('created', 'DESC')
->fieldCondition('field_blog_categories', 'tid', $term_id)
->fieldCondition('field_blog_categories', 'tid', array('55', '26'))
->range(0, 9);
$result = $query->execute();
}
if(isset($result['node'])) {
$blog_items_nids = array_keys($result['node']);
$blog_items = entity_load('node', $blog_items_nids);
if (count($blog_items)>2){
$data = array();
foreach ($blog_items as $blg) {
$field_blog_header_image = field_view_field('node', $blg, 'field_blog_header_image', 'picture');
$temp_blog_cat = field_view_field('node', $blg, 'field_blog_categories');
$data[$temp_blog_cat['#items'][0]['tid']][] = array(
'blog' => $temp_blog_cat["#object"]->title,
'img' => isset($field_blog_header_image) ? render($field_blog_header_image) : '',
'link' => $temp_blog_cat['#object']->path['alias'],
);
}
?>

Yes, you can use function image_style_url():
https://api.drupal.org/api/drupal/modules!image!image.module/function/image_style_url/7
That function will give you path to image in any style you pass to it as long as you can acquire and pass image URI to it as parameter.
So, check the values you get from your view, find how to get image URI and call it like:
$image_path = image_style_url('style_name', $uri);

Related

How can I automatically HREF the articles of my MongoDB database to each article specific page?

I'm working on a site which is more of a DB project that anything else. Currently I mainly using my mongoDB database to show the previews of those articles after querying them with these functions (which are in 2 different files)
//this one is from the functions file which i load in all the various pages of the site
function article_recordset() {
require 'vendor/autoload.php';
$client = new MongoDB\Client("mongodb://localhost:27017");
$i = 0;
$SiteArticles = $client->SiteArticles;
$articoli=$SiteArticles->Articles;
$cursor = $articoli->find();
return $cursor;
foreach ($cursor as $articolo) {
$recordset = array(
$i=$i+1 => array(
"title" => $articolo["title"],
"description" => $articolo["content"],
"article" => "",
)
);
}
}
//this is the index.php file so the homepage where i display the articles which i stored into an array and then i display with an echo later
include("funzioni.inc.php");//name of the functions file
$ARR_articles = article_recordset();
$STR_article = NULL;
foreach($ARR_articles as $index => $article){
$STR_article .= get_article_item_html(
$img_path = "img/article/".$index.".jpg",
$title = $article["title"],
$description = $article["content"]
);
}
The display works fine and all but what I'm trying to do is to HREF each displayed article preview to their own article page
So article 1 is displayed and is linked to the page of localhost/folder/article1.php for example
Now the href I want to do is somewhat automatic so it links to each article page which I manually created already just like it does the foreach for the query of all the article contents
So like it's going to store each article in the array which I display later while also like setting an href to each one
For example:
Query article 1, store into array, display that array content for article 1 and href it to it's own page, and so on for each article
Note: I want to ""automate"" just the HREF of each queried article not automate the article page creation since I have those pages already created and it's for a small project and nothing too serious

Drupal 8 change image style in first row on views list

I have created view that displays on page 10 newest article. I have in row two fields: image and content. In settings of image field I chose image style (for example: medium). How can I change image style to another (example: large) only in first row?
I have tried it in preprocess but i don't know where is stored information about image style:
function theme_preprocess_views_view_unformatted__lista_depesz_default(&$variables) {
$view = $variables['view'];
$rows = $variables['rows'];
$style = $view->style_plugin;
$options = $style->options;
$variables['default_row_class'] = !empty($options['default_row_class']);
foreach ($rows as $id => $row) { $variables['rows'][$id] = array();
$variables['rows'][$id]['content'] = $row;
$variables['rows'][$id]['attributes'] = new Attribute();
if ($row_class = $view->style_plugin->getRowClass($id)) {
$variables['rows'][$id]['attributes']->addClass($row_class);
}
if ($id == 0 && $row['content']['#row']->_entity->field_image[0] != NULL) {
//some php code to change image style
}
}
}
Regards
You can create view with original images, and set style inside your twig files, using for example twig tweak:
https://www.drupal.org/project/twig_tweak
Inside twig file you can set any style with conditioning
{{ 'public://images/ocean.jpg' | image_style('thumbnail') }}
Regarding your code and your explanations, I'm not sure to understand what you are trying to achieve.
1/ you try to add a CSS class to the 1st image of your view. Why not using the following CSS path .my_view .first img {}
2/ if you try to call another image style, can you create a view with only the 1st item, or a promoted? Then a second view with the rest of the items ?
3/ if you try to call another image style, you can do it without any code.
You install the module http://drupal.org/project/views_conditional then you add 2 images with 2 different image style, and you apply your condition inside the condition fieldset.
I really prefer the solution 3 because it's pure Drupal build.
I hope it helps.
Cheers

Site map generator in laravel 5 with RoumenDianoff sitemap

Im implementing the laravel RoumenDianoff Sitemap in my application
https://github.com/RoumenDamianoff/laravel-sitemap/wiki/Dynamic-sitemap
And im really confused on this part of the code
Route::get('sitemap', function(){
// create new sitemap object
$sitemap = App::make("sitemap");
// set cache key (string), duration in minutes (Carbon|Datetime|int), turn on/off (boolean)
$sitemap->setCache('laravel.sitemap', 60);
$posts = DB::table('posts')->orderBy('created_at', 'desc')->get();
foreach ($posts as $post)
{
$sitemap->add($post->slug, $post->modified, $post->priority, $post->freq);
}
As I understand i'm creating a route to use sitemap as that function so the part I don't get is when do I iterate trough every link of my website, or how do i get this links from my website to add then to the for each on that function, i mean it looks like that $posts variable but I don't have record of my links on any database so how can i get this links.
What I would do is generate the links dynamically (probably using values from the database). Then create a sitemap object that will be populated with all the links (use foreach loop) and finally store this. Check out the code fragment below:
Route::get('sitemap',function(){
// Generate your links dynamically here
$link = [];
$locations = DB::locations(); /** some database values **/
foreach($locations as $location){
$link = route('home') . '/shoes-for-sale-in-' . strtolower($location);
// create new sitemap object
$sitemap = \App::make("sitemap");
// Add link, priority,last modified and change frequency
$sitemap->add($link, null, 0.5, 'monthly');
}
// generate sitemap (format, filename)
$sitemap->store('xml', 'commercial');
});

How to Download Images from new RETS CRMLS

I am trying to download images from new RETS CRMLS
$photos = $rets->SearchQuery("Media","Media",$lid, array('Limit' => 'none', 'Select' => "MediaOrder,MediaURL"));
foreach ($photos as $photo)
{
if ($photo['Success'] == true)
{
file_put_contents("../images/{$photo['Content-ID']}-{$photo['Object-ID']}.jpg", $photo['Data']);
$count++;
}
}
this is not working
If you want to download the images from the Property class, you can use this rets function
$rets->GetObject("Property", "Photo", $listingId, "*", 1);
This will return the array of images associated with the particular listingId as image url.
If you want to download as binary image files then you can use the last parameter as '0', instead of '1'. [ '1' is for getting public image Urls]
If the image is to be downloaded from the Media class then you can use the same function with Media as the class. And you should first get the listing Id from the Property class.
Your "file_put_contents" function appears to be attempting to parse the info headers within a multipart/mime response from a GetObject query. Rather, you'd want to do a straight http download of the MediaUrls. I'm not a php wizard, but you'd do something more like:
$photoUrls = $rets->SearchQuery("Media","Media",$lid, array('Limit' => 'none', 'Select' => "MediaOrder,MediaURL"));
while ($photo = $rets->FetchRow($photoUrls)) {
file_put_contents("../images/{$lid}-{$photo['MediaOrder']}.jpg", file_get_contents($photo['MediaURL']));
}
$rets->FreeResult($photoUrls);
Also, you probably want to limit your search to MediaTypes of "Image" so you don't get other binary data or documents, like virtual tour URLs or PDFs (see google group for this MLS that you also posted within).

cakePHP : combining 2 actions and loading all players and also one player that changes

In my index (/players) view I have a carousel with images of a bunch of players. When I click on an Image I go to the view screen of the clicked player (/players/view/1).
Now I want to only have an index screen with that carousel of player images and after I click on an Image I want the information of that player on that same index screen.
How would I best combine the index and view so that I can click on a player image and retrieve his information on the same page? The goal is to have one view file and one action in the controller. So how can I put find('all') and find('first') in one action?
Now I have /players and /players/view/1.
I want to have for instance /players/1 so it loads on the same page. However this will still give a page load I think.
Eventually I don't want a page load, but only a content change.
index action of PlayersController (gives me all the players) :
public function index() {
$this->layout = 'default_front_players';
$this->Player->recursive = 0;
//$this->Player->find('all');
$this->set('players', $this->Paginator->paginate());
}
view action of PlayersController (gives me the player that has been clicked) :
public function view($id = null) {
$this->layout = 'default_front_players';
if (!$this->Player->exists($id)) {
throw new NotFoundException(__('Invalid player'));
}
$options = array('conditions' => array('Player.' . $this->Player->primaryKey => $id));
$this->set('player', $this->Player->find('first', $options));
}
UPDATE
First case
This gives me all the players and gives me also one player (atm player 143, the last one in the database), problem is that when I click on a players image the player stays on player 143. The url changes to players/index/{number of clicked player}
public function index($id = null) {
$this->layout = 'default_front_players';
$this->Player->recursive = 0;
$players = $this->Player->find('all');
$this->set(compact('players'));
$options = array('conditions' => array('Player.' . $this->Player->primaryKey => $id));
$player = $this->set('player', $this->Player->find('first', $options));
}
Second case
This doesn't shows me the player images, but when I change the url, it gives me the content of the player which id I give in the URL.
public function index($id = null) {
$this->layout = 'default_front_players';
$this->Player->recursive = 0;
//$players = $this->Player->find('all');
//$this->Paginator->settings = $this->paginator_players;
$this->set('players');
$options = array('conditions' => array('Player.' . $this->Player->primaryKey => $id));
$player = $this->set('player', $this->Player->find('first', $options));
}
If I go to /players or players/index I don't get any values.
How can I combine these 2?
Have you tried ajax and routes to achieve a content refresh ( essentially creating your own api call to internal resources )? I use cakephp for my own side project, and needed a way to circumvent the whole controller, view load mechanism for a faster UI experience.
The gist is including jquery in your template or layout file. When a certain route or url is called ( this requires a route to be setup in your routes.php file in app/config relatively from your apps root folder ) , fire the ajax call to fetch the content and dynamically update the dom without having to reload the page.
This suggestion might open up a whole new can of worms for you, but its achievable I think.

Categories