SystemConfigSubscriber.php 3.3 KB

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