3 namespace Drupal\Core\Config\Entity;
5 use Drupal\Core\Config\ConfigManagerInterface;
6 use Drupal\Core\Entity\EntityManagerInterface;
9 * Lists affected configuration entities by a dependency removal.
11 * This trait relies on the StringTranslationTrait.
13 trait ConfigDependencyDeleteFormTrait {
16 * Translates a string to the current language or to a given language.
18 * Provided by \Drupal\Core\StringTranslation\StringTranslationTrait.
20 abstract protected function t($string, array $args = [], array $options = []);
23 * Adds form elements to list affected configuration entities.
26 * The form array to add elements to.
28 * The type of dependency being checked. Either 'module', 'theme', 'config'
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
36 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
39 * @see \Drupal\Core\Config\ConfigManagerInterface::getConfigEntitiesToChangeOnDependencyRemoval()
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);
46 $form['entity_updates'] = [
48 '#title' => $this->t('Configuration updates'),
49 '#description' => $this->t('The listed configuration will be updated.'),
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',
68 $form['entity_updates'][$entity_type_id]['#items'][$entity->id()] = $entity->label() ?: $entity->id();
70 if (!empty($dependent_entities['update'])) {
71 $form['entity_updates']['#access'] = TRUE;
73 // Add a weight key to the entity type sections.
74 asort($entity_types, SORT_FLAG_CASE);
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);
84 $form['entity_deletes'] = [
86 '#title' => $this->t('Configuration deletions'),
87 '#description' => $this->t('The listed configuration will be deleted.'),
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',
105 $form['entity_deletes'][$entity_type_id]['#items'][$entity->id()] = $entity->label() ?: $entity->id();
107 if (!empty($dependent_entities['delete'])) {
108 $form['entity_deletes']['#access'] = TRUE;
110 // Add a weight key to the entity type sections.
111 asort($entity_types, SORT_FLAG_CASE);
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);