1.20.x theme.inc template_preprocess(&$variables, $hook)

Adds a default set of variables for variable preprocessors and templates.

This function is called for theme hooks implemented as templates only, not for theme hooks implemented as functions. This preprocess function is the first in the sequence of preprocessing functions that is called when preparing variables for a template. See theme() for more details about the full sequence.

See also

theme()

File

includes/theme.inc, line 2633
The theme system, which controls the output of Backdrop.

Code

function template_preprocess(&$variables, $hook) {
  global $user;
  static $count = array();

  // Track run count for each hook to provide zebra striping. See
  // "template_preprocess_block()" which provides the same feature specific to
  // blocks.
  $count[$hook] = isset($count[$hook]) && is_int($count[$hook]) ? $count[$hook] : 1;
  $variables['zebra'] = ($count[$hook] % 2) ? 'odd' : 'even';
  $variables['id'] = $count[$hook]++;

  // Tell all templates where they are located.
  $variables['directory'] = path_to_theme();

  // Initialize html class attribute for the current hook.
  $variables['classes'] = array(backdrop_html_class($hook));

  // Initialize additional attribute placeholder for the current hook.
  $variables['attributes'] = array();

  // Merge in variables that don't depend on hook and don't change during a
  // single page request.
  // Use the advanced backdrop_static() pattern, since this is called very often.
  static $backdrop_static_fast;
  if (!isset($backdrop_static_fast)) {
    $backdrop_static_fast['default_variables'] = &backdrop_static(__FUNCTION__);
  }
  $default_variables = &$backdrop_static_fast['default_variables'];
  // Global $user object shouldn't change during a page request once rendering
  // has started, but if there's an edge case where it does, re-fetch the
  // variables appropriate for the new user.
  if (!isset($default_variables) || ($user !== $default_variables['user'])) {
    $default_variables = _template_preprocess_default_variables();
  }
  $variables += $default_variables;
}