Hi guys,

I've created a few tiny modules that uses the theme() function to style their output, this works great.
My modules share some common logic, and i'd like to share this between the modules.

I've tried to define variables in the phptemplate_preproccess function in my themes template.php fil, but they don't seem to be available in my module templates.

Is it not possible for my module tempate file, to access variables defined in the preproccess functions? and if it's not, is there another way to share variables/functionality between multiple modules?

Regards
Martin Nielsen

Comments

pbarnett’s picture

You can use variable_get and variable_set to share variables between modules - see http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/variab...

Boedlen’s picture

Hi Pete,

Thanks for the tip. It's exactly what i need :-)

/Martin

RoloDMonkey’s picture

I have seen this before and it is probably not the right solution for what he is asking.

This should only be used if the variable is not changing between requests.

The variable_get() and variable_set() functions work at the application level. They are stored in the database and meant to hold configuration options that do not change very often. They should not be used to store values that are dynamically generated based on the current request.

Imagine this scenario:

  1. I request a page.
  2. Your module saves a variable, using variable_set(), based on my request.
  3. Before my page is rendered, someone else requests a different page.
  4. Your module overrides the variable based on that request
  5. When my page is rendered, your module gets the other user's variable!

If you need to have a variable that is persistent for one request, use a static variable inside a method as described in a post below.

--

Read more at iRolo.net

Jaypan’s picture

Add a call to a function with a static variable. As long as the function is in the .module file, it will be available to all modules on the site.

Ex:

function get_my_variable()
{
  static $my_variable;
  if(!$my_variable)
  {
    $my_variable = TRUE;
  }
  return $my_variable;
}

This essentially caches the variable in the function, and it can be called by any module at any time. It will code that loads the data into the variable will only ever be called the first time the function is executed, as the value is held in the static $my_variable.

pbarnett’s picture

Shouldn't that be

function my_variable($value = NULL)
{
  static $my_variable;
  if($value)
  {
    $my_variable = $value;
  }
  return $my_variable;
}

That way, you can get, and optionally set, the variable.

Jaypan’s picture

Sure. It wasn't meant as a real example - my code makes absolutely no sense if it were supposed to be real, as it would always mean that $my_variable was TRUE. It was just meant to show the way the code can work.

Maybe the data is coming from a database, in such a case you would never need to set it. Or maybe it's based on the current time, or the current user or whatnot. There are any number of variations. Yours is fine, mine is fine, the main point is the usage of the static variable.

RoloDMonkey’s picture

There are several more examples here:

http://drupal.org/node/904150#comment-3422734

--

Read more at iRolo.net