Drupal 7 - Variables

  • user notice: The custom_breadcrumbs_nodeapi() function called token replacement with an array rather than a string for $text in /home/redleafmedia/redleafmedia.com/sites/all/modules/token/token.module on line 263.
  • user notice: The custom_breadcrumbs_nodeapi() function called token replacement with an array rather than a string for $text in /home/redleafmedia/redleafmedia.com/sites/all/modules/token/token.module on line 263.

Drupal allows modules and themes to be quite customizable, with many configuration options available to site builders. For every setting that can be tweaked by site builders, there needs to be a method for Drupal to store and recall the settings. As with previous versions, Drupal 7 has such capabilities built into its APIs. Essentially, Drupal stores each setting in a "variable" table in its MySQL database. Each setting has a "name" and "value" that are stored in the database. Drupal reads, writes and deletes these entries as needed over the course of a site's lifetime. Many of the variables remain virtually unchanged over time. As such, Drupal saves processing time by caching variable values as much as possible. With this post, I want to quickly review the code that Drupal uses in its variable system.

Much of the code that comprises the variable system is found 'site_root/includes/bootstrap.inc'. The first function definition related to variables is variable_initialize(). This function is used to build up a $variables array that contains all of the various site settings:

<?php
function variable_initialize($conf = array()) {
 
// NOTE: caching the variables improves performance by 20% when serving
  // cached pages.
 
if ($cached = cache_get('variables', 'cache_bootstrap')) {
   
$variables = $cached->data;
  }
  else {
   
// Cache miss. Avoid a stampede.
   
$name = 'variable_init';
    if (!
lock_acquire($name, 1)) {
     
// Another request is building the variable cache.
      // Wait, then re-run this function.
     
lock_wait($name);
      return
variable_initialize($conf);
    }
    else {
     
// Proceed with variable rebuild.
     
$variables = array_map('unserialize', db_query('SELECT name, value FROM {variable}')->fetchAllKeyed());
     
cache_set('variables', $variables, 'cache_bootstrap');
     
lock_release($name);
    }
  }

  foreach (
$conf as $name => $value) {
   
$variables[$name] = $value;
  }

  return
$variables;
}
?>

It first checks to see if the variables have been cached already. If so, it returns the cached values. If not, it continues to build up a $variables array by querying the variable table in the database. It then caches the obtained values and returns the $variables array.

Once Drupal has built up its variables values from the database, it is convenient for developers to have a method of quickly accessing and/or setting values of variables. This is where the functions variable_get() and variable_set() come in. First variable_get() is defined:

<?php
function variable_get($name, $default = NULL) {
  global
$conf;

  return isset(
$conf[$name]) ? $conf[$name] : $default;
}
?>

A variable name and an optional default value are passed to the function. Drupal checks the global $conf array for a key with the requested variable name. If it finds that key, the associated value is returned. Otherwise, the default value is returned. This provides developers with a fallback variable setting in case no setting has been defined at any given point in time. Next up is variable_set():

<?php
function variable_set($name, $value) {
  global
$conf;

 
db_merge('variable')->key(array('name' => $name))->fields(array('value' => serialize($value)))->execute();

 
cache_clear_all('variables', 'cache_bootstrap');

 
$conf[$name] = $value;
}
?>

Here Drupal 7's new db_merge() function is used to properly insert the variable value into the database. Then the variable cache is cleared, as would be expected to account for the new value. Finally, the new value is incorporated into the global $conf array. The next function is variable_del(). It essentially deletes the desired variable value from the database and unsets the appropriate key/value pair in the global $conf array.

The preceding code makes up the bulk of Drupal's variable system. These functions are used quite often by developers and are the backbone that allows site builders' customizations to be remembered by Drupal.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.