Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\language\Config;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Cache\CacheableMetadata;
|
Chris@0
|
6 use Drupal\Core\Config\ConfigCollectionInfo;
|
Chris@0
|
7 use Drupal\Core\Config\ConfigCrudEvent;
|
Chris@0
|
8 use Drupal\Core\Config\ConfigFactoryOverrideBase;
|
Chris@0
|
9 use Drupal\Core\Config\ConfigRenameEvent;
|
Chris@0
|
10 use Drupal\Core\Config\StorageInterface;
|
Chris@0
|
11 use Drupal\Core\Config\TypedConfigManagerInterface;
|
Chris@0
|
12 use Drupal\Core\Language\LanguageDefault;
|
Chris@0
|
13 use Drupal\Core\Language\LanguageInterface;
|
Chris@0
|
14 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
Chris@0
|
15 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Provides language overrides for the configuration factory.
|
Chris@0
|
19 */
|
Chris@0
|
20 class LanguageConfigFactoryOverride extends ConfigFactoryOverrideBase implements LanguageConfigFactoryOverrideInterface, EventSubscriberInterface {
|
Chris@0
|
21
|
Chris@0
|
22 use LanguageConfigCollectionNameTrait;
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * The configuration storage.
|
Chris@0
|
26 *
|
Chris@0
|
27 * Do not access this directly. Should be accessed through self::getStorage()
|
Chris@0
|
28 * so that the cache of storages per langcode is used.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @var \Drupal\Core\Config\StorageInterface
|
Chris@0
|
31 */
|
Chris@0
|
32 protected $baseStorage;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * An array of configuration storages keyed by langcode.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @var \Drupal\Core\Config\StorageInterface[]
|
Chris@0
|
38 */
|
Chris@0
|
39 protected $storages;
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * The typed config manager.
|
Chris@0
|
43 *
|
Chris@0
|
44 * @var \Drupal\Core\Config\TypedConfigManagerInterface
|
Chris@0
|
45 */
|
Chris@0
|
46 protected $typedConfigManager;
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * An event dispatcher instance to use for configuration events.
|
Chris@0
|
50 *
|
Chris@0
|
51 * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
|
Chris@0
|
52 */
|
Chris@0
|
53 protected $eventDispatcher;
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * The language object used to override configuration data.
|
Chris@0
|
57 *
|
Chris@0
|
58 * @var \Drupal\Core\Language\LanguageInterface
|
Chris@0
|
59 */
|
Chris@0
|
60 protected $language;
|
Chris@0
|
61
|
Chris@0
|
62 /**
|
Chris@0
|
63 * Constructs the LanguageConfigFactoryOverride object.
|
Chris@0
|
64 *
|
Chris@0
|
65 * @param \Drupal\Core\Config\StorageInterface $storage
|
Chris@0
|
66 * The configuration storage engine.
|
Chris@0
|
67 * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
|
Chris@0
|
68 * An event dispatcher instance to use for configuration events.
|
Chris@0
|
69 * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
|
Chris@0
|
70 * The typed configuration manager.
|
Chris@0
|
71 * @param \Drupal\Core\Language\LanguageDefault $default_language
|
Chris@0
|
72 * The default language.
|
Chris@0
|
73 */
|
Chris@0
|
74 public function __construct(StorageInterface $storage, EventDispatcherInterface $event_dispatcher, TypedConfigManagerInterface $typed_config, LanguageDefault $default_language) {
|
Chris@0
|
75 $this->baseStorage = $storage;
|
Chris@0
|
76 $this->eventDispatcher = $event_dispatcher;
|
Chris@0
|
77 $this->typedConfigManager = $typed_config;
|
Chris@17
|
78 // Prior to negotiation the override language should be the default
|
Chris@0
|
79 // language.
|
Chris@0
|
80 $this->language = $default_language->get();
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 /**
|
Chris@0
|
84 * {@inheritdoc}
|
Chris@0
|
85 */
|
Chris@0
|
86 public function loadOverrides($names) {
|
Chris@0
|
87 if ($this->language) {
|
Chris@0
|
88 $storage = $this->getStorage($this->language->getId());
|
Chris@0
|
89 return $storage->readMultiple($names);
|
Chris@0
|
90 }
|
Chris@0
|
91 return [];
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 /**
|
Chris@0
|
95 * {@inheritdoc}
|
Chris@0
|
96 */
|
Chris@0
|
97 public function getOverride($langcode, $name) {
|
Chris@0
|
98 $storage = $this->getStorage($langcode);
|
Chris@0
|
99 $data = $storage->read($name);
|
Chris@0
|
100
|
Chris@0
|
101 $override = new LanguageConfigOverride(
|
Chris@0
|
102 $name,
|
Chris@0
|
103 $storage,
|
Chris@0
|
104 $this->typedConfigManager,
|
Chris@0
|
105 $this->eventDispatcher
|
Chris@0
|
106 );
|
Chris@0
|
107
|
Chris@0
|
108 if (!empty($data)) {
|
Chris@0
|
109 $override->initWithData($data);
|
Chris@0
|
110 }
|
Chris@0
|
111 return $override;
|
Chris@0
|
112 }
|
Chris@0
|
113
|
Chris@0
|
114 /**
|
Chris@0
|
115 * {@inheritdoc}
|
Chris@0
|
116 */
|
Chris@0
|
117 public function getStorage($langcode) {
|
Chris@0
|
118 if (!isset($this->storages[$langcode])) {
|
Chris@0
|
119 $this->storages[$langcode] = $this->baseStorage->createCollection($this->createConfigCollectionName($langcode));
|
Chris@0
|
120 }
|
Chris@0
|
121 return $this->storages[$langcode];
|
Chris@0
|
122 }
|
Chris@0
|
123
|
Chris@0
|
124 /**
|
Chris@0
|
125 * {@inheritdoc}
|
Chris@0
|
126 */
|
Chris@0
|
127 public function getCacheSuffix() {
|
Chris@0
|
128 return $this->language ? $this->language->getId() : NULL;
|
Chris@0
|
129 }
|
Chris@0
|
130
|
Chris@0
|
131 /**
|
Chris@0
|
132 * {@inheritdoc}
|
Chris@0
|
133 */
|
Chris@0
|
134 public function getLanguage() {
|
Chris@0
|
135 return $this->language;
|
Chris@0
|
136 }
|
Chris@0
|
137
|
Chris@0
|
138 /**
|
Chris@0
|
139 * {@inheritdoc}
|
Chris@0
|
140 */
|
Chris@0
|
141 public function setLanguage(LanguageInterface $language = NULL) {
|
Chris@0
|
142 $this->language = $language;
|
Chris@0
|
143 return $this;
|
Chris@0
|
144 }
|
Chris@0
|
145
|
Chris@0
|
146 /**
|
Chris@0
|
147 * {@inheritdoc}
|
Chris@0
|
148 */
|
Chris@0
|
149 public function setLanguageFromDefault(LanguageDefault $language_default = NULL) {
|
Chris@0
|
150 $this->language = $language_default ? $language_default->get() : NULL;
|
Chris@0
|
151 return $this;
|
Chris@0
|
152 }
|
Chris@0
|
153
|
Chris@0
|
154 /**
|
Chris@0
|
155 * {@inheritdoc}
|
Chris@0
|
156 */
|
Chris@0
|
157 public function installLanguageOverrides($langcode) {
|
Chris@0
|
158 /** @var \Drupal\Core\Config\ConfigInstallerInterface $config_installer */
|
Chris@0
|
159 $config_installer = \Drupal::service('config.installer');
|
Chris@0
|
160 $config_installer->installCollectionDefaultConfig($this->createConfigCollectionName($langcode));
|
Chris@0
|
161 }
|
Chris@0
|
162
|
Chris@0
|
163 /**
|
Chris@0
|
164 * {@inheritdoc}
|
Chris@0
|
165 */
|
Chris@0
|
166 public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION) {
|
Chris@0
|
167 $langcode = $this->getLangcodeFromCollectionName($collection);
|
Chris@0
|
168 return $this->getOverride($langcode, $name);
|
Chris@0
|
169 }
|
Chris@0
|
170
|
Chris@0
|
171 /**
|
Chris@0
|
172 * {@inheritdoc}
|
Chris@0
|
173 */
|
Chris@0
|
174 public function addCollections(ConfigCollectionInfo $collection_info) {
|
Chris@0
|
175 foreach (\Drupal::languageManager()->getLanguages() as $language) {
|
Chris@0
|
176 $collection_info->addCollection($this->createConfigCollectionName($language->getId()), $this);
|
Chris@0
|
177 }
|
Chris@0
|
178 }
|
Chris@0
|
179
|
Chris@0
|
180 /**
|
Chris@0
|
181 * {@inheritdoc}
|
Chris@0
|
182 */
|
Chris@0
|
183 public function onConfigSave(ConfigCrudEvent $event) {
|
Chris@0
|
184 $config = $event->getConfig();
|
Chris@0
|
185 $name = $config->getName();
|
Chris@0
|
186 foreach (\Drupal::languageManager()->getLanguages() as $language) {
|
Chris@0
|
187 $config_translation = $this->getOverride($language->getId(), $name);
|
Chris@0
|
188 if (!$config_translation->isNew()) {
|
Chris@0
|
189 $this->filterOverride($config, $config_translation);
|
Chris@0
|
190 }
|
Chris@0
|
191 }
|
Chris@0
|
192 }
|
Chris@0
|
193
|
Chris@0
|
194 /**
|
Chris@0
|
195 * {@inheritdoc}
|
Chris@0
|
196 */
|
Chris@0
|
197 public function onConfigRename(ConfigRenameEvent $event) {
|
Chris@0
|
198 $config = $event->getConfig();
|
Chris@0
|
199 $name = $config->getName();
|
Chris@0
|
200 $old_name = $event->getOldName();
|
Chris@0
|
201 foreach (\Drupal::languageManager()->getLanguages() as $language) {
|
Chris@0
|
202 $config_translation = $this->getOverride($language->getId(), $old_name);
|
Chris@0
|
203 if (!$config_translation->isNew()) {
|
Chris@0
|
204 $saved_config = $config_translation->get();
|
Chris@0
|
205 $storage = $this->getStorage($language->getId());
|
Chris@0
|
206 $storage->write($name, $saved_config);
|
Chris@0
|
207 $config_translation->delete();
|
Chris@0
|
208 }
|
Chris@0
|
209 }
|
Chris@0
|
210 }
|
Chris@0
|
211
|
Chris@0
|
212 /**
|
Chris@0
|
213 * {@inheritdoc}
|
Chris@0
|
214 */
|
Chris@0
|
215 public function onConfigDelete(ConfigCrudEvent $event) {
|
Chris@0
|
216 $config = $event->getConfig();
|
Chris@0
|
217 $name = $config->getName();
|
Chris@0
|
218 foreach (\Drupal::languageManager()->getLanguages() as $language) {
|
Chris@0
|
219 $config_translation = $this->getOverride($language->getId(), $name);
|
Chris@0
|
220 if (!$config_translation->isNew()) {
|
Chris@0
|
221 $config_translation->delete();
|
Chris@0
|
222 }
|
Chris@0
|
223 }
|
Chris@0
|
224 }
|
Chris@0
|
225
|
Chris@0
|
226 /**
|
Chris@0
|
227 * {@inheritdoc}
|
Chris@0
|
228 */
|
Chris@0
|
229 public function getCacheableMetadata($name) {
|
Chris@0
|
230 $metadata = new CacheableMetadata();
|
Chris@0
|
231 if ($this->language) {
|
Chris@0
|
232 $metadata->setCacheContexts(['languages:language_interface']);
|
Chris@0
|
233 }
|
Chris@0
|
234 return $metadata;
|
Chris@0
|
235 }
|
Chris@0
|
236
|
Chris@0
|
237 }
|