diff --git a/core/includes/update.inc b/core/includes/update.inc index 1a2a242..7bce4f4 100644 --- a/core/includes/update.inc +++ b/core/includes/update.inc @@ -850,6 +850,37 @@ function update_retrieve_dependencies() { } /** + * Provide a generalised method to migrate variables from Drupal 7 to Drupal 8's + * configuration management system. + * + * @param $config_name + * The name of the configuration object to retrieve. + */ +function update_variables_to_config($config_name) { + $var_names = array_keys(config($config_name)->get()); + if (!empty($var_names)) { + // Get any variables currently defined that match the new setting names in + // the config file. + $query = db_select('variable', 'v') + ->fields('v') + ->condition('name', $var_names, 'IN'); + $var_values = $query->execute()->fetchAllKeyed(0); + $config = config($config_name); + // Update the config system settings to use the values previously stored in + // the variable table. + foreach($var_values as $name => $val) { + $config->set($name, $val); + } + $config->save(); + // Delete the old variables. The config system will throw an exception if a + // value cannot be saved, so this code will not run if there is a problem + // running the update. + $del = db_delete('variable')->condition('name', $var_names, 'IN'); + $del->execute(); + } +} + +/** * @defgroup update-api-7.x-to-8.x Update versions of API functions * @{ * Functions similar to normal API function but not firing hooks.