Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\field;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
|
Chris@0
|
6 use Drupal\Core\Extension\ModuleUninstallValidatorInterface;
|
Chris@0
|
7 use Drupal\Core\Field\FieldTypePluginManagerInterface;
|
Chris@0
|
8 use Drupal\Core\StringTranslation\StringTranslationTrait;
|
Chris@0
|
9 use Drupal\Core\StringTranslation\TranslationInterface;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Prevents uninstallation of modules providing active field storage.
|
Chris@0
|
13 */
|
Chris@0
|
14 class FieldUninstallValidator implements ModuleUninstallValidatorInterface {
|
Chris@0
|
15
|
Chris@0
|
16 use StringTranslationTrait;
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * The field storage config storage.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
|
Chris@0
|
22 */
|
Chris@0
|
23 protected $fieldStorageConfigStorage;
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * The field type plugin manager.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
|
Chris@0
|
29 */
|
Chris@0
|
30 protected $fieldTypeManager;
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * Constructs a new FieldUninstallValidator.
|
Chris@0
|
34 *
|
Chris@0
|
35 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
|
Chris@18
|
36 * The entity type manager.
|
Chris@0
|
37 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
|
Chris@0
|
38 * The string translation service.
|
Chris@0
|
39 * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
|
Chris@0
|
40 * The field type plugin manager.
|
Chris@0
|
41 */
|
Chris@0
|
42 public function __construct(EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation, FieldTypePluginManagerInterface $field_type_manager) {
|
Chris@0
|
43 $this->fieldStorageConfigStorage = $entity_type_manager->getStorage('field_storage_config');
|
Chris@0
|
44 $this->stringTranslation = $string_translation;
|
Chris@0
|
45 $this->fieldTypeManager = $field_type_manager;
|
Chris@0
|
46 }
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * {@inheritdoc}
|
Chris@0
|
50 */
|
Chris@0
|
51 public function validate($module) {
|
Chris@0
|
52 $reasons = [];
|
Chris@0
|
53 if ($field_storages = $this->getFieldStoragesByModule($module)) {
|
Chris@0
|
54 // Provide an explanation message (only mention pending deletions if there
|
Chris@0
|
55 // remain no actual, non-deleted fields.)
|
Chris@0
|
56 $fields_in_use = [];
|
Chris@0
|
57 foreach ($field_storages as $field_storage) {
|
Chris@0
|
58 if (!$field_storage->isDeleted()) {
|
Chris@0
|
59 $fields_in_use[$field_storage->getType()][] = $field_storage->getLabel();
|
Chris@0
|
60 }
|
Chris@0
|
61 }
|
Chris@0
|
62 if (!empty($fields_in_use)) {
|
Chris@0
|
63 foreach ($fields_in_use as $field_type => $field_storages) {
|
Chris@0
|
64 $field_type_label = $this->getFieldTypeLabel($field_type);
|
Chris@0
|
65 $reasons[] = $this->formatPlural(count($fields_in_use[$field_type]), 'The %field_type_label field type is used in the following field: @fields', 'The %field_type_label field type is used in the following fields: @fields', ['%field_type_label' => $field_type_label, '@fields' => implode(', ', $field_storages)]);
|
Chris@0
|
66 }
|
Chris@0
|
67 }
|
Chris@0
|
68 else {
|
Chris@0
|
69 $reasons[] = $this->t('Fields pending deletion');
|
Chris@0
|
70 }
|
Chris@0
|
71 }
|
Chris@0
|
72 return $reasons;
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 /**
|
Chris@0
|
76 * Returns all field storages for a specified module.
|
Chris@0
|
77 *
|
Chris@0
|
78 * @param string $module
|
Chris@0
|
79 * The module to filter field storages by.
|
Chris@0
|
80 *
|
Chris@0
|
81 * @return \Drupal\field\FieldStorageConfigInterface[]
|
Chris@0
|
82 * An array of field storages for a specified module.
|
Chris@0
|
83 */
|
Chris@0
|
84 protected function getFieldStoragesByModule($module) {
|
Chris@0
|
85 return $this->fieldStorageConfigStorage->loadByProperties(['module' => $module, 'include_deleted' => TRUE]);
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 /**
|
Chris@0
|
89 * Returns the label for a specified field type.
|
Chris@0
|
90 *
|
Chris@0
|
91 * @param string $field_type
|
Chris@0
|
92 * The field type.
|
Chris@0
|
93 *
|
Chris@0
|
94 * @return string
|
Chris@0
|
95 * The field type label.
|
Chris@0
|
96 */
|
Chris@0
|
97 protected function getFieldTypeLabel($field_type) {
|
Chris@0
|
98 return $this->fieldTypeManager->getDefinitions()[$field_type]['label'];
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 }
|