Same name and namespace in other branches
  1. 10 core/includes/theme.inc \template_preprocess()
  2. 7.x includes/theme.inc \template_preprocess()
  3. 8.9.x core/includes/theme.inc \template_preprocess()
  4. 9 core/includes/theme.inc \template_preprocess()

Adds a default set of helper variables for preprocess functions and templates. This comes in before any other preprocess function which makes it possible to be used in default theme implementations (non-overriden theme functions).

File

includes/theme.inc, line 1785
The theme system, which controls the output of Drupal.

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

  // Set default variables that depend on the database.
  $variables['is_admin'] = FALSE;
  $variables['is_front'] = FALSE;
  $variables['logged_in'] = FALSE;
  if ($variables['db_is_active'] = db_is_active() && !defined('MAINTENANCE_MODE')) {

    // Check for administrators.
    if (user_access('access administration pages')) {
      $variables['is_admin'] = TRUE;
    }

    // Flag front page status.
    $variables['is_front'] = drupal_is_front_page();

    // Tell all templates by which kind of user they're viewed.
    $variables['logged_in'] = $user->uid > 0;

    // Provide user object to all templates
    $variables['user'] = $user;
  }
}