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 Answers
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;
}
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