class SystemConfigSubscriber

Same name in other branches
  1. 9 core/modules/system/src/SystemConfigSubscriber.php \Drupal\system\SystemConfigSubscriber
  2. 8.9.x core/modules/system/src/SystemConfigSubscriber.php \Drupal\system\SystemConfigSubscriber
  3. 11.x core/modules/system/src/SystemConfigSubscriber.php \Drupal\system\SystemConfigSubscriber

System Config subscriber.

Hierarchy

Expanded class hierarchy of SystemConfigSubscriber

1 string reference to 'SystemConfigSubscriber'
system.services.yml in core/modules/system/system.services.yml
core/modules/system/system.services.yml
1 service uses SystemConfigSubscriber
system.config_subscriber in core/modules/system/system.services.yml
Drupal\system\SystemConfigSubscriber

File

core/modules/system/src/SystemConfigSubscriber.php, line 15

Namespace

Drupal\system
View source
class SystemConfigSubscriber implements EventSubscriberInterface {
    use StringTranslationTrait;
    
    /**
     * The router builder.
     *
     * @var \Drupal\Core\Routing\RouteBuilderInterface
     */
    protected $routerBuilder;
    
    /**
     * Constructs the SystemConfigSubscriber.
     *
     * @param \Drupal\Core\Routing\RouteBuilderInterface $router_builder
     *   The router builder service.
     */
    public function __construct(RouteBuilderInterface $router_builder) {
        $this->routerBuilder = $router_builder;
    }
    
    /**
     * Rebuilds the router when the default or admin theme is changed.
     *
     * @param \Drupal\Core\Config\ConfigCrudEvent $event
     *   The configuration event.
     */
    public function onConfigSave(ConfigCrudEvent $event) {
        $saved_config = $event->getConfig();
        if ($saved_config->getName() == 'system.theme' && ($event->isChanged('admin') || $event->isChanged('default'))) {
            $this->routerBuilder
                ->setRebuildNeeded();
        }
    }
    
    /**
     * Checks that the configuration synchronization is valid.
     *
     * This event listener prevents deleting all configuration. If there is
     * nothing to import then event propagation is stopped because there is no
     * config import to validate.
     *
     * @param \Drupal\Core\Config\ConfigImporterEvent $event
     *   The config import event.
     */
    public function onConfigImporterValidateNotEmpty(ConfigImporterEvent $event) {
        $importList = $event->getConfigImporter()
            ->getStorageComparer()
            ->getSourceStorage()
            ->listAll();
        if (empty($importList)) {
            $event->getConfigImporter()
                ->logError($this->t('This import is empty and if applied would delete all of your configuration, so has been rejected.'));
            $event->stopPropagation();
        }
    }
    
    /**
     * Checks that the configuration synchronization is valid.
     *
     * This event listener checks that the system.site:uuid's in the source and
     * target match.
     *
     * @param \Drupal\Core\Config\ConfigImporterEvent $event
     *   The config import event.
     */
    public function onConfigImporterValidateSiteUUID(ConfigImporterEvent $event) {
        if (!$event->getConfigImporter()
            ->getStorageComparer()
            ->getSourceStorage()
            ->exists('system.site')) {
            $event->getConfigImporter()
                ->logError($this->t('This import does not contain system.site configuration, so has been rejected.'));
        }
        if (!$event->getConfigImporter()
            ->getStorageComparer()
            ->validateSiteUuid()) {
            $event->getConfigImporter()
                ->logError($this->t('Site UUID in source storage does not match the target storage.'));
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() : array {
        $events[ConfigEvents::SAVE][] = [
            'onConfigSave',
            0,
        ];
        // The empty check has a high priority so that it can stop propagation if
        // there is no configuration to import.
        $events[ConfigEvents::IMPORT_VALIDATE][] = [
            'onConfigImporterValidateNotEmpty',
            512,
        ];
        $events[ConfigEvents::IMPORT_VALIDATE][] = [
            'onConfigImporterValidateSiteUUID',
            256,
        ];
        return $events;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
SystemConfigSubscriber::$routerBuilder protected property The router builder.
SystemConfigSubscriber::getSubscribedEvents public static function
SystemConfigSubscriber::onConfigImporterValidateNotEmpty public function Checks that the configuration synchronization is valid.
SystemConfigSubscriber::onConfigImporterValidateSiteUUID public function Checks that the configuration synchronization is valid.
SystemConfigSubscriber::onConfigSave public function Rebuilds the router when the default or admin theme is changed.
SystemConfigSubscriber::__construct public function Constructs the SystemConfigSubscriber.

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