3 namespace Drupal\Core\Config;
5 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8 * Defines a base event listener implementation configuration overrides.
10 abstract class ConfigFactoryOverrideBase implements EventSubscriberInterface {
13 * Reacts to the ConfigEvents::COLLECTION_INFO event.
15 * @param \Drupal\Core\Config\ConfigCollectionInfo $collection_info
16 * The configuration collection info event.
18 abstract public function addCollections(ConfigCollectionInfo $collection_info);
21 * Actions to be performed to configuration override on configuration save.
23 * @param \Drupal\Core\Config\ConfigCrudEvent $event
24 * The config CRUD event.
26 abstract public function onConfigSave(ConfigCrudEvent $event);
29 * Actions to be performed to configuration override on configuration delete.
31 * @param \Drupal\Core\Config\ConfigCrudEvent $event
32 * The config CRUD event.
34 abstract public function onConfigDelete(ConfigCrudEvent $event);
37 * Actions to be performed to configuration override on configuration rename.
39 * @param \Drupal\Core\Config\ConfigRenameEvent $event
40 * The config rename event.
42 abstract public function onConfigRename(ConfigRenameEvent $event);
47 public static function getSubscribedEvents() {
48 $events[ConfigEvents::COLLECTION_INFO][] = ['addCollections'];
49 $events[ConfigEvents::SAVE][] = ['onConfigSave', 20];
50 $events[ConfigEvents::DELETE][] = ['onConfigDelete', 20];
51 $events[ConfigEvents::RENAME][] = ['onConfigRename', 20];
56 * Filters data in the override based on what is currently in configuration.
58 * @param \Drupal\Core\Config\Config $config
59 * Current configuration object.
60 * @param \Drupal\Core\Config\StorableConfigBase $override
61 * Override object corresponding to the configuration to filter data in.
63 protected function filterOverride(Config $config, StorableConfigBase $override) {
64 $override_data = $override->get();
65 $changed = $this->filterNestedArray($config->get(), $override_data);
66 if (empty($override_data)) {
67 // If no override values are left that would apply, remove the override.
71 // Otherwise set the filtered override values back.
72 $override->setData($override_data)->save(TRUE);
77 * Filters data in nested arrays.
79 * @param array $original_data
80 * Original data array to filter against.
81 * @param array $override_data
82 * Override data to filter.
85 * TRUE if $override_data was changed, FALSE otherwise.
87 protected function filterNestedArray(array $original_data, array &$override_data) {
89 foreach ($override_data as $key => $value) {
90 if (!isset($original_data[$key])) {
91 // The original data is not there anymore, remove the override.
92 unset($override_data[$key]);
95 elseif (is_array($override_data[$key])) {
96 if (is_array($original_data[$key])) {
97 // Do the filtering one level deeper.
98 // Ensure that we track $changed along the way.
99 if ($this->filterNestedArray($original_data[$key], $override_data[$key])) {
102 // If no overrides are left under this level, remove the level.
103 if (empty($override_data[$key])) {
104 unset($override_data[$key]);
109 // The override is an array but the value is not, this will not go
110 // well, remove the override.
111 unset($override_data[$key]);