Mercurial > hg > isophonics-drupal-site
comparison core/modules/system/src/SystemConfigSubscriber.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 7a779792577d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\system; | |
4 | |
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 use Drupal\Core\StringTranslation\StringTranslationTrait; | |
10 use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
11 | |
12 /** | |
13 * System Config subscriber. | |
14 */ | |
15 class SystemConfigSubscriber implements EventSubscriberInterface { | |
16 use StringTranslationTrait; | |
17 | |
18 /** | |
19 * The router builder. | |
20 * | |
21 * @var \Drupal\Core\Routing\RouteBuilderInterface | |
22 */ | |
23 protected $routerBuilder; | |
24 | |
25 /** | |
26 * Constructs the SystemConfigSubscriber. | |
27 * | |
28 * @param \Drupal\Core\Routing\RouteBuilderInterface $router_builder | |
29 * The router builder service. | |
30 */ | |
31 public function __construct(RouteBuilderInterface $router_builder) { | |
32 $this->routerBuilder = $router_builder; | |
33 } | |
34 | |
35 /** | |
36 * Rebuilds the router when the default or admin theme is changed. | |
37 * | |
38 * @param \Drupal\Core\Config\ConfigCrudEvent $event | |
39 */ | |
40 public function onConfigSave(ConfigCrudEvent $event) { | |
41 $saved_config = $event->getConfig(); | |
42 if ($saved_config->getName() == 'system.theme' && ($event->isChanged('admin') || $event->isChanged('default'))) { | |
43 $this->routerBuilder->setRebuildNeeded(); | |
44 } | |
45 } | |
46 | |
47 /** | |
48 * Checks that the configuration synchronization is valid. | |
49 * | |
50 * This event listener prevents deleting all configuration. If there is | |
51 * nothing to import then event propagation is stopped because there is no | |
52 * config import to validate. | |
53 * | |
54 * @param \Drupal\Core\Config\ConfigImporterEvent $event | |
55 * The config import event. | |
56 */ | |
57 public function onConfigImporterValidateNotEmpty(ConfigImporterEvent $event) { | |
58 $importList = $event->getConfigImporter()->getStorageComparer()->getSourceStorage()->listAll(); | |
59 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 * Checks that the configuration synchronization is valid. | |
67 * | |
68 * This event listener checks that the system.site:uuid's in the source and | |
69 * target match. | |
70 * | |
71 * @param ConfigImporterEvent $event | |
72 * The config import event. | |
73 */ | |
74 public function onConfigImporterValidateSiteUUID(ConfigImporterEvent $event) { | |
75 if (!$event->getConfigImporter()->getStorageComparer()->validateSiteUuid()) { | |
76 $event->getConfigImporter()->logError($this->t('Site UUID in source storage does not match the target storage.')); | |
77 } | |
78 } | |
79 | |
80 /** | |
81 * {@inheritdoc} | |
82 */ | |
83 public static function getSubscribedEvents() { | |
84 $events[ConfigEvents::SAVE][] = ['onConfigSave', 0]; | |
85 // The empty check has a high priority so that it can stop propagation if | |
86 // there is no configuration to import. | |
87 $events[ConfigEvents::IMPORT_VALIDATE][] = ['onConfigImporterValidateNotEmpty', 512]; | |
88 $events[ConfigEvents::IMPORT_VALIDATE][] = ['onConfigImporterValidateSiteUUID', 256]; | |
89 return $events; | |
90 } | |
91 | |
92 } |