| 6 theme.inc | template_preprocess(&$variables, $hook) |
| 7 theme.inc | template_preprocess(&$variables, $hook) |
| 8 theme.inc | template_preprocess(&$variables, $hook) |
Adds a default set of helper variables for variable processors and templates. This comes in before any other preprocess function which makes it possible to be used in default theme implementations (non-overridden theme functions).
For more detailed information, see theme().
File
- includes/
theme.inc, line 2208 - The theme system, which controls the output of Drupal.
Code
<?php
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'] = array(drupal_html_class($hook));
// Merge in variables that don't depend on hook and don't change during a
// single page request.
// Use the advanced drupal_static() pattern, since this is called very often.
static $drupal_static_fast;
if (!isset($drupal_static_fast)) {
$drupal_static_fast['default_variables'] = &drupal_static(__FUNCTION__);
}
$default_variables = &$drupal_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;
}
?> Login or register to post comments
Comments
$zebra is not reset per
$zebra is not reset per instance, e.g. two block instances of the same view will accumulate counts. To add more granularity, you can do this in your template.php (and flush cache after adding):
<?php
/**
* Implements hook_preprocess().
*/
function mytheme_preprocess(&$variables, $hook) {
// 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_hook'][$hook] = ($count[$hook] % 2) ? 'odd' : 'even';
$variables['id_hook'][$hook] = $count[$hook]++;
}
?>
Now $zebra_hook is an array with the counts per hook. For example, you can use $zebra_hook['node'] or $zebra_hook['my_entity']. This allows for a bit more granularity in $zebra striping and $id counting.