Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / lib / Drupal / Core / Config / Entity / ConfigDependencyDeleteFormTrait.php
1 <?php
2
3 namespace Drupal\Core\Config\Entity;
4
5 use Drupal\Core\Config\ConfigManagerInterface;
6 use Drupal\Core\Entity\EntityManagerInterface;
7
8 /**
9  * Lists affected configuration entities by a dependency removal.
10  *
11  * This trait relies on the StringTranslationTrait.
12  */
13 trait ConfigDependencyDeleteFormTrait {
14
15   /**
16    * Translates a string to the current language or to a given language.
17    *
18    * Provided by \Drupal\Core\StringTranslation\StringTranslationTrait.
19    */
20   abstract protected function t($string, array $args = [], array $options = []);
21
22   /**
23    * Adds form elements to list affected configuration entities.
24    *
25    * @param array $form
26    *   The form array to add elements to.
27    * @param string $type
28    *   The type of dependency being checked. Either 'module', 'theme', 'config'
29    *   or 'content'.
30    * @param array $names
31    *   The specific names to check. If $type equals 'module' or 'theme' then it
32    *   should be a list of module names or theme names. In the case of 'config'
33    *   or 'content' it should be a list of configuration dependency names.
34    * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
35    *   The config manager.
36    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
37    *   The entity manager.
38    *
39    * @see \Drupal\Core\Config\ConfigManagerInterface::getConfigEntitiesToChangeOnDependencyRemoval()
40    */
41   protected function addDependencyListsToForm(array &$form, $type, array $names, ConfigManagerInterface $config_manager, EntityManagerInterface $entity_manager) {
42     // Get the dependent entities.
43     $dependent_entities = $config_manager->getConfigEntitiesToChangeOnDependencyRemoval($type, $names);
44     $entity_types = [];
45
46     $form['entity_updates'] = [
47       '#type' => 'details',
48       '#title' => $this->t('Configuration updates'),
49       '#description' => $this->t('The listed configuration will be updated.'),
50       '#open' => TRUE,
51       '#access' => FALSE,
52     ];
53
54     foreach ($dependent_entities['update'] as $entity) {
55       /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface  $entity */
56       $entity_type_id = $entity->getEntityTypeId();
57       if (!isset($form['entity_updates'][$entity_type_id])) {
58         $entity_type = $entity_manager->getDefinition($entity_type_id);
59         // Store the ID and label to sort the entity types and entities later.
60         $label = $entity_type->getLabel();
61         $entity_types[$entity_type_id] = $label;
62         $form['entity_updates'][$entity_type_id] = [
63           '#theme' => 'item_list',
64           '#title' => $label,
65           '#items' => [],
66         ];
67       }
68       $form['entity_updates'][$entity_type_id]['#items'][$entity->id()] = $entity->label() ?: $entity->id();
69     }
70     if (!empty($dependent_entities['update'])) {
71       $form['entity_updates']['#access'] = TRUE;
72
73       // Add a weight key to the entity type sections.
74       asort($entity_types, SORT_FLAG_CASE);
75       $weight = 0;
76       foreach ($entity_types as $entity_type_id => $label) {
77         $form['entity_updates'][$entity_type_id]['#weight'] = $weight;
78         // Sort the list of entity labels alphabetically.
79         ksort($form['entity_updates'][$entity_type_id]['#items'], SORT_FLAG_CASE);
80         $weight++;
81       }
82     }
83
84     $form['entity_deletes'] = [
85       '#type' => 'details',
86       '#title' => $this->t('Configuration deletions'),
87       '#description' => $this->t('The listed configuration will be deleted.'),
88       '#open' => TRUE,
89       '#access' => FALSE,
90     ];
91
92     foreach ($dependent_entities['delete'] as $entity) {
93       $entity_type_id = $entity->getEntityTypeId();
94       if (!isset($form['entity_deletes'][$entity_type_id])) {
95         $entity_type = $entity_manager->getDefinition($entity_type_id);
96         // Store the ID and label to sort the entity types and entities later.
97         $label = $entity_type->getLabel();
98         $entity_types[$entity_type_id] = $label;
99         $form['entity_deletes'][$entity_type_id] = [
100           '#theme' => 'item_list',
101           '#title' => $label,
102           '#items' => [],
103         ];
104       }
105       $form['entity_deletes'][$entity_type_id]['#items'][$entity->id()] = $entity->label() ?: $entity->id();
106     }
107     if (!empty($dependent_entities['delete'])) {
108       $form['entity_deletes']['#access'] = TRUE;
109
110       // Add a weight key to the entity type sections.
111       asort($entity_types, SORT_FLAG_CASE);
112       $weight = 0;
113       foreach ($entity_types as $entity_type_id => $label) {
114         if (isset($form['entity_deletes'][$entity_type_id])) {
115           $form['entity_deletes'][$entity_type_id]['#weight'] = $weight;
116           // Sort the list of entity labels alphabetically.
117           ksort($form['entity_deletes'][$entity_type_id]['#items'], SORT_FLAG_CASE);
118           $weight++;
119         }
120       }
121     }
122
123   }
124
125 }