template_preprocess_node

Versions
6 – 7
template_preprocess_node(&$variables)

Process variables for node.tpl.php

Most themes utilize their own copy of node.tpl.php. The default is located inside "modules/node/node.tpl.php". Look in there for the full list of variables.

The $variables array contains the following arguments:

  • $node
  • $build_mode
  • $page

See also

node.tpl.php

Code

modules/node/node.module, line 1317

<?php
function template_preprocess_node(&$variables) {
  $variables['build_mode'] = $variables['elements']['#build_mode'];
  // Provide a distinct $teaser boolean.
  $variables['teaser'] = $variables['build_mode'] == 'teaser';
  $variables['node'] = $variables['elements']['#node'];
  $node = $variables['node'];

  $variables['date']      = format_date($node->created);
  $variables['name']      = theme('username', array('account' => $node));
  $variables['node_url']  = url('node/' . $node->nid);
  $variables['node_title'] = check_plain($node->title[FIELD_LANGUAGE_NONE][0]['value']);
  $variables['page']      = (bool)menu_get_object();

  if (!empty($node->in_preview)) {
    unset($node->content['links']);
  }

  // Flatten the node object's member fields.
  $variables = array_merge((array)$node, $variables);

  // Helpful $content variable for templates.
  foreach (element_children($variables['elements']) as $key) {
    $variables['content'][$key] = $variables['elements'][$key];
  }

  // Make the field variables available with the appropriate language.
  field_attach_preprocess('node', $node, $variables['content'], $variables);

  if (isset($variables['content']['title'])) {
    unset($variables['content']['title']);
  }

  // Display post information only on certain node types.
  if (variable_get('node_submitted_' . $node->type, TRUE)) {
    $variables['display_submitted'] = TRUE;
    $variables['user_picture'] = theme_get_setting('toggle_node_user_picture') ? theme('user_picture', array('account' => $node)) : '';
  }
  else {
    $variables['display_submitted'] = FALSE;
    $variables['user_picture'] = '';
  }

  // Gather node classes.
  $variables['classes_array'][] = drupal_html_class('node-' . $node->type);
  if ($variables['promote']) {
    $variables['classes_array'][] = 'node-promoted';
  }
  if ($variables['sticky']) {
    $variables['classes_array'][] = 'node-sticky';
  }
  if (!$variables['status']) {
    $variables['classes_array'][] = 'node-unpublished';
  }
  if ($variables['teaser']) {
    $variables['classes_array'][] = 'node-teaser';
  }
  if (isset($variables['preview'])) {
    $variables['classes_array'][] = 'node-preview';
  }

  // Clean up name so there are no underscores.
  $variables['template_files'][] = 'node-' . str_replace('_', '-', $node->type);
  $variables['template_files'][] = 'node-' . $node->nid;
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.