Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Config;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Diff\Diff;
|
Chris@0
|
6 use Drupal\Core\Config\Entity\ConfigDependencyManager;
|
Chris@0
|
7 use Drupal\Core\Config\Entity\ConfigEntityInterface;
|
Chris@0
|
8 use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
|
Chris@0
|
9 use Drupal\Core\Entity\EntityManagerInterface;
|
Chris@0
|
10 use Drupal\Core\Entity\EntityTypeInterface;
|
Chris@0
|
11 use Drupal\Core\Serialization\Yaml;
|
Chris@0
|
12 use Drupal\Core\StringTranslation\StringTranslationTrait;
|
Chris@0
|
13 use Drupal\Core\StringTranslation\TranslationInterface;
|
Chris@0
|
14 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * The ConfigManager provides helper functions for the configuration system.
|
Chris@0
|
18 */
|
Chris@0
|
19 class ConfigManager implements ConfigManagerInterface {
|
Chris@0
|
20 use StringTranslationTrait;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * The entity manager.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @var \Drupal\Core\Entity\EntityManagerInterface
|
Chris@0
|
26 */
|
Chris@0
|
27 protected $entityManager;
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * The configuration factory.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @var \Drupal\Core\Config\ConfigFactoryInterface
|
Chris@0
|
33 */
|
Chris@0
|
34 protected $configFactory;
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * The typed config manager.
|
Chris@0
|
38 *
|
Chris@0
|
39 * @var \Drupal\Core\Config\TypedConfigManagerInterface
|
Chris@0
|
40 */
|
Chris@0
|
41 protected $typedConfigManager;
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * The active configuration storage.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @var \Drupal\Core\Config\StorageInterface
|
Chris@0
|
47 */
|
Chris@0
|
48 protected $activeStorage;
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * The event dispatcher.
|
Chris@0
|
52 *
|
Chris@0
|
53 * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
|
Chris@0
|
54 */
|
Chris@0
|
55 protected $eventDispatcher;
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * The configuration collection info.
|
Chris@0
|
59 *
|
Chris@0
|
60 * @var \Drupal\Core\Config\ConfigCollectionInfo
|
Chris@0
|
61 */
|
Chris@0
|
62 protected $configCollectionInfo;
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * The configuration storages keyed by collection name.
|
Chris@0
|
66 *
|
Chris@0
|
67 * @var \Drupal\Core\Config\StorageInterface[]
|
Chris@0
|
68 */
|
Chris@0
|
69 protected $storages;
|
Chris@0
|
70
|
Chris@0
|
71 /**
|
Chris@0
|
72 * Creates ConfigManager objects.
|
Chris@0
|
73 *
|
Chris@0
|
74 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
|
Chris@0
|
75 * The entity manager.
|
Chris@0
|
76 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
|
Chris@0
|
77 * The configuration factory.
|
Chris@0
|
78 * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config_manager
|
Chris@0
|
79 * The typed config manager.
|
Chris@0
|
80 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
|
Chris@0
|
81 * The string translation service.
|
Chris@0
|
82 * @param \Drupal\Core\Config\StorageInterface $active_storage
|
Chris@0
|
83 * The active configuration storage.
|
Chris@0
|
84 * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
|
Chris@0
|
85 * The event dispatcher.
|
Chris@0
|
86 */
|
Chris@0
|
87 public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, TypedConfigManagerInterface $typed_config_manager, TranslationInterface $string_translation, StorageInterface $active_storage, EventDispatcherInterface $event_dispatcher) {
|
Chris@0
|
88 $this->entityManager = $entity_manager;
|
Chris@0
|
89 $this->configFactory = $config_factory;
|
Chris@0
|
90 $this->typedConfigManager = $typed_config_manager;
|
Chris@0
|
91 $this->stringTranslation = $string_translation;
|
Chris@0
|
92 $this->activeStorage = $active_storage;
|
Chris@0
|
93 $this->eventDispatcher = $event_dispatcher;
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 /**
|
Chris@0
|
97 * {@inheritdoc}
|
Chris@0
|
98 */
|
Chris@0
|
99 public function getEntityTypeIdByName($name) {
|
Chris@0
|
100 $entities = array_filter($this->entityManager->getDefinitions(), function (EntityTypeInterface $entity_type) use ($name) {
|
Chris@0
|
101 return ($entity_type instanceof ConfigEntityTypeInterface && $config_prefix = $entity_type->getConfigPrefix()) && strpos($name, $config_prefix . '.') === 0;
|
Chris@0
|
102 });
|
Chris@0
|
103 return key($entities);
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 /**
|
Chris@0
|
107 * {@inheritdoc}
|
Chris@0
|
108 */
|
Chris@0
|
109 public function loadConfigEntityByName($name) {
|
Chris@0
|
110 $entity_type_id = $this->getEntityTypeIdByName($name);
|
Chris@0
|
111 if ($entity_type_id) {
|
Chris@0
|
112 $entity_type = $this->entityManager->getDefinition($entity_type_id);
|
Chris@0
|
113 $id = substr($name, strlen($entity_type->getConfigPrefix()) + 1);
|
Chris@0
|
114 return $this->entityManager->getStorage($entity_type_id)->load($id);
|
Chris@0
|
115 }
|
Chris@0
|
116 return NULL;
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 /**
|
Chris@0
|
120 * {@inheritdoc}
|
Chris@0
|
121 */
|
Chris@0
|
122 public function getEntityManager() {
|
Chris@0
|
123 return $this->entityManager;
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 /**
|
Chris@0
|
127 * {@inheritdoc}
|
Chris@0
|
128 */
|
Chris@0
|
129 public function getConfigFactory() {
|
Chris@0
|
130 return $this->configFactory;
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@0
|
133 /**
|
Chris@0
|
134 * {@inheritdoc}
|
Chris@0
|
135 */
|
Chris@0
|
136 public function diff(StorageInterface $source_storage, StorageInterface $target_storage, $source_name, $target_name = NULL, $collection = StorageInterface::DEFAULT_COLLECTION) {
|
Chris@0
|
137 if ($collection != StorageInterface::DEFAULT_COLLECTION) {
|
Chris@0
|
138 $source_storage = $source_storage->createCollection($collection);
|
Chris@0
|
139 $target_storage = $target_storage->createCollection($collection);
|
Chris@0
|
140 }
|
Chris@0
|
141 if (!isset($target_name)) {
|
Chris@0
|
142 $target_name = $source_name;
|
Chris@0
|
143 }
|
Chris@0
|
144 // The output should show configuration object differences formatted as YAML.
|
Chris@0
|
145 // But the configuration is not necessarily stored in files. Therefore, they
|
Chris@0
|
146 // need to be read and parsed, and lastly, dumped into YAML strings.
|
Chris@0
|
147 $source_data = explode("\n", Yaml::encode($source_storage->read($source_name)));
|
Chris@0
|
148 $target_data = explode("\n", Yaml::encode($target_storage->read($target_name)));
|
Chris@0
|
149
|
Chris@0
|
150 // Check for new or removed files.
|
Chris@0
|
151 if ($source_data === ['false']) {
|
Chris@0
|
152 // Added file.
|
Chris@0
|
153 // Cast the result of t() to a string, as the diff engine doesn't know
|
Chris@0
|
154 // about objects.
|
Chris@0
|
155 $source_data = [(string) $this->t('File added')];
|
Chris@0
|
156 }
|
Chris@0
|
157 if ($target_data === ['false']) {
|
Chris@0
|
158 // Deleted file.
|
Chris@0
|
159 // Cast the result of t() to a string, as the diff engine doesn't know
|
Chris@0
|
160 // about objects.
|
Chris@0
|
161 $target_data = [(string) $this->t('File removed')];
|
Chris@0
|
162 }
|
Chris@0
|
163
|
Chris@0
|
164 return new Diff($source_data, $target_data);
|
Chris@0
|
165 }
|
Chris@0
|
166
|
Chris@0
|
167 /**
|
Chris@0
|
168 * {@inheritdoc}
|
Chris@0
|
169 */
|
Chris@0
|
170 public function createSnapshot(StorageInterface $source_storage, StorageInterface $snapshot_storage) {
|
Chris@0
|
171 // Empty the snapshot of all configuration.
|
Chris@0
|
172 $snapshot_storage->deleteAll();
|
Chris@0
|
173 foreach ($snapshot_storage->getAllCollectionNames() as $collection) {
|
Chris@0
|
174 $snapshot_collection = $snapshot_storage->createCollection($collection);
|
Chris@0
|
175 $snapshot_collection->deleteAll();
|
Chris@0
|
176 }
|
Chris@0
|
177 foreach ($source_storage->listAll() as $name) {
|
Chris@0
|
178 $snapshot_storage->write($name, $source_storage->read($name));
|
Chris@0
|
179 }
|
Chris@0
|
180 // Copy collections as well.
|
Chris@0
|
181 foreach ($source_storage->getAllCollectionNames() as $collection) {
|
Chris@0
|
182 $source_collection = $source_storage->createCollection($collection);
|
Chris@0
|
183 $snapshot_collection = $snapshot_storage->createCollection($collection);
|
Chris@0
|
184 foreach ($source_collection->listAll() as $name) {
|
Chris@0
|
185 $snapshot_collection->write($name, $source_collection->read($name));
|
Chris@0
|
186 }
|
Chris@0
|
187 }
|
Chris@0
|
188 }
|
Chris@0
|
189
|
Chris@0
|
190 /**
|
Chris@0
|
191 * {@inheritdoc}
|
Chris@0
|
192 */
|
Chris@0
|
193 public function uninstall($type, $name) {
|
Chris@0
|
194 $entities = $this->getConfigEntitiesToChangeOnDependencyRemoval($type, [$name], FALSE);
|
Chris@0
|
195 // Fix all dependent configuration entities.
|
Chris@0
|
196 /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity */
|
Chris@0
|
197 foreach ($entities['update'] as $entity) {
|
Chris@0
|
198 $entity->save();
|
Chris@0
|
199 }
|
Chris@0
|
200 // Remove all dependent configuration entities.
|
Chris@0
|
201 foreach ($entities['delete'] as $entity) {
|
Chris@0
|
202 $entity->setUninstalling(TRUE);
|
Chris@0
|
203 $entity->delete();
|
Chris@0
|
204 }
|
Chris@0
|
205
|
Chris@0
|
206 $config_names = $this->configFactory->listAll($name . '.');
|
Chris@0
|
207 foreach ($config_names as $config_name) {
|
Chris@0
|
208 $this->configFactory->getEditable($config_name)->delete();
|
Chris@0
|
209 }
|
Chris@0
|
210
|
Chris@0
|
211 // Remove any matching configuration from collections.
|
Chris@0
|
212 foreach ($this->activeStorage->getAllCollectionNames() as $collection) {
|
Chris@0
|
213 $collection_storage = $this->activeStorage->createCollection($collection);
|
Chris@0
|
214 $collection_storage->deleteAll($name . '.');
|
Chris@0
|
215 }
|
Chris@0
|
216
|
Chris@0
|
217 $schema_dir = drupal_get_path($type, $name) . '/' . InstallStorage::CONFIG_SCHEMA_DIRECTORY;
|
Chris@0
|
218 if (is_dir($schema_dir)) {
|
Chris@0
|
219 // Refresh the schema cache if uninstalling an extension that provides
|
Chris@0
|
220 // configuration schema.
|
Chris@0
|
221 $this->typedConfigManager->clearCachedDefinitions();
|
Chris@0
|
222 }
|
Chris@0
|
223 }
|
Chris@0
|
224
|
Chris@0
|
225 /**
|
Chris@0
|
226 * {@inheritdoc}
|
Chris@0
|
227 */
|
Chris@0
|
228 public function getConfigDependencyManager() {
|
Chris@0
|
229 $dependency_manager = new ConfigDependencyManager();
|
Chris@0
|
230 // Read all configuration using the factory. This ensures that multiple
|
Chris@0
|
231 // deletes during the same request benefit from the static cache. Using the
|
Chris@0
|
232 // factory also ensures configuration entity dependency discovery has no
|
Chris@0
|
233 // dependencies on the config entity classes. Assume data with UUID is a
|
Chris@0
|
234 // config entity. Only configuration entities can be depended on so we can
|
Chris@0
|
235 // ignore everything else.
|
Chris@0
|
236 $data = array_map(function ($config) {
|
Chris@0
|
237 $data = $config->get();
|
Chris@0
|
238 if (isset($data['uuid'])) {
|
Chris@0
|
239 return $data;
|
Chris@0
|
240 }
|
Chris@0
|
241 return FALSE;
|
Chris@0
|
242 }, $this->configFactory->loadMultiple($this->activeStorage->listAll()));
|
Chris@0
|
243 $dependency_manager->setData(array_filter($data));
|
Chris@0
|
244 return $dependency_manager;
|
Chris@0
|
245 }
|
Chris@0
|
246
|
Chris@0
|
247 /**
|
Chris@0
|
248 * {@inheritdoc}
|
Chris@0
|
249 */
|
Chris@0
|
250 public function findConfigEntityDependents($type, array $names, ConfigDependencyManager $dependency_manager = NULL) {
|
Chris@0
|
251 if (!$dependency_manager) {
|
Chris@0
|
252 $dependency_manager = $this->getConfigDependencyManager();
|
Chris@0
|
253 }
|
Chris@0
|
254 $dependencies = [];
|
Chris@0
|
255 foreach ($names as $name) {
|
Chris@0
|
256 $dependencies = array_merge($dependencies, $dependency_manager->getDependentEntities($type, $name));
|
Chris@0
|
257 }
|
Chris@0
|
258 return $dependencies;
|
Chris@0
|
259 }
|
Chris@0
|
260
|
Chris@0
|
261 /**
|
Chris@0
|
262 * {@inheritdoc}
|
Chris@0
|
263 */
|
Chris@0
|
264 public function findConfigEntityDependentsAsEntities($type, array $names, ConfigDependencyManager $dependency_manager = NULL) {
|
Chris@0
|
265 $dependencies = $this->findConfigEntityDependents($type, $names, $dependency_manager);
|
Chris@0
|
266 $entities = [];
|
Chris@0
|
267 $definitions = $this->entityManager->getDefinitions();
|
Chris@0
|
268 foreach ($dependencies as $config_name => $dependency) {
|
Chris@0
|
269 // Group by entity type to efficient load entities using
|
Chris@0
|
270 // \Drupal\Core\Entity\EntityStorageInterface::loadMultiple().
|
Chris@0
|
271 $entity_type_id = $this->getEntityTypeIdByName($config_name);
|
Chris@0
|
272 // It is possible that a non-configuration entity will be returned if a
|
Chris@0
|
273 // simple configuration object has a UUID key. This would occur if the
|
Chris@0
|
274 // dependents of the system module are calculated since system.site has
|
Chris@0
|
275 // a UUID key.
|
Chris@0
|
276 if ($entity_type_id) {
|
Chris@0
|
277 $id = substr($config_name, strlen($definitions[$entity_type_id]->getConfigPrefix()) + 1);
|
Chris@0
|
278 $entities[$entity_type_id][] = $id;
|
Chris@0
|
279 }
|
Chris@0
|
280 }
|
Chris@0
|
281 $entities_to_return = [];
|
Chris@0
|
282 foreach ($entities as $entity_type_id => $entities_to_load) {
|
Chris@0
|
283 $storage = $this->entityManager->getStorage($entity_type_id);
|
Chris@0
|
284 // Remove the keys since there are potential ID clashes from different
|
Chris@0
|
285 // configuration entity types.
|
Chris@0
|
286 $entities_to_return = array_merge($entities_to_return, array_values($storage->loadMultiple($entities_to_load)));
|
Chris@0
|
287 }
|
Chris@0
|
288 return $entities_to_return;
|
Chris@0
|
289 }
|
Chris@0
|
290
|
Chris@0
|
291 /**
|
Chris@0
|
292 * {@inheritdoc}
|
Chris@0
|
293 */
|
Chris@0
|
294 public function getConfigEntitiesToChangeOnDependencyRemoval($type, array $names, $dry_run = TRUE) {
|
Chris@0
|
295 $dependency_manager = $this->getConfigDependencyManager();
|
Chris@0
|
296
|
Chris@12
|
297 // Store the list of dependents in three separate variables. This allows us
|
Chris@12
|
298 // to determine how the dependency graph changes as entities are fixed by
|
Chris@12
|
299 // calling the onDependencyRemoval() method.
|
Chris@12
|
300
|
Chris@12
|
301 // The list of original dependents on $names. This list never changes.
|
Chris@12
|
302 $original_dependents = $this->findConfigEntityDependentsAsEntities($type, $names, $dependency_manager);
|
Chris@12
|
303
|
Chris@12
|
304 // The current list of dependents on $names. This list is recalculated when
|
Chris@12
|
305 // calling an entity's onDependencyRemoval() method results in the entity
|
Chris@12
|
306 // changing. This list is passed to each entity's onDependencyRemoval()
|
Chris@12
|
307 // method as the list of affected entities.
|
Chris@12
|
308 $current_dependents = $original_dependents;
|
Chris@12
|
309
|
Chris@12
|
310 // The list of dependents to process. This list changes as entities are
|
Chris@12
|
311 // processed and are either fixed or deleted.
|
Chris@12
|
312 $dependents_to_process = $original_dependents;
|
Chris@12
|
313
|
Chris@12
|
314 // Initialize other variables.
|
Chris@12
|
315 $affected_uuids = [];
|
Chris@0
|
316 $return = [
|
Chris@0
|
317 'update' => [],
|
Chris@0
|
318 'delete' => [],
|
Chris@0
|
319 'unchanged' => [],
|
Chris@0
|
320 ];
|
Chris@0
|
321
|
Chris@12
|
322 // Try to fix the dependents and find out what will happen to the dependency
|
Chris@12
|
323 // graph. Entities are processed in the order of most dependent first. For
|
Chris@12
|
324 // example, this ensures that Menu UI third party dependencies on node types
|
Chris@12
|
325 // are fixed before processing the node type's other dependents.
|
Chris@12
|
326 while ($dependent = array_pop($dependents_to_process)) {
|
Chris@0
|
327 /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $dependent */
|
Chris@0
|
328 if ($dry_run) {
|
Chris@0
|
329 // Clone the entity so any changes do not change any static caches.
|
Chris@0
|
330 $dependent = clone $dependent;
|
Chris@0
|
331 }
|
Chris@0
|
332 $fixed = FALSE;
|
Chris@12
|
333 if ($this->callOnDependencyRemoval($dependent, $current_dependents, $type, $names)) {
|
Chris@0
|
334 // Recalculate dependencies and update the dependency graph data.
|
Chris@0
|
335 $dependent->calculateDependencies();
|
Chris@0
|
336 $dependency_manager->updateData($dependent->getConfigDependencyName(), $dependent->getDependencies());
|
Chris@12
|
337 // Based on the updated data rebuild the list of current dependents.
|
Chris@12
|
338 // This will remove entities that are no longer dependent after the
|
Chris@12
|
339 // recalculation.
|
Chris@12
|
340 $current_dependents = $this->findConfigEntityDependentsAsEntities($type, $names, $dependency_manager);
|
Chris@12
|
341 // Rebuild the list of entities that we need to process using the new
|
Chris@12
|
342 // list of current dependents and removing any entities that we've
|
Chris@12
|
343 // already processed.
|
Chris@12
|
344 $dependents_to_process = array_filter($current_dependents, function ($current_dependent) use ($affected_uuids) {
|
Chris@12
|
345 return !in_array($current_dependent->uuid(), $affected_uuids);
|
Chris@0
|
346 });
|
Chris@12
|
347 // Ensure that the dependent has actually been fixed. It is possible
|
Chris@12
|
348 // that other dependencies cause it to still be in the list.
|
Chris@0
|
349 $fixed = TRUE;
|
Chris@12
|
350 foreach ($dependents_to_process as $key => $entity) {
|
Chris@0
|
351 if ($entity->uuid() == $dependent->uuid()) {
|
Chris@0
|
352 $fixed = FALSE;
|
Chris@12
|
353 unset($dependents_to_process[$key]);
|
Chris@0
|
354 break;
|
Chris@0
|
355 }
|
Chris@0
|
356 }
|
Chris@0
|
357 if ($fixed) {
|
Chris@12
|
358 $affected_uuids[] = $dependent->uuid();
|
Chris@0
|
359 $return['update'][] = $dependent;
|
Chris@0
|
360 }
|
Chris@0
|
361 }
|
Chris@0
|
362 // If the entity cannot be fixed then it has to be deleted.
|
Chris@0
|
363 if (!$fixed) {
|
Chris@12
|
364 $affected_uuids[] = $dependent->uuid();
|
Chris@0
|
365 // Deletes should occur in the order of the least dependent first. For
|
Chris@0
|
366 // example, this ensures that fields are removed before field storages.
|
Chris@0
|
367 array_unshift($return['delete'], $dependent);
|
Chris@0
|
368 }
|
Chris@0
|
369 }
|
Chris@12
|
370 // Use the list of affected UUIDs to filter the original list to work out
|
Chris@12
|
371 // which configuration entities are unchanged.
|
Chris@12
|
372 $return['unchanged'] = array_filter($original_dependents, function ($dependent) use ($affected_uuids) {
|
Chris@12
|
373 return !(in_array($dependent->uuid(), $affected_uuids));
|
Chris@0
|
374 });
|
Chris@0
|
375
|
Chris@0
|
376 return $return;
|
Chris@0
|
377 }
|
Chris@0
|
378
|
Chris@0
|
379 /**
|
Chris@0
|
380 * {@inheritdoc}
|
Chris@0
|
381 */
|
Chris@0
|
382 public function getConfigCollectionInfo() {
|
Chris@0
|
383 if (!isset($this->configCollectionInfo)) {
|
Chris@0
|
384 $this->configCollectionInfo = new ConfigCollectionInfo();
|
Chris@0
|
385 $this->eventDispatcher->dispatch(ConfigEvents::COLLECTION_INFO, $this->configCollectionInfo);
|
Chris@0
|
386 }
|
Chris@0
|
387 return $this->configCollectionInfo;
|
Chris@0
|
388 }
|
Chris@0
|
389
|
Chris@0
|
390 /**
|
Chris@0
|
391 * Calls an entity's onDependencyRemoval() method.
|
Chris@0
|
392 *
|
Chris@0
|
393 * A helper method to call onDependencyRemoval() with the correct list of
|
Chris@0
|
394 * affected entities. This list should only contain dependencies on the
|
Chris@0
|
395 * entity. Configuration and content entity dependencies will be converted
|
Chris@0
|
396 * into entity objects.
|
Chris@0
|
397 *
|
Chris@0
|
398 * @param \Drupal\Core\Config\Entity\ConfigEntityInterface $entity
|
Chris@0
|
399 * The entity to call onDependencyRemoval() on.
|
Chris@0
|
400 * @param \Drupal\Core\Config\Entity\ConfigEntityInterface[] $dependent_entities
|
Chris@0
|
401 * The list of dependent configuration entities.
|
Chris@0
|
402 * @param string $type
|
Chris@0
|
403 * The type of dependency being checked. Either 'module', 'theme', 'config'
|
Chris@0
|
404 * or 'content'.
|
Chris@0
|
405 * @param array $names
|
Chris@0
|
406 * The specific names to check. If $type equals 'module' or 'theme' then it
|
Chris@0
|
407 * should be a list of module names or theme names. In the case of 'config'
|
Chris@0
|
408 * or 'content' it should be a list of configuration dependency names.
|
Chris@0
|
409 *
|
Chris@0
|
410 * @return bool
|
Chris@0
|
411 * TRUE if the entity has changed as a result of calling the
|
Chris@0
|
412 * onDependencyRemoval() method, FALSE if not.
|
Chris@0
|
413 */
|
Chris@0
|
414 protected function callOnDependencyRemoval(ConfigEntityInterface $entity, array $dependent_entities, $type, array $names) {
|
Chris@0
|
415 $entity_dependencies = $entity->getDependencies();
|
Chris@0
|
416 if (empty($entity_dependencies)) {
|
Chris@0
|
417 // No dependent entities nothing to do.
|
Chris@0
|
418 return FALSE;
|
Chris@0
|
419 }
|
Chris@0
|
420
|
Chris@0
|
421 $affected_dependencies = [
|
Chris@0
|
422 'config' => [],
|
Chris@0
|
423 'content' => [],
|
Chris@0
|
424 'module' => [],
|
Chris@0
|
425 'theme' => [],
|
Chris@0
|
426 ];
|
Chris@0
|
427
|
Chris@0
|
428 // Work out if any of the entity's dependencies are going to be affected.
|
Chris@0
|
429 if (isset($entity_dependencies[$type])) {
|
Chris@0
|
430 // Work out which dependencies the entity has in common with the provided
|
Chris@0
|
431 // $type and $names.
|
Chris@0
|
432 $affected_dependencies[$type] = array_intersect($entity_dependencies[$type], $names);
|
Chris@0
|
433
|
Chris@0
|
434 // If the dependencies are entities we need to convert them into objects.
|
Chris@0
|
435 if ($type == 'config' || $type == 'content') {
|
Chris@0
|
436 $affected_dependencies[$type] = array_map(function ($name) use ($type) {
|
Chris@0
|
437 if ($type == 'config') {
|
Chris@0
|
438 return $this->loadConfigEntityByName($name);
|
Chris@0
|
439 }
|
Chris@0
|
440 else {
|
Chris@0
|
441 // Ignore the bundle.
|
Chris@0
|
442 list($entity_type_id,, $uuid) = explode(':', $name);
|
Chris@0
|
443 return $this->entityManager->loadEntityByConfigTarget($entity_type_id, $uuid);
|
Chris@0
|
444 }
|
Chris@0
|
445 }, $affected_dependencies[$type]);
|
Chris@0
|
446 }
|
Chris@0
|
447 }
|
Chris@0
|
448
|
Chris@0
|
449 // Merge any other configuration entities into the list of affected
|
Chris@0
|
450 // dependencies if necessary.
|
Chris@0
|
451 if (isset($entity_dependencies['config'])) {
|
Chris@0
|
452 foreach ($dependent_entities as $dependent_entity) {
|
Chris@0
|
453 if (in_array($dependent_entity->getConfigDependencyName(), $entity_dependencies['config'])) {
|
Chris@0
|
454 $affected_dependencies['config'][] = $dependent_entity;
|
Chris@0
|
455 }
|
Chris@0
|
456 }
|
Chris@0
|
457 }
|
Chris@0
|
458
|
Chris@0
|
459 // Key the entity arrays by config dependency name to make searching easy.
|
Chris@0
|
460 foreach (['config', 'content'] as $dependency_type) {
|
Chris@0
|
461 $affected_dependencies[$dependency_type] = array_combine(
|
Chris@0
|
462 array_map(function ($entity) {
|
Chris@0
|
463 return $entity->getConfigDependencyName();
|
Chris@0
|
464 }, $affected_dependencies[$dependency_type]),
|
Chris@0
|
465 $affected_dependencies[$dependency_type]
|
Chris@0
|
466 );
|
Chris@0
|
467 }
|
Chris@0
|
468
|
Chris@0
|
469 // Inform the entity.
|
Chris@0
|
470 return $entity->onDependencyRemoval($affected_dependencies);
|
Chris@0
|
471 }
|
Chris@0
|
472
|
Chris@0
|
473 /**
|
Chris@0
|
474 * {@inheritdoc}
|
Chris@0
|
475 */
|
Chris@0
|
476 public function findMissingContentDependencies() {
|
Chris@0
|
477 $content_dependencies = [];
|
Chris@0
|
478 $missing_dependencies = [];
|
Chris@0
|
479 foreach ($this->activeStorage->readMultiple($this->activeStorage->listAll()) as $config_data) {
|
Chris@0
|
480 if (isset($config_data['dependencies']['content'])) {
|
Chris@0
|
481 $content_dependencies = array_merge($content_dependencies, $config_data['dependencies']['content']);
|
Chris@0
|
482 }
|
Chris@0
|
483 if (isset($config_data['dependencies']['enforced']['content'])) {
|
Chris@0
|
484 $content_dependencies = array_merge($content_dependencies, $config_data['dependencies']['enforced']['content']);
|
Chris@0
|
485 }
|
Chris@0
|
486 }
|
Chris@0
|
487 foreach (array_unique($content_dependencies) as $content_dependency) {
|
Chris@0
|
488 // Format of the dependency is entity_type:bundle:uuid.
|
Chris@0
|
489 list($entity_type, $bundle, $uuid) = explode(':', $content_dependency, 3);
|
Chris@0
|
490 if (!$this->entityManager->loadEntityByUuid($entity_type, $uuid)) {
|
Chris@0
|
491 $missing_dependencies[$uuid] = [
|
Chris@0
|
492 'entity_type' => $entity_type,
|
Chris@0
|
493 'bundle' => $bundle,
|
Chris@0
|
494 'uuid' => $uuid,
|
Chris@0
|
495 ];
|
Chris@0
|
496 }
|
Chris@0
|
497 }
|
Chris@0
|
498 return $missing_dependencies;
|
Chris@0
|
499 }
|
Chris@0
|
500
|
Chris@0
|
501 }
|