2

I've some problem with themes and preprocess functions.

I see that, if I declare a variable in, for example, MYTHEME_preprocess_region(&$vars), this variable is available ONLY in region.tpl.php (I'm using Omega). But in preprocess_region and region.tpl there is the $vars['node'] variable (if I'm displaying a node), so in these functions I can't create variable based on $node.

In my example, the $vars['node'] variable is available in preprocess_page and preprocess_node.

The question is: if I want to add on region.tpl.php (or preprocess_region) a variable based on $node how should I do?

The "general" question is: how can I declare a variable in a theme function, in template.php, and let this function available in other functions?

1 Answer 1

3

Hm...

First of all you should control order of preprocess functions execution.

preprocess_block executes earlier than preprocess_region, preprocess_region earlier than preprocess_node, and so on...

In first preprocess, you should declare static variable, like here.

$var = &drupal_static('variable_name_that_will_be_placed_in_static_stack');
if (!isset($var)) {
  $var = $value_to_store;
}

So, you can access this var from later executed functions.

$var = &drupal_static('variable_name_that_will_be_placed_in_static_stack');

As for your problem, it's more easy to get node from menu_router:

theme_preprocess_region(&$vars)
  $node = menu_get_object();
  if ($node) {
    // get required value
  }
}
1
  • This could be a solution, but it's a "general" solution, I would like to do this in a "theme" way. However I'll try it thanks!
    – arrubiu
    Commented Apr 9, 2013 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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