0

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.

  • 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

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.');
}
| improve this answer | |
0

What about hook_preprocess? This hook called for all templates.

| improve this answer | |
-1

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

| improve this answer | |

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.