Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer

Hm...

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

preprocess_block executes easlier 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
  }
}
share|improve this answer
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 Apr 9 at 13:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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