Posted By

berkes on 03/09/10


deployment drupal update install drupal5 hookupdaten

Versions (?)

Who likes this?

2 people have marked this snippet as a favorite


Routine to update and set variables in Drupal using hook_update

 / Published in: PHP


Drupal allows hookupdateN 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 'projectnamemeta'. So this function would go in projectnamemeta.install, and will be called from projectnamemetainstall and projectnamemetaupdate_5002, for example.

  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  

You need to login to post a comment.