diff --git a/core/lib/Drupal/Core/Config/Config.php b/core/lib/Drupal/Core/Config/Config.php index 68502c6..9b92879 100644 --- a/core/lib/Drupal/Core/Config/Config.php +++ b/core/lib/Drupal/Core/Config/Config.php @@ -74,7 +74,7 @@ class Config { * * @var array */ - protected $originalData; + protected $originalData = array(); /** * The current runtime data. @@ -457,7 +457,7 @@ public function save() { $this->storage->write($this->name, $this->data); $this->isNew = FALSE; - $this->notify('save'); + $this->eventDispatcher->dispatch(ConfigEvents::SAVE, new ConfigCrudEvent($this)); $this->originalData = $this->data; return $this; } @@ -474,7 +474,7 @@ public function delete() { $this->storage->delete($this->name); $this->isNew = TRUE; $this->resetOverriddenData(); - $this->notify('delete'); + $this->eventDispatcher->dispatch(ConfigEvents::DELETE, new ConfigCrudEvent($this)); $this->originalData = $this->data; return $this; } @@ -490,16 +490,6 @@ public function getStorage() { } /** - * Dispatches a configuration event. - * - * @param string $config_event_name - * The configuration event name. - */ - protected function notify($config_event_name) { - $this->eventDispatcher->dispatch('config.' . $config_event_name, new ConfigEvent($this)); - } - - /** * Merges data into a configuration object. * * @param array $data_to_merge diff --git a/core/lib/Drupal/Core/Config/ConfigCrudEvent.php b/core/lib/Drupal/Core/Config/ConfigCrudEvent.php new file mode 100644 index 0000000..f7d9b70 --- /dev/null +++ b/core/lib/Drupal/Core/Config/ConfigCrudEvent.php @@ -0,0 +1,57 @@ +config = $config; + } + + /** + * Gets configuration object. + * + * @return \Drupal\Core\Config\Config + * The configuration object that caused the event to fire. + */ + public function getConfig() { + return $this->config; + } + + /** + * Checks to see if the provided configuration key's value has changed. + * + * @param string $key + * The configuration key to check if it has changed. + * + * @return bool + */ + public function isChanged($key) { + return $this->config->get($key) !== $this->config->getOriginal($key); + } + +} + diff --git a/core/lib/Drupal/Core/Config/ConfigEvent.php b/core/lib/Drupal/Core/Config/ConfigEvent.php deleted file mode 100644 index 5b07098..0000000 --- a/core/lib/Drupal/Core/Config/ConfigEvent.php +++ /dev/null @@ -1,33 +0,0 @@ -config = $config; - } - - /** - * Get configuration object. - */ - public function getConfig() { - return $this->config; - } -} - diff --git a/core/lib/Drupal/Core/Config/ConfigEvents.php b/core/lib/Drupal/Core/Config/ConfigEvents.php new file mode 100644 index 0000000..3049932 --- /dev/null +++ b/core/lib/Drupal/Core/Config/ConfigEvents.php @@ -0,0 +1,59 @@ +language); - $this->eventDispatcher->dispatch('config.module.overrides', $configOverridesEvent); + $this->eventDispatcher->dispatch(ConfigEvents::MODULE_OVERRIDES, $configOverridesEvent); return $configOverridesEvent->getOverrides(); } @@ -258,12 +258,10 @@ public function rename($old_name, $new_name) { unset($this->cache[$old_cache_key]); } - $new_cache_key = $this->getCacheKey($new_name); - $this->cache[$new_cache_key] = new Config($new_name, $this->storage, $this->eventDispatcher, $this->typedConfigManager, $this->language); - if ($data = $this->storage->read($new_name)) { - $this->cache[$new_cache_key]->initWithData($data); - } - return $this->cache[$new_cache_key]; + // Prime the cache and load the configuration with the correct overrides. + $config = $this->get($new_name); + $this->eventDispatcher->dispatch(ConfigEvents::RENAME, new ConfigRenameEvent($config, $old_name)); + return $config; } /** @@ -363,10 +361,10 @@ protected function canOverride($name) { /** * Removes stale static cache entries when configuration is saved. * - * @param ConfigEvent $event + * @param ConfigCrudEvent $event * The configuration event. */ - public function onConfigSave(ConfigEvent $event) { + public function onConfigSave(ConfigCrudEvent $event) { // Ensure that the static cache contains up to date configuration objects by // replacing the data on any entries for the configuration object apart // from the one that references the actual config object being saved. @@ -383,7 +381,7 @@ public function onConfigSave(ConfigEvent $event) { * {@inheritdoc} */ static function getSubscribedEvents() { - $events['config.save'][] = array('onConfigSave', 255); + $events[ConfigEvents::SAVE][] = array('onConfigSave', 255); return $events; } diff --git a/core/lib/Drupal/Core/Config/ConfigImporter.php b/core/lib/Drupal/Core/Config/ConfigImporter.php index e2354f4..d14a45e 100644 --- a/core/lib/Drupal/Core/Config/ConfigImporter.php +++ b/core/lib/Drupal/Core/Config/ConfigImporter.php @@ -23,11 +23,11 @@ * * The ConfigImporter has a identifier which is used to construct event names. * The events fired during an import are: - * - 'config.importer.validate': Events listening can throw a + * - ConfigEvents::VALIDATE: Events listening can throw a * \Drupal\Core\Config\ConfigImporterException to prevent an import from * occurring. * @see \Drupal\Core\EventSubscriber\ConfigImportSubscriber - * - 'config.importer.import': Events listening can react to a successful import. + * - ConfigEvents::IMPORT: Events listening can react to a successful import. * @see \Drupal\Core\EventSubscriber\ConfigSnapshotSubscriber * * @see \Drupal\Core\Config\ConfigImporterEvent diff --git a/core/lib/Drupal/Core/Config/ConfigRenameEvent.php b/core/lib/Drupal/Core/Config/ConfigRenameEvent.php new file mode 100644 index 0000000..83a3874 --- /dev/null +++ b/core/lib/Drupal/Core/Config/ConfigRenameEvent.php @@ -0,0 +1,45 @@ +config = $config; + $this->oldName = $old_name; + } + + /** + * Gets the old configuration object name. + * + * @return string + * The old configuration object name. + */ + public function getOldName() { + return $this->oldName; + } + +} diff --git a/core/lib/Drupal/Core/EventSubscriber/ConfigImportSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/ConfigImportSubscriber.php index 6cd3129..16267f4 100644 --- a/core/lib/Drupal/Core/EventSubscriber/ConfigImportSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/ConfigImportSubscriber.php @@ -8,6 +8,7 @@ namespace Drupal\Core\EventSubscriber; use Drupal\Core\Config\Config; +use Drupal\Core\Config\ConfigEvents; use Drupal\Core\Config\ConfigImporterEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -40,7 +41,7 @@ public function onConfigImporterValidate(ConfigImporterEvent $event) { * An array of event listener definitions. */ static function getSubscribedEvents() { - $events['config.importer.validate'][] = array('onConfigImporterValidate', 40); + $events[ConfigEvents::VALIDATE][] = array('onConfigImporterValidate', 40); return $events; } diff --git a/core/lib/Drupal/Core/EventSubscriber/ConfigSnapshotSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/ConfigSnapshotSubscriber.php index 54c60dc..e920e0f 100644 --- a/core/lib/Drupal/Core/EventSubscriber/ConfigSnapshotSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/ConfigSnapshotSubscriber.php @@ -7,7 +7,7 @@ namespace Drupal\Core\EventSubscriber; -use Drupal\Core\Config\Config; +use Drupal\Core\Config\ConfigEvents; use Drupal\Core\Config\StorageInterface; use Drupal\Core\Config\ConfigImporterEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -61,7 +61,7 @@ public function onConfigImporterImport(ConfigImporterEvent $event) { * An array of event listener definitions. */ static function getSubscribedEvents() { - $events['config.importer.import'][] = array('onConfigImporterImport', 40); + $events[ConfigEvents::IMPORT][] = array('onConfigImporterImport', 40); return $events; } diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigEventsTest.php b/core/modules/config/lib/Drupal/config/Tests/ConfigEventsTest.php new file mode 100644 index 0000000..27e8845 --- /dev/null +++ b/core/modules/config/lib/Drupal/config/Tests/ConfigEventsTest.php @@ -0,0 +1,90 @@ + 'Config events', + 'description' => 'Tests events fired on configuration objects.', + 'group' => 'Configuration', + ); + } + + /** + * Tests configuration events. + */ + function testConfigEvents() { + $name = 'config_events_test.test'; + + $config = new Config($name, \Drupal::service('config.storage'), \Drupal::service('event_dispatcher'), \Drupal::service('config.typed')); + $config->set('key', 'initial'); + \Drupal::state()->get('config_events_test.event', FALSE); + $this->assertIdentical(\Drupal::state()->get('config_events_test.event', array()), array(), 'No events fired by creating a new configuration object'); + $config->save(); + + $event = \Drupal::state()->get('config_events_test.event', array()); + $this->assertIdentical($event['event_name'], ConfigEvents::SAVE); + $this->assertIdentical($event['current_config_data'], array('key' => 'initial')); + $this->assertIdentical($event['raw_config_data'], array('key' => 'initial')); + $this->assertIdentical($event['original_config_data'], array()); + + $config->set('key', 'updated')->save(); + $event = \Drupal::state()->get('config_events_test.event', array()); + $this->assertIdentical($event['event_name'], ConfigEvents::SAVE); + $this->assertIdentical($event['current_config_data'], array('key' => 'updated')); + $this->assertIdentical($event['raw_config_data'], array('key' => 'updated')); + $this->assertIdentical($event['original_config_data'], array('key' => 'initial')); + + $config->delete(); + $event = \Drupal::state()->get('config_events_test.event', array()); + $this->assertIdentical($event['event_name'], ConfigEvents::DELETE); + $this->assertIdentical($event['current_config_data'], array()); + $this->assertIdentical($event['raw_config_data'], array()); + $this->assertIdentical($event['original_config_data'], array('key' => 'updated')); + } + + /** + * Tests configuration rename event that is fired from the ConfigFactory. + */ + function testConfigRenameEvent() { + $name = 'config_events_test.test'; + $new_name = 'config_events_test.test_rename'; + $GLOBALS['config'][$name] = array('key' => 'overridden'); + $GLOBALS['config'][$new_name] = array('key' => 'new overridden'); + + $config = \Drupal::config($name); + $config->set('key', 'initial')->save(); + $event = \Drupal::state()->get('config_events_test.event', array()); + $this->assertIdentical($event['event_name'], ConfigEvents::SAVE); + $this->assertIdentical($event['current_config_data'], array('key' => 'overridden')); + + \Drupal::configFactory()->rename($name, $new_name); + $event = \Drupal::state()->get('config_events_test.event', array()); + $this->assertIdentical($event['event_name'], ConfigEvents::RENAME); + $this->assertIdentical($event['current_config_data'], array('key' => 'new overridden')); + $this->assertIdentical($event['raw_config_data'], array('key' => 'initial')); + $this->assertIdentical($event['original_config_data'], array('key' => 'new overridden')); + } + +} diff --git a/core/modules/config/tests/config_events_test/config_events_test.info.yml b/core/modules/config/tests/config_events_test/config_events_test.info.yml new file mode 100644 index 0000000..6cb7504 --- /dev/null +++ b/core/modules/config/tests/config_events_test/config_events_test.info.yml @@ -0,0 +1,6 @@ +name: 'Configuration events test' +type: module +package: Testing +version: VERSION +core: 8.x +hidden: true diff --git a/core/modules/config/tests/config_events_test/config_events_test.module b/core/modules/config/tests/config_events_test/config_events_test.module new file mode 100644 index 0000000..aa52475 --- /dev/null +++ b/core/modules/config/tests/config_events_test/config_events_test.module @@ -0,0 +1,6 @@ +state = $state; + } + + /** + * Reacts to config event. + * + * @param \Drupal\Core\Config\ConfigCrudEvent $event + * The configuration event. + */ + public function configEventRecorder(ConfigCrudEvent $event) { + $config = $event->getConfig(); + $this->state->set('config_events_test.event', array( + 'event_name' => $event->getName(), + 'current_config_data' => $config->get(), + 'original_config_data' => $config->getOriginal(), + 'raw_config_data' => $config->getRawData() + )); + } + + /** + * {@inheritdoc} + */ + static function getSubscribedEvents() { + $events[ConfigEvents::SAVE][] = array('configEventRecorder'); + $events[ConfigEvents::DELETE][] = array('configEventRecorder'); + $events[ConfigEvents::RENAME][] = array('configEventRecorder'); + return $events; + } +} diff --git a/core/modules/config/tests/config_override/lib/Drupal/config_override/EventSubscriber/ConfigModuleLowPriorityOverrideSubscriber.php b/core/modules/config/tests/config_override/lib/Drupal/config_override/EventSubscriber/ConfigModuleLowPriorityOverrideSubscriber.php index 1e1b5ef..fd7e26c 100644 --- a/core/modules/config/tests/config_override/lib/Drupal/config_override/EventSubscriber/ConfigModuleLowPriorityOverrideSubscriber.php +++ b/core/modules/config/tests/config_override/lib/Drupal/config_override/EventSubscriber/ConfigModuleLowPriorityOverrideSubscriber.php @@ -7,6 +7,7 @@ namespace Drupal\config_override\EventSubscriber; +use Drupal\Core\Config\ConfigEvents; use Drupal\Core\Config\ConfigModuleOverridesEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -36,7 +37,7 @@ public function onConfigModuleOverride(ConfigModuleOverridesEvent $event) { * An array of event listener definitions. */ static function getSubscribedEvents() { - $events['config.module.overrides'][] = array('onConfigModuleOverride', 35); + $events[ConfigEvents::MODULE_OVERRIDES][] = array('onConfigModuleOverride', 35); return $events; } } diff --git a/core/modules/config/tests/config_override/lib/Drupal/config_override/EventSubscriber/ConfigModuleOverrideSubscriber.php b/core/modules/config/tests/config_override/lib/Drupal/config_override/EventSubscriber/ConfigModuleOverrideSubscriber.php index 54f0f64..e168111 100644 --- a/core/modules/config/tests/config_override/lib/Drupal/config_override/EventSubscriber/ConfigModuleOverrideSubscriber.php +++ b/core/modules/config/tests/config_override/lib/Drupal/config_override/EventSubscriber/ConfigModuleOverrideSubscriber.php @@ -7,6 +7,7 @@ namespace Drupal\config_override\EventSubscriber; +use Drupal\Core\Config\ConfigEvents; use Drupal\Core\Config\ConfigModuleOverridesEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -34,7 +35,7 @@ public function onConfigModuleOverride(ConfigModuleOverridesEvent $event) { * An array of event listener definitions. */ static function getSubscribedEvents() { - $events['config.module.overrides'][] = array('onConfigModuleOverride', 40); + $events[ConfigEvents::MODULE_OVERRIDES][] = array('onConfigModuleOverride', 40); return $events; } } diff --git a/core/modules/language/lib/Drupal/language/EventSubscriber/ConfigSubscriber.php b/core/modules/language/lib/Drupal/language/EventSubscriber/ConfigSubscriber.php index 819c2d3..82b6f9d 100644 --- a/core/modules/language/lib/Drupal/language/EventSubscriber/ConfigSubscriber.php +++ b/core/modules/language/lib/Drupal/language/EventSubscriber/ConfigSubscriber.php @@ -8,7 +8,8 @@ namespace Drupal\language\EventSubscriber; use Drupal\Component\PhpStorage\PhpStorageFactory; -use Drupal\Core\Config\ConfigEvent; +use Drupal\Core\Config\ConfigCrudEvent; +use Drupal\Core\Config\ConfigEvents; use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** @@ -19,12 +20,12 @@ class ConfigSubscriber implements EventSubscriberInterface { /** * Causes the container to be rebuilt on the next request. * - * @param ConfigEvent $event + * @param ConfigCrudEvent $event * The configuration event. */ - public function onConfigSave(ConfigEvent $event) { + public function onConfigSave(ConfigCrudEvent $event) { $saved_config = $event->getConfig(); - if ($saved_config->getName() == 'system.site' && $saved_config->get('langcode') != $saved_config->getOriginal('langcode')) { + if ($saved_config->getName() == 'system.site' && $event->isChanged('langcode')) { // Trigger a container rebuild on the next request by deleting compiled // from PHP storage. PhpStorageFactory::get('service_container')->deleteAll(); @@ -35,7 +36,7 @@ public function onConfigSave(ConfigEvent $event) { * {@inheritdoc} */ static function getSubscribedEvents() { - $events['config.save'][] = array('onConfigSave', 0); + $events[ConfigEvents::SAVE][] = array('onConfigSave', 0); return $events; } diff --git a/core/modules/system/lib/Drupal/system/SystemConfigSubscriber.php b/core/modules/system/lib/Drupal/system/SystemConfigSubscriber.php index d6e1fc6..4abc714 100644 --- a/core/modules/system/lib/Drupal/system/SystemConfigSubscriber.php +++ b/core/modules/system/lib/Drupal/system/SystemConfigSubscriber.php @@ -7,6 +7,7 @@ namespace Drupal\system; +use Drupal\Core\Config\ConfigEvents; use Drupal\Core\Config\ConfigImporterEvent; use Drupal\Core\Config\ConfigImporterException; use Drupal\Core\Config\StorageDispatcher; @@ -21,7 +22,7 @@ class SystemConfigSubscriber implements EventSubscriberInterface { * {@inheritdoc} */ static function getSubscribedEvents() { - $events['config.importer.validate'][] = array('onConfigImporterValidate', 20); + $events[ConfigEvents::VALIDATE][] = array('onConfigImporterValidate', 20); return $events; }