function template_preprocess_node

Prepares variables for node templates.

Default template: node.html.twig.

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

Parameters

array $variables: An associative array containing:

  • elements: An array of elements to display in view mode.
  • node: The node object.
  • view_mode: View mode; e.g., 'full', 'teaser'...

File

drupal/core/modules/node/node.module, line 1075
The core module that allows content to be submitted to the site.

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);

  // @todo Change 'name' to 'author' and also convert to a render array pending
  //   http://drupal.org/node/1941286.
  $username = array(
    '#theme' => 'username',
    '#account' => $node,
    '#link_options' => array(
      'attributes' => array(
        'rel' => 'author',
      ),
    ),
  );
  $variables['name'] = drupal_render($username);
  $uri = $node
    ->uri();
  $variables['node_url'] = url($uri['path'], $uri['options']);
  $variables['label'] = check_plain($node
    ->label());
  $variables['page'] = $variables['view_mode'] == 'full' && node_is_page($node);

  // Helpful $content variable for templates.
  $variables += array(
    'content' => array(),
  );
  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, $variables['content'], $variables);

  // Display post information only on certain node types.
  if (variable_get('node_submitted_' . $node->type, TRUE)) {
    $variables['display_submitted'] = TRUE;
    $variables['submitted'] = t('Submitted by !username on !datetime', array(
      '!username' => $variables['name'],
      '!datetime' => $variables['date'],
    ));
    if (theme_get_setting('features.node_user_picture')) {

      // To change user picture settings (e.g. image style), edit the 'compact'
      // view mode on the User entity. Note that the 'compact' view mode might
      // not be configured, so remember to always check the theme setting first.
      $variables['user_picture'] = user_view($node->account, 'compact');
    }
    else {
      $variables['user_picture'] = array();
    }
  }
  else {
    $variables['display_submitted'] = FALSE;
    $variables['submitted'] = '';
    $variables['user_picture'] = '';
  }

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

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

  // Clean up name so there are no underscores.
  $variables['theme_hook_suggestions'][] = 'node__' . $node->type;
  $variables['theme_hook_suggestions'][] = 'node__' . $node->nid;
  $variables['content_attributes']['class'][] = 'content';
}