1.20.x node.theme.inc template_preprocess_node(&$variables)

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

Parameters

$variables: An array containing the following arguments:

  • $node
  • $view_mode
  • $page

See also

node.tpl.php

File

modules/node/node.theme.inc, line 196
Theme functions for the Node module.

Code

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

  $variables['date'] = format_date($node->created);
  $variables['name'] = theme('username', array(
    'account' => $node,
    'link_attributes' => array('rel' => 'author'),
  ));

  $uri = entity_uri('node', $node);
  $variables['node_url'] = url($uri['path'], $uri['options']);
  $variables['title'] = check_plain($node->title);
  $variables['page'] = ($variables['view_mode'] == 'full' && node_is_page($node)) || (!empty($node->in_preview));

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

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

  // Comments are coded directly into the node template, but comment module
  // may not be enabled. Set a variable to ensure it exists. Comment module will
  // add it's own replacements over the top of this.
  $variables['comments'] = FALSE;

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

  // Display post information only on certain node types.
  $node_type = node_type_get_type($node->type);

  $variables['display_submitted'] = FALSE;
  $variables['submitted'] = '';
  $variables['user_picture'] = '';

  if ($node_type->settings['node_submitted']) {
    if (!isset($variables['elements']['#display_submitted']) || $variables['elements']['#display_submitted'] == TRUE) {
      $variables['display_submitted'] = TRUE;
      $variables['submitted'] = token_replace($node_type->settings['node_submitted_format'], array('node' => $node));
      $variables['user_picture'] = '';
      if ($node_type->settings['node_user_picture']) {
        $variables['user_picture'] = theme('user_picture', array('account' => $node));
      }
    }
  }

  // Add article ARIA role.
  $variables['attributes']['role'] = 'article';

  // Gather node classes.
  $variables['classes'][] = backdrop_html_class('node-' . $node->type);
  if ($variables['promote']) {
    $variables['classes'][] = 'promoted';
  }
  if ($variables['sticky']) {
    $variables['classes'][] = 'sticky';
  }
  if (!$variables['status']) {
    $variables['classes'][] = 'unpublished';
  }
  if ($variables['view_mode']) {
    $variables['classes'][] = backdrop_html_class('view-mode-' . $variables['view_mode']);
  }
  if (!$node->uid) {
    $variables['classes'][] = 'node-by-anonymous';
  }
  elseif ($node->uid == $variables['user']->uid) {
    $variables['classes'][] = 'node-by-viewer';
  }

  // Add an extra theme hook suggestion for node id.
  $variables['theme_hook_suggestions'][] = 'node__' . $node->nid;
}