Return to Snippet

Revision: 24723
at March 9, 2010 09:00 by berkes


Initial Code
/**
 * Sets a variable range of variables to LUX-values.
 *
 * @return
 *   An array of name=>value pairs containing the var and its value.
 */
function _projectname_meta_set_variables() {
  $variables = array(
    'site_name' => 'My New Site!',
    'site_offline' => FALSE,
  );
  
  foreach($variables as $key => $value) {
    variable_set($key, $value);
    $statii[] = "$key => $value";
  } 
  return array('success' => TRUE, 'query' => implode('<br />\n', $statii));
}

Initial URL
http://wizzlern.nl

Initial Description
Drupal allows hook_update_N to run any sort of code. Most of the time, however, people only use this to alter the database scheme. 
But you can use it for much more. Like setting variables, creating nodes, importing views and so on. 
This code shows how to set a series of variables in Drupal.

When you deploy this code, running update.php will set all the variables as in the listing.

I typically catch this in a module called 'projectname_meta'. So this function would go in projectname_meta.install, and will be called from projectname_meta_install and projectname_meta_update_5002, for example.

Initial Title
Routine to update and set variables in Drupal using hook_update

Initial Tags
drupal, update

Initial Language
PHP