Routine to update and set variables in Drupal using hook_update

/ Published in: PHP
Save to your folder(s)

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.

Copy this code and paste it in your HTML
  1. /**
  2.  * Sets a variable range of variables to LUX-values.
  3.  *
  4.  * @return
  5.  * An array of name=>value pairs containing the var and its value.
  6.  */
  7. function _projectname_meta_set_variables() {
  8. $variables = array(
  9. 'site_name' => 'My New Site!',
  10. 'site_offline' => FALSE,
  11. );
  13. foreach($variables as $key => $value) {
  14. variable_set($key, $value);
  15. $statii[] = "$key => $value";
  16. }
  17. return array('success' => TRUE, 'query' => implode('<br />\n', $statii));
  18. }


Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.