3 namespace Drupal\config_import_test;
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;
13 * Config import subscriber for config import events.
15 class EventSubscriber implements EventSubscriberInterface {
18 * The key value store.
20 * @var \Drupal\Core\State\StateInterface
25 * Constructs the event subscriber.
27 * @param \Drupal\Core\State\StateInterface $state
28 * The key value store.
30 public function __construct(StateInterface $state) {
31 $this->state = $state;
35 * Validates the configuration to be imported.
37 * @param \Drupal\Core\Config\ConfigImporterEvent $event
38 * The Event to process.
40 * @throws \Drupal\Core\Config\ConfigNameException
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.');
51 * Handles the missing content event.
53 * @param \Drupal\Core\Config\Importer\MissingContentEvent $event
54 * The missing content event.
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();
69 * Handles the missing content event.
71 * @param \Drupal\Core\Config\Importer\MissingContentEvent $event
72 * The missing content event.
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);
84 * Reacts to a config save and records information in state for testing.
86 * @param \Drupal\Core\Config\ConfigCrudEvent $event
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);
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);
105 $uninstall = array_diff_key($original, $data);
106 if (!empty($uninstall)) {
107 $uninstalled[] = key($uninstall);
110 $this->state->set('ConfigImportUITest.core.extension.modules_installed', $installed);
111 $this->state->set('ConfigImportUITest.core.extension.modules_uninstalled', $uninstalled);
116 * Reacts to a config delete and records information in state for testing.
118 * @param \Drupal\Core\Config\ConfigCrudEvent $event
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);
129 * Registers the methods in this class that should be listeners.
132 * An array of event listener definitions.
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]];