Version 1
[yaffs-website] / web / core / modules / config / tests / config_import_test / src / EventSubscriber.php
1 <?php
2
3 namespace Drupal\config_import_test;
4
5 use Drupal\Core\Config\ConfigCrudEvent;
6 use Drupal\Core\Config\ConfigEvents;
7 use Drupal\Core\Config\ConfigImporterEvent;
8 use Drupal\Core\Config\Importer\MissingContentEvent;
9 use Drupal\Core\State\StateInterface;
10 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11
12 /**
13  * Config import subscriber for config import events.
14  */
15 class EventSubscriber implements EventSubscriberInterface {
16
17   /**
18    * The key value store.
19    *
20    * @var \Drupal\Core\State\StateInterface
21    */
22   protected $state;
23
24   /**
25    * Constructs the event subscriber.
26    *
27    * @param \Drupal\Core\State\StateInterface $state
28    *   The key value store.
29    */
30   public function __construct(StateInterface $state) {
31     $this->state = $state;
32   }
33
34   /**
35    * Validates the configuration to be imported.
36    *
37    * @param \Drupal\Core\Config\ConfigImporterEvent $event
38    *   The Event to process.
39    *
40    * @throws \Drupal\Core\Config\ConfigNameException
41    */
42   public function onConfigImporterValidate(ConfigImporterEvent $event) {
43     if ($this->state->get('config_import_test.config_import_validate_fail', FALSE)) {
44       // Log more than one error to test multiple validation errors.
45       $event->getConfigImporter()->logError('Config import validate error 1.');
46       $event->getConfigImporter()->logError('Config import validate error 2.');
47     }
48   }
49
50   /**
51    * Handles the missing content event.
52    *
53    * @param \Drupal\Core\Config\Importer\MissingContentEvent $event
54    *   The missing content event.
55    */
56   public function onConfigImporterMissingContentOne(MissingContentEvent $event) {
57     if ($this->state->get('config_import_test.config_import_missing_content', FALSE) && $this->state->get('config_import_test.config_import_missing_content_one', FALSE) === FALSE) {
58       $missing = $event->getMissingContent();
59       $uuid = key($missing);
60       $this->state->set('config_import_test.config_import_missing_content_one', key($missing));
61       $event->resolveMissingContent($uuid);
62       // Stopping propagation ensures that onConfigImporterMissingContentTwo
63       // will be fired on the next batch step.
64       $event->stopPropagation();
65     }
66   }
67
68   /**
69    * Handles the missing content event.
70    *
71    * @param \Drupal\Core\Config\Importer\MissingContentEvent $event
72    *   The missing content event.
73    */
74   public function onConfigImporterMissingContentTwo(MissingContentEvent $event) {
75     if ($this->state->get('config_import_test.config_import_missing_content', FALSE) && $this->state->get('config_import_test.config_import_missing_content_two', FALSE) === FALSE) {
76       $missing = $event->getMissingContent();
77       $uuid = key($missing);
78       $this->state->set('config_import_test.config_import_missing_content_two', key($missing));
79       $event->resolveMissingContent($uuid);
80     }
81   }
82
83   /**
84    * Reacts to a config save and records information in state for testing.
85    *
86    * @param \Drupal\Core\Config\ConfigCrudEvent $event
87    */
88   public function onConfigSave(ConfigCrudEvent $event) {
89     $config = $event->getConfig();
90     if ($config->getName() == 'action.settings') {
91       $values = $this->state->get('ConfigImportUITest.action.settings.recursion_limit', []);
92       $values[] = $config->get('recursion_limit');
93       $this->state->set('ConfigImportUITest.action.settings.recursion_limit', $values);
94     }
95
96     if ($config->getName() == 'core.extension') {
97       $installed = $this->state->get('ConfigImportUITest.core.extension.modules_installed', []);
98       $uninstalled = $this->state->get('ConfigImportUITest.core.extension.modules_uninstalled', []);
99       $original = $config->getOriginal('module');
100       $data = $config->get('module');
101       $install = array_diff_key($data, $original);
102       if (!empty($install)) {
103         $installed[] = key($install);
104       }
105       $uninstall = array_diff_key($original, $data);
106       if (!empty($uninstall)) {
107         $uninstalled[] = key($uninstall);
108       }
109
110       $this->state->set('ConfigImportUITest.core.extension.modules_installed', $installed);
111       $this->state->set('ConfigImportUITest.core.extension.modules_uninstalled', $uninstalled);
112     }
113   }
114
115   /**
116    * Reacts to a config delete and records information in state for testing.
117    *
118    * @param \Drupal\Core\Config\ConfigCrudEvent $event
119    */
120   public function onConfigDelete(ConfigCrudEvent $event) {
121     $config = $event->getConfig();
122     if ($config->getName() == 'action.settings') {
123       $value = $this->state->get('ConfigImportUITest.action.settings.delete', 0);
124       $this->state->set('ConfigImportUITest.action.settings.delete', $value + 1);
125     }
126   }
127
128   /**
129    * Registers the methods in this class that should be listeners.
130    *
131    * @return array
132    *   An array of event listener definitions.
133    */
134   public static function getSubscribedEvents() {
135     $events[ConfigEvents::SAVE][] = ['onConfigSave', 40];
136     $events[ConfigEvents::DELETE][] = ['onConfigDelete', 40];
137     $events[ConfigEvents::IMPORT_VALIDATE] = ['onConfigImporterValidate'];
138     $events[ConfigEvents::IMPORT_MISSING_CONTENT] = [['onConfigImporterMissingContentOne'], ['onConfigImporterMissingContentTwo', -100]];
139     return $events;
140   }
141
142 }