annotate core/modules/system/src/SystemConfigSubscriber.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\system;
Chris@0 4
Chris@0 5 use Drupal\Core\Config\ConfigCrudEvent;
Chris@0 6 use Drupal\Core\Config\ConfigEvents;
Chris@0 7 use Drupal\Core\Config\ConfigImporterEvent;
Chris@0 8 use Drupal\Core\Routing\RouteBuilderInterface;
Chris@0 9 use Drupal\Core\StringTranslation\StringTranslationTrait;
Chris@0 10 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * System Config subscriber.
Chris@0 14 */
Chris@0 15 class SystemConfigSubscriber implements EventSubscriberInterface {
Chris@0 16 use StringTranslationTrait;
Chris@0 17
Chris@0 18 /**
Chris@0 19 * The router builder.
Chris@0 20 *
Chris@0 21 * @var \Drupal\Core\Routing\RouteBuilderInterface
Chris@0 22 */
Chris@0 23 protected $routerBuilder;
Chris@0 24
Chris@0 25 /**
Chris@0 26 * Constructs the SystemConfigSubscriber.
Chris@0 27 *
Chris@0 28 * @param \Drupal\Core\Routing\RouteBuilderInterface $router_builder
Chris@0 29 * The router builder service.
Chris@0 30 */
Chris@0 31 public function __construct(RouteBuilderInterface $router_builder) {
Chris@0 32 $this->routerBuilder = $router_builder;
Chris@0 33 }
Chris@0 34
Chris@0 35 /**
Chris@0 36 * Rebuilds the router when the default or admin theme is changed.
Chris@0 37 *
Chris@0 38 * @param \Drupal\Core\Config\ConfigCrudEvent $event
Chris@0 39 */
Chris@0 40 public function onConfigSave(ConfigCrudEvent $event) {
Chris@0 41 $saved_config = $event->getConfig();
Chris@0 42 if ($saved_config->getName() == 'system.theme' && ($event->isChanged('admin') || $event->isChanged('default'))) {
Chris@0 43 $this->routerBuilder->setRebuildNeeded();
Chris@0 44 }
Chris@0 45 }
Chris@0 46
Chris@0 47 /**
Chris@0 48 * Checks that the configuration synchronization is valid.
Chris@0 49 *
Chris@0 50 * This event listener prevents deleting all configuration. If there is
Chris@0 51 * nothing to import then event propagation is stopped because there is no
Chris@0 52 * config import to validate.
Chris@0 53 *
Chris@0 54 * @param \Drupal\Core\Config\ConfigImporterEvent $event
Chris@0 55 * The config import event.
Chris@0 56 */
Chris@0 57 public function onConfigImporterValidateNotEmpty(ConfigImporterEvent $event) {
Chris@0 58 $importList = $event->getConfigImporter()->getStorageComparer()->getSourceStorage()->listAll();
Chris@0 59 if (empty($importList)) {
Chris@0 60 $event->getConfigImporter()->logError($this->t('This import is empty and if applied would delete all of your configuration, so has been rejected.'));
Chris@0 61 $event->stopPropagation();
Chris@0 62 }
Chris@0 63 }
Chris@0 64
Chris@0 65 /**
Chris@0 66 * Checks that the configuration synchronization is valid.
Chris@0 67 *
Chris@0 68 * This event listener checks that the system.site:uuid's in the source and
Chris@0 69 * target match.
Chris@0 70 *
Chris@12 71 * @param \Drupal\Core\Config\ConfigImporterEvent $event
Chris@0 72 * The config import event.
Chris@0 73 */
Chris@0 74 public function onConfigImporterValidateSiteUUID(ConfigImporterEvent $event) {
Chris@17 75 if (!$event->getConfigImporter()->getStorageComparer()->getSourceStorage()->exists('system.site')) {
Chris@17 76 $event->getConfigImporter()->logError($this->t('This import does not contain system.site configuration, so has been rejected.'));
Chris@17 77 }
Chris@0 78 if (!$event->getConfigImporter()->getStorageComparer()->validateSiteUuid()) {
Chris@0 79 $event->getConfigImporter()->logError($this->t('Site UUID in source storage does not match the target storage.'));
Chris@0 80 }
Chris@0 81 }
Chris@0 82
Chris@0 83 /**
Chris@0 84 * {@inheritdoc}
Chris@0 85 */
Chris@0 86 public static function getSubscribedEvents() {
Chris@0 87 $events[ConfigEvents::SAVE][] = ['onConfigSave', 0];
Chris@0 88 // The empty check has a high priority so that it can stop propagation if
Chris@0 89 // there is no configuration to import.
Chris@0 90 $events[ConfigEvents::IMPORT_VALIDATE][] = ['onConfigImporterValidateNotEmpty', 512];
Chris@0 91 $events[ConfigEvents::IMPORT_VALIDATE][] = ['onConfigImporterValidateSiteUUID', 256];
Chris@0 92 return $events;
Chris@0 93 }
Chris@0 94
Chris@0 95 }