Load a node for a template file- in exactly the same way a node object is presented to a theme

Key words and phrases: 
useful function module Drupal drupal render node
Description & Info: 

Motivation for this function: sure, we could use a plain node_load($nid) and wrap every $node->field['0']['value'] in a check_plain or the function for rendering with the proper input format (check_plain). The proper format is available to be grabbed from the data provided by a regular node_load. But that's a lot of work and not the way we're used to theming things in .tpl.php

A module that provides a function "agaric_node_load_view($nid)" is attached. Here is that function, which could alternately be prefaced with phptemplate_ and kept in your theme's template.php file:

<?php
function agaric_node_load_view($nid, $teaser = FALSE, $page = FALSE, $links = TRUE) {
 
$node = node_load($nid);
 
$node = (object)$node;

 
$node = node_build_content($node, $teaser, $page);

  if (
$links) {
   
$node->links = module_invoke_all('link', 'node', $node, $teaser);

    foreach (
module_implements('link_alter') AS $module) {
     
$function = $module .'_link_alter';
     
$function($node, $node->links);
    }
  }

 
// Set the proper node part, then unset unused $node part so that a bad
  // theme can not open a security hole.
 
$content = drupal_render($node->content);
  if (
$teaser) {
   
$node->teaser = $content;
    unset(
$node->body);
  }
  else {
   
$node->body = $content;
    unset(
$node->teaser);
  }

 
// Allow modules to modify the fully-built node.
 
node_invoke_nodeapi($node, 'alter', $teaser, $page);
 
 
// return the node object in the same state it is handed to the theme layer
 
return $node;
}
?>

Example of use in a node-custom.tpl.php (I know, I know, we should be putting this in template.php and passing variables in):

    <?php
   
if (function_exists('bio_for_user')) {
     
$bnid = bio_for_user($uid);     
     
$bio = agaric_node_load_view($bnid);
   
?>


<div class="view-field view-data-field-city-value">
  <?php print $bio->field_city[0]['view'] ?>
</div>

<div class="view-field view-data-field-state-value">
  <?php print $bio->field_state[0]['view'] ?>
</div>

    <?php     
   
}
   
?>

Backstory (IM chat excerpts): I can't believe there isn't a Drupal function for doing what I made that module for (to hold one function), but there really seems there isn't. It's basically the node_view function but I cut out before theme('node', $node) -- so that we get the same thing by calling this function as we do from inside a template file.

The module has no user interface. I guess it really should be a phptemplate_ function we put in all our themes, but actually it would be nicer to start abstracting that stuff to agaric.module -- easier to include for every site than copying template functions. And it'd be legit to share that with the community, even if blatantly branded ;-)

The road to getting to developing function/module:

<?php
   
if (function_exists('bio_for_user')) {
     
$bnid = bio_for_user($uid);
     
$bio = node_load($bnid);
     
$tbio = theme('node',$bio);
// does not work - strangely empty output
drupal_set_message('<pre>'.print_r($tbio,TRUE).'</pre>');
   
?>

<div class="field field-type-text field-field-city">
  <h3 class="field-label">City</h3>
  <div class="field-items">

      <div class="field-item"></div>
  </div>
</div> 

<div class="field field-type-text field-field-state">
  <h3 class="field-label">State</h3>
  <div class="field-items">
      <div class="field-item"></div>
  </div>
</div>

<h2>YOUR LIFE "ON THE OTHER SIDE OF CANCER"</h2>

<h2> MY STORY </h2>

  </div>

Aha! The answer is in http://api.drupal.org/api/function/node_view/5 ?

$node = node_build_content($node, $teaser, $page);
$content = drupal_render($node->content);

And then I just took the whole function.

AttachmentSize
agaric.zip4.64 KB
Post new comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <small> <pre> <strike> <sub> <sup>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.