Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Cleaning up system variables

Currently, many of our modules have no .install file, yet they set Druapl system variables that would get left behind in the database if the module were disabled/removed. Adding this function to an existing .install file, or creating a blank .install file with just this function will remedy this. If the .install file already has a hook_uninstall() function, check the code to make sure it does something similar to this funciton to clean up its variables.

...

Code Block
titlemodulename.install
borderStylesolid
<?php

function modulename_uninstall() {
  // remove module system variables
  $module_name = 'modulename';

  $vars = db_query("select * from {variable} where name like '" . $module_name . "%'");
  while ($var = db_fetch_object($vars)) {
    variable_del($var->name);
  }
}

Renaming system variables when changing module name

Code Block
titlemodulename.install
borderStylesolid

<?php

function newmodulename_install() {
  // rename module system variables
  $oldname = 'modulename';
  $newname = 'newmodulename';
  
  $vars = db_query("select * from {variable} where name like 'modulename%" . $oldname . "%'");
  while ($var = db_fetch_object($vars)) {
    $newvar = str_replace($oldname,$newname,$var->name);

    variable_set($newvar, variable_get($var->name, NULL));
    variable_del($var->name);
  }
}