3 namespace Drupal\system;
5 use Drupal\Core\Config\ConfigCrudEvent;
6 use Drupal\Core\Config\ConfigEvents;
7 use Drupal\Core\Config\ConfigImporterEvent;
8 use Drupal\Core\Routing\RouteBuilderInterface;
9 use Drupal\Core\StringTranslation\StringTranslationTrait;
10 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13 * System Config subscriber.
15 class SystemConfigSubscriber implements EventSubscriberInterface {
16 use StringTranslationTrait;
21 * @var \Drupal\Core\Routing\RouteBuilderInterface
23 protected $routerBuilder;
26 * Constructs the SystemConfigSubscriber.
28 * @param \Drupal\Core\Routing\RouteBuilderInterface $router_builder
29 * The router builder service.
31 public function __construct(RouteBuilderInterface $router_builder) {
32 $this->routerBuilder = $router_builder;
36 * Rebuilds the router when the default or admin theme is changed.
38 * @param \Drupal\Core\Config\ConfigCrudEvent $event
40 public function onConfigSave(ConfigCrudEvent $event) {
41 $saved_config = $event->getConfig();
42 if ($saved_config->getName() == 'system.theme' && ($event->isChanged('admin') || $event->isChanged('default'))) {
43 $this->routerBuilder->setRebuildNeeded();
48 * Checks that the configuration synchronization is valid.
50 * This event listener prevents deleting all configuration. If there is
51 * nothing to import then event propagation is stopped because there is no
52 * config import to validate.
54 * @param \Drupal\Core\Config\ConfigImporterEvent $event
55 * The config import event.
57 public function onConfigImporterValidateNotEmpty(ConfigImporterEvent $event) {
58 $importList = $event->getConfigImporter()->getStorageComparer()->getSourceStorage()->listAll();
59 if (empty($importList)) {
60 $event->getConfigImporter()->logError($this->t('This import is empty and if applied would delete all of your configuration, so has been rejected.'));
61 $event->stopPropagation();
66 * Checks that the configuration synchronization is valid.
68 * This event listener checks that the system.site:uuid's in the source and
71 * @param \Drupal\Core\Config\ConfigImporterEvent $event
72 * The config import event.
74 public function onConfigImporterValidateSiteUUID(ConfigImporterEvent $event) {
75 if (!$event->getConfigImporter()->getStorageComparer()->validateSiteUuid()) {
76 $event->getConfigImporter()->logError($this->t('Site UUID in source storage does not match the target storage.'));
83 public static function getSubscribedEvents() {
84 $events[ConfigEvents::SAVE][] = ['onConfigSave', 0];
85 // The empty check has a high priority so that it can stop propagation if
86 // there is no configuration to import.
87 $events[ConfigEvents::IMPORT_VALIDATE][] = ['onConfigImporterValidateNotEmpty', 512];
88 $events[ConfigEvents::IMPORT_VALIDATE][] = ['onConfigImporterValidateSiteUUID', 256];