ConfigDependencyDeleteFormTrait.php 4.7 KB

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