User login

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

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:

<div class="codeblock"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp; </span><span style="color: #007700">if (</span><span style="color: #0000BB">function_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'bio_for_user'</span><span style="color: #007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$bnid </span><span style="color: #007700">= </span><span style="color: #0000BB">bio_for_user</span><span style="color: #007700">(</span><span style="color: #0000BB">$uid</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$bio </span><span style="color: #007700">= </span><span style="color: #0000BB">node_load</span><span style="color: #007700">(</span><span style="color: #0000BB">$bnid</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$tbio </span><span style="color: #007700">= </span><span style="color: #0000BB">theme</span><span style="color: #007700">(</span><span style="color: #DD0000">'node'</span><span style="color: #007700">,</span><span style="color: #0000BB">$bio</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// does not work - strangely empty output<br /></span><span style="color: #0000BB">drupal_set_message</span><span style="color: #007700">(</span><span style="color: #DD0000">'&lt;pre&gt;'</span><span style="color: #007700">.</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$tbio</span><span style="color: #007700">,</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">).</span><span style="color: #DD0000">'&lt;/pre&gt;'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp; <br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>

<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.

Resolution

Searched words: 
useful function module Drupal drupal render node create page template tpl pass in node variables node variables

Comments

Drupal Version?

Thanks for this function. Will this work for Drupal 6? I can't tell what version this refers to, and there's no date information anywhere on the page other than "14 Mar". 2009? 2008? 2007? 2006? It's always nice to know how relevant information is on the web, since things change so frequently around here. A full date on this page (and other pages) would be helpful.

You aren't kidding

It is Drupal 5, this post is 2008. When we relaunch Agaric notes on the new data.agaric these comments will look silly, because all that information will be there...

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.