Skip to main content

The Cheeky Monkey Media Blog

A few words from the apes, monkeys, and various primates that make up the Cheeky Monkey Super Squad.

Drupal 7 - Passing Page Level Variables to the Node Level

Today I had the need to pass variables typically only available at the page.tpl level to be accessible at the node.tpl level. My solution resulted in adding a preprocess_page(&$vars) to the template.php file. In this specific situation I needed to know in the node.tpl if the sidebar_first region was populated with anything or not and if it was then it would determine which image style to use on a header image. template.php

Template.php Preprocess_page hook
  1.  /** * Page preprocessor we use to check to see if there is the sidebar and then pass it on to the node level 
  2. * @param $vars 
  3. * @return unknown_type */ 
  4. function comfortTech_preprocess_page(&$vars){ 
  5. //GB: We want the Node, if it's exists to know about the $sidebar_first and if it's loaded as it will determine what image style to use 
  6.    if(isset($vars["page"]["sidebar_first"])){ 
  7.     // add the flag to the node to say it's been set and there is only a single node to display 
  8.        if(isset($vars["page"]["content"]["system_main"]["nodes"]) && count($vars["page"]["content"]["system_main"]["nodes"]) == 2){ 
  9.             $keys = array_keys($vars["page"]["content"]["system_main"]["nodes"]); 
  10.            // Set a new variable called sidebar_first equal to true on the node.tpl level 
  11.            $vars["page"]["content"]["system_main"]["nodes"][$keys[0]]['#node']->sidebar_first = true; 
  12.       }
  13.     } 
  14. } 

node.tpl

Node.tpl implementation
  1. // set the image style variables needed for the theme call, checks to see if the $node->sidebar_first was set if it is then the sidebar is being displayed 
  2. if(isset($node->sidebar_first)){ 
  3.     $image_variable["style_name"] = 'header_image_half'; 
  4. } else { 
  5.   $image_variable["style_name"] = 'header_image_full'; 
  6. } 

Web Development

Would you like to know more about what we do?

View Our Services

Graphic Design Cheeky Monkey

Have a look at some our client work

View Our Work

Cheeky Monkey Discovery

Learn more about Cheeky Monkey Media

About Us

Comments