Calling node_load from within template

Events happening in the community are now at Drupal community events on www.drupal.org.
el_reverend's picture

Does anybody know of the implications of using node_load() in a template and have a better suggestion?

Some details:

I have a cck content type that uses the cck reference field to link to a node manually entered (intentionally) by an editor. I am using that nid via node_load() to get a image from the referenced node and display it together with the title. This 'related post' block is a curated list of other posts related to the current one.

I have read that node_load isn't a good choice for this since it loads the entire node object (so in my case several times), and while this works and seemingly very fast on my local setup, are there ways to improve this? What other alternatives do I have?

Thanks a lot for your input.

<?php
if ($node->field_node_reference[0]['nid'] != ""){ //ensuring that at least the first entry isn't empty
   
$num_of_references = count($node->field_node_reference);
    print (
"<div class=\"field-type-nodereference\"><h3>You might be interested in these ".$num_of_references." other article(s):</h3>");

for (
$counter = 0; $counter < $num_of_references; ++$counter){
     
// get referenced node id
     
$the_node_id $node->field_node_reference[$counter]['nid'];
    
$the_other_node = node_load(array("nid" => $the_node_id));

      
$thepreset = "Lead-Image-Thumbnail";
    
$theimage = $the_other_node->field_blog_lead_image[0]['filepath'];
       
$thealt = $the_other_node->field_blog_lead_image[0][data]['alt'];
    
$thetitle = $the_other_node->field_blog_lead_image[0][data]['title'];

       
$theposttitle = "<a href=\"". base_path() . $the_other_node->path ."\">" . $the_other_node->title . "</a>";

     print (
"<div class=\"image\"><div class=\"image\">");
        print
theme('imagecache', $thepreset, $theimage, $thealt, $thetitle);
        print (
"</div><div>");
       print
$theposttitle;
       print (
"</div></div>");
  }
  print (
"</div>");
}
?>

Comments

Views maybe?

TimG1's picture

Hey there,

It sounds like you're trying to do something similar to what I have here. http://bit.ly/tHBhTl If you scroll to the bottom and look in the left column you'll see a list of "Editor's Picks". For my story content type the user can enter other nodes as reference fields. Then the editor's pick is a view block that pulls in the fields from each referenced node (based off of the current page's story nid) and links to it.

-Tim

+1 for Views block

ahimsauzi's picture

For a related posts functionality like you describe I used NID and Has taxonomy term ID as filters see sample here: http://ow.ly/7b8sA.

Thank Uzi and Tim,I will try

el_reverend's picture

Thank Uzi and Tim,
I will try that out. My first try somehow didn't pull in the results listed by the node reference fields and I needed it to display after the content but before the $links in the node.

I will try this again (do you have any suggestions on how I can get the CCK image field to be displayed from the NID reference?) and do you have any suggestions on how I could get this views block to display before the $links? Normally blocks (depending on the regions) are published after the links and tags. I am using fusion as base theme and that tends to have pretty good region settings.

View Reference + Argument / Display Suite

TimG1's picture

Hey El_reverend,

The site I showed you is in Drupal 6/views2 but steps should be similar & the idea the same for D7/views 3. You'll need to add Reference in your view which should be the CCK node reference field in your content type. Then you'll also need an argument for the current node id. In D6/Views2 I chose Provide Default Argument > NodeID from URL.

When you add your fields you should see the relationship name in a dropdown menu so you can choose fields from the node being referenced instead of the current node being viewed.

For switching the layout around check out Display Suite. There's a great screencast showing how Display Suite Works: http://bit.ly/ds-d7

-Tim