Variable module provides a registry for meta-data about Drupal variables and some extended Variable API and administration interface.

This is an API module so it must be installed only when other modules require it.

Module Developers: Please declare your variables.

Add-ons for module developers here: Variable Extra

This is a nice introduction to the module by Lullabot, Module Monday: Variable.

¿Drupal 8? #2146779: Port Variable to Drupal 8

Why?

  • So other modules can know about your module's variables and they can be translated, exported, used in views, etc.
  • You'll get automatic variable edit forms, tokens, access control and uninstall for free
  • Your module's variables will be allowed for Variable Realms being able to get values for each language (Internationalization) or Domain (Variable Domain)

How?

Easy: Implement hook_variable_info();

/**
 * Implements hook_variable_info().
 */
function mymodule_variable_info($options) {

  // This is the very minimum we need, some descriptive name.

  $variable['mymodule_variable'] = array(
    'title' => t('My variable'),
  );

  // But if you want to provide more information, we'll make good use of it.

  $variable['mymodule_number'] = array(
    'title' => t('Magic number', array(), $options),
    'description' => t('Magic number', array(), $options),
    'type' => 'number',
    'access' => 'administer menus',
    'token' => TRUE, // We'll produce tokens automatically for this one 
  );
 
  $variable['mymodule_name'] = array(
    'title' => t('Name', array(), $options),
    'description' => t('Enter your name, please.', array(), $options),
    'type' => 'string',
    'default' => t('Drupal user', array(), $options),
  );
  
  $variable['mymodule_mail_[mail_part]'] = array(
    'title' => t('Mail'),
    'type' => 'mail_text',
    // This type will spawn into two real variables: mymodule_mail_subject, mymodule_mail_body
    // Everything, included the form elements, will be handled automatically
  );

  return $variable;
}  

Note: You can have your variables declared in a separate file that just will be loaded when needed.

yourmodule.variable.inc

FAQ

Will I need to add a dependency on the variable.module?

Not really. Just if you want to enjoy some of the module's advanced features like:

  • Getting variable values or defaults in different languages. Use variable_get_value().
  • Let other modules alter my variable defaults. Implement hook_variable_info_alter().
  • Let other modules know when variables are changed. Use variable_set_value(). Implement hook_variable_update().
  • Getting automatic forms for all the module's variables, a group of variables, etc..
  • Having variables with multiple values handled automatically like mail body and subject or variables for node types.

Otherwise you can just provide the meta-data for other modules to use. You still get:

  • Tokens for your variables like [variable:myvariable_name].
  • Variables deleted automatically when the module is uninstalled
  • Localizable texts for your variables when using the Internationalization module (WIP).

How do I get a form with all of my module's variables?

drupal_get_form('variable_module_form', 'mymodule');

Once I have declared a default for my variable, how can I effectively use it?

variable_get_value('variable_name');

What if I don't want to provide any administration form for my variables?

That's ok, people will still be able to see and edit them by enabling the 'Variable Admin' module included.

How can I learn about more (advanced) options for declaring variables?

Other features (sub-modules)

  • Variable Realms, which is an API for multiple modules overriding global variables.
  • Variable Store, which provides database storage for modules using variable realms.

Variable 6.x

This branch is a minimum backport and is maintained by mkalkbrenner.
Variable Realms and Variable Store are currently not supported by the 6.x-1.x branch.

Project information

Releases