How to add extra variables to the all template files _phptemplate_variables

Posted by: 
Dominique De Cooman

In this function you can alter existing or give new variables to every theme function.
In this example the theme_page variables are altered. Here we have a product page and this page needs another template file to be loaded. We alter $vars['template_file'] and if the test is true the page-product.tpl.php file will be loaded.
Also we create new variables $vars['serviceMenu' and $vars['mainMenu'] which can be used in the page.tpl.php files as $mainMenu and serviceMenu.

<?php
function  _phptemplate_variables($hook, $vars) {
  switch($hook) {
    case 'page' :
      $vars['serviceMenu'] = build_menu(74);
      $vars['mainMenu'] = build_menu(73);
    	if(arg(0) == 'node' && is_numeric(arg(1))) {
        $node = node_load(arg(1));
 
        if(arg(0) == "node" && is_numeric(arg(1)) && $node->type == "page" && $node->taxonomy && $node->sticky == 1){ 
        foreach ($node->taxonomy as $taxo){
            if($taxo->vid == 3){
              $vars['template_file'] = 'page-product';
            }
          }
        }
      }
      if(drupal_is_front_page()) {
  				$vars['template_file'] = 'page-front';
  		}
  }
  return $vars;
}
?>

Add new comment