SystemConfigSubscriber.php 3.25 KB
Newer Older
1
<?php
2

3 4
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 10
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11 12 13 14

/**
 * System Config subscriber.
 */
15 16
class SystemConfigSubscriber implements EventSubscriberInterface {
  use StringTranslationTrait;
17

18 19 20 21 22 23 24 25 26 27
  /**
   * The router builder.
   *
   * @var \Drupal\Core\Routing\RouteBuilderInterface
   */
  protected $routerBuilder;

  /**
   * Constructs the SystemConfigSubscriber.
   *
28
   * @param \Drupal\Core\Routing\RouteBuilderInterface $router_builder
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
   *   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
   */
  public function onConfigSave(ConfigCrudEvent $event) {
    $saved_config = $event->getConfig();
    if ($saved_config->getName() == 'system.theme' && ($event->isChanged('admin') || $event->isChanged('default'))) {
      $this->routerBuilder->setRebuildNeeded();
    }
  }

47
  /**
48 49
   * Checks that the configuration synchronization is valid.
   *
50 51 52
   * 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.
53
   *
54
   * @param \Drupal\Core\Config\ConfigImporterEvent $event
55 56
   *   The config import event.
   */
57
  public function onConfigImporterValidateNotEmpty(ConfigImporterEvent $event) {
58 59
    $importList = $event->getConfigImporter()->getStorageComparer()->getSourceStorage()->listAll();
    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();
62
    }
63 64 65 66 67 68 69 70
  }

  /**
   * Checks that the configuration synchronization is valid.
   *
   * This event listener checks that the system.site:uuid's in the source and
   * target match.
   *
71
   * @param \Drupal\Core\Config\ConfigImporterEvent $event
72 73 74
   *   The config import event.
   */
  public function onConfigImporterValidateSiteUUID(ConfigImporterEvent $event) {
75 76 77
    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.'));
    }
78 79
    if (!$event->getConfigImporter()->getStorageComparer()->validateSiteUuid()) {
      $event->getConfigImporter()->logError($this->t('Site UUID in source storage does not match the target storage.'));
80 81
    }
  }
82 83 84 85 86

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
87
    $events[ConfigEvents::SAVE][] = ['onConfigSave', 0];
88
    // The empty check has a high priority so that it can stop propagation if
89
    // there is no configuration to import.
90 91
    $events[ConfigEvents::IMPORT_VALIDATE][] = ['onConfigImporterValidateNotEmpty', 512];
    $events[ConfigEvents::IMPORT_VALIDATE][] = ['onConfigImporterValidateSiteUUID', 256];
92 93
    return $events;
  }
94

95
}