Summary
Allow the variable functions - variable_del, variable_get and variable_set to receive and return name/value pairs allowing multiple variables to be set, deleted or got in a single call.
Background
It is a common design practise for contributed modules to set a number of variables in their install or enable hooks eg:
/**
* Implements hook_install
*/
function my_module_install() {
variable_set('my_module_variable', TRUE);
variable_set('my_module_other_variable', FALSE);
}
Similarly in the uninstall functions, it is common for modules to delete a lot of variables
/**
* Implements hook_uninstall
*/
function my_module_uninstall() {
variable_del('my_module_variable');
variable_del('my_module_other_variable');
}
Likewise, Drush has provided support for extracting variables as keyed arrays -eg:
drush vget fivestar --pipe
Which would return a php array of all variables starting with fivestar.
This has become a useful tool for keeping multiple deployments (development, testing, production) of a Drupal installation in sync without the need to pass around the database.
Examples uses of the variable functions with this patch
Variable set
$my_variables = array(
'my_module_variable' => 2,
'my_module_other_variable' => 3
);
variable_set($my_variables);
Variable Get
$defaults = array(
'my_module_variable' => 'default 1',
'my_module_other_variable' => 'default 2'
);
$my_variables = variable_get($defaults);
$form['my_module_variable'] = array(
'#type' => 'textarea',
'#default_value' => $my_variables['my_module_variable']
);
Variable delete
$my_variables = array('my_module_variable','my_module_other_variable');
variable_del($my_variables);
The patch also includes new tests and update system_settings_form_submit to use the array format of variable_set.
Comment | File | Size | Author |
---|---|---|---|
drupal_variable_functions.patch | 7.54 KB | larowlan | |
Comments
Comment #1
larowlanForgot to say, this is backwards compatible too
Comment #2
catchThere's an existing issue for variable_del() at #138544: Allow multiple variable deletes in variable_del() - this should probably be marked as duplicate and the patch posted over there. Leaving open for now since that issue only deals with variable_del().
Comment #3
larowlanAlso there is #317930: Allow multiple values in a single variable_set() call
Closing this one, referencing it from the other two