0

I want to theme the Recent Content block with associated thumbnail of each node. After researching for a long time I found the function node_get_recent(). This function selects data from table 'node', but this table doesn't include any url pointing to the url of an image, which means I am not be able to use $node->(url of this thumbnail) to fetch its thumbnail. I happend to find the url of the thumbnail is located in the table 'file_managed', but I still cannot associated it with table 'node' through leftjoin.

I do not know how to do it the right way. I do not want to use Views. Thanks!


When I put the following code into my .module file the title displayed for each node is the same.

function example_contents() {
  $number_of_post = 10;
  $result = db_query("select n.title,n.nid,f.entity_id,fm.fid,fm.uri from {node} n left join {field_data_field_image} f on n.nid=f.entity_id left join {file_managed} fm on f.field_image_fid=fm.fid   limit $number_of_post");
  $items = array();
  foreach ($result as $node) {
    $items[] = l($node -> title,'node/'.$node -> nid);
  }
  return array('#markup' => theme('item_list', array('items' => $items)));
}

$node -> title turned out to be same, however $node -> nid worked. That is to say the list of nodes used the same title but different urls. What was wrong with my code? Secondly, I do not know where to put my $node -> uri into $items[] in order to display the thumbnail.

  • 4
    Why don't you want to use Views? Generally speaking, this is exactly what Views was designed to do. – Patrick Kenny Apr 14 '12 at 5:52
  • 1
    flexibility is limited as well as the effectivness of the site. – Drupal cai Apr 14 '12 at 5:54
  • What version of Drupal are you using? – sheena_d Apr 18 '12 at 8:45
  • OK,I know what was wrong,devel generated a chunk of articles began with the same few words which made me thought they were one article,everything works,thanks! – Drupal cai Apr 20 '12 at 13:40
  • Please include the drupal version you are working on. Looking at the question it is very difficult to figure out. – junedkazi Sep 25 '12 at 20:35
0

to slecet image uri you have to do

selest uri from node
join field_data_filed_image on field_data_filed_image.entity_id = node.nid
join file_managed on filed_image_fid = fid

anfd you can use this function to get thumbnail using uri

<img class="image" src="<?php print image_style_url('thumbnail', $block->uri) ?>"/> 
| improve this answer | |
  • if you use node_load only you will need image_style_url('thumbnail',$node->uri); – Ahmed Apr 14 '12 at 7:51
0

Looking at the sql query I am assuming it is Drupal 7 so you can also use entity field query

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
      ->entityCondition('bundle', 'node type')
      ->propertyCondition('status', 1);
$result = $query->execute();
$nodes = node_load_multiple(array_keys($result['node']));  

After you get a list of nodes you can do a foreach loop over all the nodes and you will have the image field available in each node object.

Also for more documentation about field entity query and options please refer to http://drupal.org/node/1343708 http://api.drupal.org/api/drupal/includes!entity.inc/class/EntityFieldQuery/7

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.