class EventSubscriber

Same name in this branch
  1. 8.9.x core/modules/config/tests/config_collection_install_test/src/EventSubscriber.php \Drupal\config_collection_install_test\EventSubscriber
  2. 8.9.x core/modules/config/tests/config_events_test/src/EventSubscriber.php \Drupal\config_events_test\EventSubscriber
  3. 8.9.x core/modules/config/tests/config_transformer_test/src/EventSubscriber.php \Drupal\config_transformer_test\EventSubscriber
  4. 8.9.x core/modules/system/tests/modules/module_install_class_loader_test2/src/EventSubscriber.php \Drupal\module_install_class_loader_test2\EventSubscriber
  5. 8.9.x core/modules/system/tests/modules/module_install_class_loader_test1/src/EventSubscriber.php \Drupal\module_install_class_loader_test1\EventSubscriber
Same name in other branches
  1. 9 core/modules/config/tests/config_collection_install_test/src/EventSubscriber.php \Drupal\config_collection_install_test\EventSubscriber
  2. 9 core/modules/config/tests/config_events_test/src/EventSubscriber.php \Drupal\config_events_test\EventSubscriber
  3. 9 core/modules/config/tests/config_transformer_test/src/EventSubscriber.php \Drupal\config_transformer_test\EventSubscriber
  4. 9 core/modules/config/tests/config_import_test/src/EventSubscriber.php \Drupal\config_import_test\EventSubscriber
  5. 9 core/modules/system/tests/modules/module_install_class_loader_test2/src/EventSubscriber.php \Drupal\module_install_class_loader_test2\EventSubscriber
  6. 9 core/modules/system/tests/modules/module_install_class_loader_test1/src/EventSubscriber.php \Drupal\module_install_class_loader_test1\EventSubscriber
  7. 10 core/modules/language/tests/language_events_test/src/EventSubscriber.php \Drupal\language_events_test\EventSubscriber
  8. 10 core/modules/config/tests/config_collection_install_test/src/EventSubscriber.php \Drupal\config_collection_install_test\EventSubscriber
  9. 10 core/modules/config/tests/config_events_test/src/EventSubscriber.php \Drupal\config_events_test\EventSubscriber
  10. 10 core/modules/config/tests/config_transformer_test/src/EventSubscriber.php \Drupal\config_transformer_test\EventSubscriber
  11. 10 core/modules/config/tests/config_import_test/src/EventSubscriber.php \Drupal\config_import_test\EventSubscriber
  12. 10 core/modules/system/tests/modules/module_install_class_loader_test2/src/EventSubscriber.php \Drupal\module_install_class_loader_test2\EventSubscriber
  13. 10 core/modules/system/tests/modules/module_install_class_loader_test1/src/EventSubscriber.php \Drupal\module_install_class_loader_test1\EventSubscriber
  14. 11.x core/modules/language/tests/language_events_test/src/EventSubscriber.php \Drupal\language_events_test\EventSubscriber
  15. 11.x core/modules/config/tests/config_collection_install_test/src/EventSubscriber.php \Drupal\config_collection_install_test\EventSubscriber
  16. 11.x core/modules/config/tests/config_events_test/src/EventSubscriber.php \Drupal\config_events_test\EventSubscriber
  17. 11.x core/modules/config/tests/config_transformer_test/src/EventSubscriber.php \Drupal\config_transformer_test\EventSubscriber
  18. 11.x core/modules/config/tests/config_import_test/src/EventSubscriber.php \Drupal\config_import_test\EventSubscriber
  19. 11.x core/modules/system/tests/modules/module_install_class_loader_test2/src/EventSubscriber.php \Drupal\module_install_class_loader_test2\EventSubscriber
  20. 11.x core/modules/system/tests/modules/module_install_class_loader_test1/src/EventSubscriber.php \Drupal\module_install_class_loader_test1\EventSubscriber

Config import subscriber for config import events.

Hierarchy

  • class \Drupal\config_import_test\EventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of EventSubscriber

1 string reference to 'EventSubscriber'
config_import_test.services.yml in core/modules/config/tests/config_import_test/config_import_test.services.yml
core/modules/config/tests/config_import_test/config_import_test.services.yml
1 service uses EventSubscriber
config_import_test.event_subscriber in core/modules/config/tests/config_import_test/config_import_test.services.yml
Drupal\config_import_test\EventSubscriber

File

core/modules/config/tests/config_import_test/src/EventSubscriber.php, line 15

Namespace

Drupal\config_import_test
View source
class EventSubscriber implements EventSubscriberInterface {
    
    /**
     * The key value store.
     *
     * @var \Drupal\Core\State\StateInterface
     */
    protected $state;
    
    /**
     * Constructs the event subscriber.
     *
     * @param \Drupal\Core\State\StateInterface $state
     *   The key value store.
     */
    public function __construct(StateInterface $state) {
        $this->state = $state;
    }
    
    /**
     * Validates the configuration to be imported.
     *
     * @param \Drupal\Core\Config\ConfigImporterEvent $event
     *   The Event to process.
     *
     * @throws \Drupal\Core\Config\ConfigNameException
     */
    public function onConfigImporterValidate(ConfigImporterEvent $event) {
        if ($this->state
            ->get('config_import_test.config_import_validate_fail', FALSE)) {
            // Log more than one error to test multiple validation errors.
            $event->getConfigImporter()
                ->logError('Config import validate error 1.');
            $event->getConfigImporter()
                ->logError('Config import validate error 2.');
        }
    }
    
    /**
     * Handles the missing content event.
     *
     * @param \Drupal\Core\Config\Importer\MissingContentEvent $event
     *   The missing content event.
     */
    public function onConfigImporterMissingContentOne(MissingContentEvent $event) {
        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) {
            $missing = $event->getMissingContent();
            $uuid = key($missing);
            $this->state
                ->set('config_import_test.config_import_missing_content_one', key($missing));
            $event->resolveMissingContent($uuid);
            // Stopping propagation ensures that onConfigImporterMissingContentTwo
            // will be fired on the next batch step.
            $event->stopPropagation();
        }
    }
    
    /**
     * Handles the missing content event.
     *
     * @param \Drupal\Core\Config\Importer\MissingContentEvent $event
     *   The missing content event.
     */
    public function onConfigImporterMissingContentTwo(MissingContentEvent $event) {
        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) {
            $missing = $event->getMissingContent();
            $uuid = key($missing);
            $this->state
                ->set('config_import_test.config_import_missing_content_two', key($missing));
            $event->resolveMissingContent($uuid);
        }
    }
    
    /**
     * Reacts to a config save and records information in state for testing.
     *
     * @param \Drupal\Core\Config\ConfigCrudEvent $event
     */
    public function onConfigSave(ConfigCrudEvent $event) {
        $config = $event->getConfig();
        if ($config->getName() == 'automated_cron.settings') {
            $values = $this->state
                ->get('ConfigImportUITest.automated_cron.settings.interval', []);
            $values[] = $config->get('interval');
            $this->state
                ->set('ConfigImportUITest.automated_cron.settings.interval', $values);
        }
        if ($config->getName() == 'core.extension') {
            $installed = $this->state
                ->get('ConfigImportUITest.core.extension.modules_installed', []);
            $uninstalled = $this->state
                ->get('ConfigImportUITest.core.extension.modules_uninstalled', []);
            $original = $config->getOriginal('module');
            $data = $config->get('module');
            $install = array_diff_key($data, $original);
            if (!empty($install)) {
                $installed[] = key($install);
            }
            $uninstall = array_diff_key($original, $data);
            if (!empty($uninstall)) {
                $uninstalled[] = key($uninstall);
            }
            $this->state
                ->set('ConfigImportUITest.core.extension.modules_installed', $installed);
            $this->state
                ->set('ConfigImportUITest.core.extension.modules_uninstalled', $uninstalled);
        }
    }
    
    /**
     * Reacts to a config delete and records information in state for testing.
     *
     * @param \Drupal\Core\Config\ConfigCrudEvent $event
     */
    public function onConfigDelete(ConfigCrudEvent $event) {
        $config = $event->getConfig();
        if ($config->getName() == 'automated_cron.settings') {
            $value = $this->state
                ->get('ConfigImportUITest.automated_cron.settings.delete', 0);
            $this->state
                ->set('ConfigImportUITest.automated_cron.settings.delete', $value + 1);
        }
    }
    
    /**
     * Registers the methods in this class that should be listeners.
     *
     * @return array
     *   An array of event listener definitions.
     */
    public static function getSubscribedEvents() {
        $events[ConfigEvents::SAVE][] = [
            'onConfigSave',
            40,
        ];
        $events[ConfigEvents::DELETE][] = [
            'onConfigDelete',
            40,
        ];
        $events[ConfigEvents::IMPORT_VALIDATE] = [
            'onConfigImporterValidate',
        ];
        $events[ConfigEvents::IMPORT_MISSING_CONTENT] = [
            [
                'onConfigImporterMissingContentOne',
            ],
            [
                'onConfigImporterMissingContentTwo',
                -100,
            ],
        ];
        return $events;
    }

}

Members

Title Sort descending Modifiers Object type Summary
EventSubscriber::$state protected property The key value store.
EventSubscriber::getSubscribedEvents public static function Registers the methods in this class that should be listeners.
EventSubscriber::onConfigDelete public function Reacts to a config delete and records information in state for testing.
EventSubscriber::onConfigImporterMissingContentOne public function Handles the missing content event.
EventSubscriber::onConfigImporterMissingContentTwo public function Handles the missing content event.
EventSubscriber::onConfigImporterValidate public function Validates the configuration to be imported.
EventSubscriber::onConfigSave public function Reacts to a config save and records information in state for testing.
EventSubscriber::__construct public function Constructs the event subscriber.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.