4

I have a Drupal Module 'example'. I have split the admin and client side functionality of the module into two include files example.admin.inc and example.pages.inc. I declared constants (define()) in the example.module file, which are accessible in both the pages.inc file and admin.inc file.

But how can I declare normal $variables in a centralized location so that they can be accessible in both the admin.inc file and pages.inc file. I tried to declare $variable in example.module, but couldn't access it in the example.admin.inc and example.pages.inc.

Sparky
  • 4,769
  • 8
  • 37
  • 52
  • What kind variables? Those that only live for a single page request, or persisted values, variables that can be overriden...? – Berdir May 18 '11 at 08:46
  • That can be over ridden. May be one that is evaluated as the result of an expression. – Sparky May 18 '11 at 09:13

3 Answers3

7

There are multiple possibilities, depending on want you want.

Overridable settings

You can use variable_get('your_module_your_key', $default_value);. Then, it is easy to override that either in settings.php with $conf['your_module_your_key'] = 'other value';. See http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/variable_get.

It is also easy to build a settings form for that, see for example http://api.drupal.org/api/drupal/modules--menu--menu.admin.inc/function/menu_configure/7.

Note that you should always prefix your variables with your module name and that you don't use this for variables that change frequently because every change results in a cache clear for all variables. Also note that all variables are loaded on every page request, so don't create too many of them or store big data structures in it. They are mainly meant for configuration.

Static get/set functions

You can write simple get/set functions which use a static internally to exchange variables during a single page request. See for example http://api.drupal.org/api/drupal/includes--path.inc/function/drupal_set_title/6.

Keep in mind that these things are only kept for a single page request. Here is an example implementation which allows to save multiple variables identified by a string and therefor works similar like variable_get()/set().

<?php
function yourmodule_static($key, $value = NULL) {
  static $storage;

  if (isset($value)) {
    $storage[$key] = $value;
  }
  return $storage[$key];
}

// Then, you can use it like this:
your_module_static('key', $value);

// And then in a different function/file:
$value = your_module_static('key');

You could also extend it to return per reference and so on.

Cache

To store for example data from slow database queries or complex calculations, you can use the cache system. http://www.lullabot.com/articles/a-beginners-guide-to-caching-data looks like a detailed and great explanation of that.

Note that the cache is stored in the database by default but is pluggable and can for example also use APC or Memcached.

Berdir
  • 6,881
  • 2
  • 26
  • 38
  • To be honest, not quite the way I expected, but thanks for the elaborate answer. – Sparky May 18 '11 at 09:53
  • Then better explain what you did expect. These are the options that you have in Drupal, there is no other possibility other than globals ( and the static get/set helper functions are a direct replacement for that). – Berdir May 18 '11 at 11:08
  • I was hoping for a global 'like' variable, which is present in the particular module's scope rather than in the global scope. variable_get() seems good, but it uses database, right? Hope u get my point. – Sparky May 18 '11 at 11:45
  • There is no "module scope" in Drupal, all modules run in the same global namespace. But as written above, you can use set/get functions which store that data in internal static functions. If you have multiple different things that you want to store, it is easy to extend the pattern above to something like `yourmodule_static($key, $value)`. Drupal 7 has a drupal_static() function to be able to reset static caches that works very similar. I'll update my example. – Berdir May 18 '11 at 14:24
1

For Drupal 7 see the new drupal_static() function: http://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/drupal_static/7

tmsimont
  • 2,651
  • 2
  • 25
  • 36
1

at the .module file but you need define it as global

hish
  • 145
  • 4