1

Both of my preprocess files use the $vars array. I assigned a new element to the $vars array in page preprocess but I can't access this variable in node preprocess. I tried doing it the other way around and had the same problem. How can I get around this?

2
  • What's a preprocess file? – Oswald Aug 6 '11 at 10:37
  • My theme has 2 files, preprocess-node.inc and preprocess-page.inc that are used to pass values to node.tpl.php and page.tpl.php – Matthew Hui Aug 6 '11 at 10:40
4

The node object itself can be used to share information across these two functions. In your template_node_preprocess() override do something like:

$node = $variables['node'];
$node->my_data = my_data_function();
$variables['my_data'] = $node->my_data;

Then in template_preprocess_page() override write:

if (!empty($variables['node'])) {
  $variables['my_data'] = $variables['node']->my_data;
}
0
0

I think that would be useful to have this piece of code here. This is how you can pass variables from page to node. in THEME_preprocess_page

if(isset($vars['page']['content']['system_main']['nodes'])){
    $keys = array_keys($vars['page']['content']['system_main']['nodes']); 
    $vars['page']['content']['system_main']['nodes'][$keys[0]]['#node']->variable_name = value; 
} 

In node.tpl.php you can print $variable_name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.