deckerdev.com

Snippets relating to Drupal preprocess functions.

Preprocess nodes

Drupal 7:

<?php
function THEME_preprocess_node(&$vars) {
  $type = $vars['type'];
  $mode = $vars['view_mode'];
  $node = $vars['node'];

  // Bundle-specific preprocess functions, like
  // THEME_preprocess_node__page() or THEME_preprocess_node__story().
  $function = __FUNCTION__ . '__' . $node->type;
  if (function_exists($function)) {
    $function($vars);
  }
}

Drupal 8:

<?php
function military_preprocess_node(&$variables) {
  /** @var \Drupal\node\NodeInterface $node */
  $node = $variables['node'];

  // Bundle-specific preprocess functions.
  $function = __FUNCTION__ . '__' . $node->bundle();
  if (function_exists($function)) {
    $function($variables);
  }
}