SystemConfigSubscriber.php 2.12 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\ConfigEvents;
11
use Drupal\Core\Config\ConfigImporterEvent;
12 13
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
14 15 16 17

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

  /**
22 23
   * Checks that the configuration synchronization is valid.
   *
24 25 26
   * 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.
27
   *
28
   * @param \Drupal\Core\Config\ConfigImporterEvent $event
29 30
   *   The config import event.
   */
31
  public function onConfigImporterValidateNotEmpty(ConfigImporterEvent $event) {
32 33
    $importList = $event->getConfigImporter()->getStorageComparer()->getSourceStorage()->listAll();
    if (empty($importList)) {
34
      $event->getConfigImporter()->logError($this->t('This import is empty and if applied would delete all of your configuration, so has been rejected.'));
35
      $event->stopPropagation();
36
    }
37 38 39 40 41 42 43 44 45 46 47 48
  }

  /**
   * 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) {
49 50
    if (!$event->getConfigImporter()->getStorageComparer()->validateSiteUuid()) {
      $event->getConfigImporter()->logError($this->t('Site UUID in source storage does not match the target storage.'));
51 52
    }
  }
53 54 55 56 57 58 59 60 61 62 63

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    // 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;
  }
64
}