diff --git a/core/includes/config.inc b/core/includes/config.inc index 9439bf8..a45153c 100644 --- a/core/includes/config.inc +++ b/core/includes/config.inc @@ -172,12 +172,13 @@ function config_sync_invoke_sync_hooks(array $config_changes) { foreach ($remaining_changes[$op] as $key => $name) { // Extract owner from configuration object name. $module = strtok($name, '.'); - // Check whether the module implements Attempt to pass + // Check whether the module implements hook_config_sync() and ask it to + // handle the configuration change. $handled_by_module = FALSE; if (module_hook($module, 'config_sync')) { $old_config = new DrupalConfig(new DatabaseStorage($name)); $new_config = new DrupalConfig(new FileStorage($name)); - // @todo Passing $name is obsolete as soon as there is a proper ConfigObject. + // @todo $name is obsolete as soon as there is a proper ConfigObject. $handled_by_module = module_invoke($module, 'config_sync', $op, $name, $new_config, $old_config); } if (!empty($handled_by_module)) { diff --git a/core/modules/config/config.api.php b/core/modules/config/config.api.php index 273936d..7e92658 100644 --- a/core/modules/config/config.api.php +++ b/core/modules/config/config.api.php @@ -17,7 +17,7 @@ * * @param array $config_changes * An associative array whose keys denote the configuration differences - * ('new', 'changed', 'deleted') and whose values are arrays of configuration + * ('create', 'change', 'delete') and whose values are arrays of configuration * object names. * @param $target_storage * A configuration class acting on the target storage to which the new @@ -33,7 +33,7 @@ */ function hook_config_sync_validate($config_changes, $target_storage, $source_storage) { // Deny changes to our settings. - if (isset($config_changes['changed']['mymodule.locked'])) { + if (isset($config_changes['change']['mymodule.locked'])) { throw new ConfigException('MyModule settings cannot be changed.'); } } @@ -42,54 +42,45 @@ function hook_config_sync_validate($config_changes, $target_storage, $source_sto * Synchronize configuration changes. * * This hook is invoked when configuration is synchronized between storages and - * allows all modules to react to new, deleted, and changed configuration. This - * hook is invoked before the new configuration is written to the target storage. - * Implementations of this hook only react to the differences. The new - * configuration itself is written by the configuration system. + * allows a module to take over the synchronization of configuration data. * - * @param array $config_changes - * An associative array whose keys denote the configuration differences - * ('new', 'changed', 'deleted') and whose values are arrays of configuration - * object names. - * @param $target_storage + * Modules should implement this hook if they manage higher-level configuration + * data (such as image styles, node types, or fields), which needs to be + * prepared and passed through module API functions to properly handle a + * configuration change. + * + * @param string $op + * The operation to perform for the configuration data; one of 'create', + * 'delete', or 'change'. + * @param string $name + * The name of the configuration object. + * @param Drupal\Core\Config\DrupalConfig $new_config * A configuration class acting on the target storage to which the new * configuration will be written. Use $target_storage->load() to load a * configuration object. - * @param $source_storage + * @param Drupal\Core\Config\DrupalConfig $old_config * A configuration class acting on the source storage from which configuration * differences were read. Use $target_storage->load() to load a configuration * object. */ -function hook_config_sync($config_changes, $target_storage, $source_storage) { - foreach ($config_changes['new'] as $name) { - if (strpos($name, 'image.style.') === 0) { - // Load the new configuration data and inform other modules about it. - $style = $source_storage->load($name)->get(); - $style['is_new'] = TRUE; - module_invoke_all('image_style_save', $style); - } +function hook_config_sync($op, $name, $new_config, $old_config) { + // Only image styles require custom handling. Any other module settings can be + // synchronized directly. + if (strpos($name, 'image.style.') !== 0) { + return FALSE; + } + + if ($op == 'delete') { + $style = $old_config->get(); + return image_style_delete($style); } - foreach ($config_changes['changed'] as $name) { - if (strpos($name, 'image.style.') === 0) { - // Load the new configuration data, inform other modules about the change, - // and perform any further actions that may be required for the change to - // take effect. - $style = $source_storage->load($name)->get(); - $style['is_new'] = FALSE; - module_invoke_all('image_style_save', $style); - // Delete existing derivative images. - image_style_flush($style); - } + if ($op == 'new') { + $style = $new_config->get(); + return image_style_save($style); } - foreach ($config_changes['deleted'] as $name) { - if (strpos($name, 'image.style.') === 0) { - // The style has been deleted, so read the previous configuration from the - // old storage. - $style = $target_storage->load($name)->get(); - module_invoke_all('image_style_delete', $style); - // Delete existing derivative images. - image_style_flush($style); - } + if ($op == 'change') { + $style = $new_config->get(); + return image_style_save($style); } } @@ -103,7 +94,7 @@ function hook_config_sync($config_changes, $target_storage, $source_storage) { * * @param array $config_changes * An associative array whose keys denote the configuration differences - * ('new', 'changed', 'deleted') and whose values are arrays of configuration + * ('create', 'change', 'delete') and whose values are arrays of configuration * object names. * @param $target_storage * A configuration class acting on the target storage to which the new diff --git a/core/modules/config/config_test/config_test.module b/core/modules/config/config_test/config_test.module index 88abf7c..8af386e 100644 --- a/core/modules/config/config_test/config_test.module +++ b/core/modules/config/config_test/config_test.module @@ -17,7 +17,7 @@ function config_test_config_sync_validate($config_changes, $target_storage, $sou /** * Implements hook_config_sync(). */ -function config_test_config_sync($config_changes, $target_storage, $source_storage) { +function config_test_config_sync($op, $name, $new_config, $old_config) { if (!empty($GLOBALS['config_sync_throw_error'])) { throw new ConfigException('Error during synchronization.'); }