Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I often use functions that contain variables in the following files:

  • Template.php
  • Custom Panels Style Plugins
  • node.tpl.php files
  • field.tpl.php files
  • views.tpl.php files

I often needed to pass variables between these functions.

At the moment, I do this by declaring my variable as a global Using PHP's global command. However, I have read that is not best practice. Is there a better way?

I know that Drupal has variable_set() and variable_get(). However, apparently these have similar problems to using Globals for lots of little variables.

share|improve this question
1  
What is the source of your global variables. Did you retrieve these data from database on every page request? –  ya.teck Dec 25 '12 at 5:07
    
@Xio I create them in the theme layer –  big_smile Dec 25 '12 at 11:02

3 Answers 3

up vote 3 down vote accepted

I am newish to Drupal, but will give this a shot (I'm sure someone will tell me if I am wrong).

You can set variables that exist in different contexts by using the template.php file of your theme by adding stuff to the $variables array with preprocessor functions. So you could maybe add your variables to the YOURTHEME_preprocess_html() function, which should make them available everywhere.

function YOURTHEME_preprocess_html(&$variables, $hook) {
  $variables['sample_variable'] = t('Lorem ipsum.');
}
share|improve this answer

What about hook_preprocess? This hook called for all templates.

share|improve this answer

You probably need some settings, so using variable_set is a normal practice I think.

share|improve this answer

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.