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

3 4 5 6 7 8 9
/**
 * @file
 * Contains \Drupal\system\SystemConfigSubscriber.
 */

namespace Drupal\system;

10
use Drupal\Core\Config\ConfigCrudEvent;
11
use Drupal\Core\Config\ConfigEvents;
12
use Drupal\Core\Config\ConfigImporterEvent;
13
use Drupal\Core\Routing\RouteBuilderInterface;
14 15
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16 17 18 19

/**
 * System Config subscriber.
 */
20 21
class SystemConfigSubscriber implements EventSubscriberInterface {
  use StringTranslationTrait;
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  /**
   * The router builder.
   *
   * @var \Drupal\Core\Routing\RouteBuilderInterface
   */
  protected $routerBuilder;

  /**
   * Constructs the SystemConfigSubscriber.
   *
   * @param \Drupal\Core\Routing\RouteBuilderInterface $route_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
   */
  public function onConfigSave(ConfigCrudEvent $event) {
    $saved_config = $event->getConfig();
    if ($saved_config->getName() == 'system.theme' && ($event->isChanged('admin') || $event->isChanged('default'))) {
      $this->routerBuilder->setRebuildNeeded();
    }
  }

52
  /**
53 54
   * Checks that the configuration synchronization is valid.
   *
55 56 57
   * 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.
58
   *
59
   * @param \Drupal\Core\Config\ConfigImporterEvent $event
60 61
   *   The config import event.
   */
62
  public function onConfigImporterValidateNotEmpty(ConfigImporterEvent $event) {
63 64
    $importList = $event->getConfigImporter()->getStorageComparer()->getSourceStorage()->listAll();
    if (empty($importList)) {
65
      $event->getConfigImporter()->logError($this->t('This import is empty and if applied would delete all of your configuration, so has been rejected.'));
66
      $event->stopPropagation();
67
    }
68 69 70 71 72 73 74 75 76 77 78 79
  }

  /**
   * Checks that the configuration synchronization is valid.
   *
   * This event listener checks that the system.site:uuid's in the source and
   * target match.
   *
   * @param ConfigImporterEvent $event
   *   The config import event.
   */
  public function onConfigImporterValidateSiteUUID(ConfigImporterEvent $event) {
80 81
    if (!$event->getConfigImporter()->getStorageComparer()->validateSiteUuid()) {
      $event->getConfigImporter()->logError($this->t('Site UUID in source storage does not match the target storage.'));
82 83
    }
  }
84 85 86 87 88

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