3 namespace Drupal\field;
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Extension\ModuleUninstallValidatorInterface;
7 use Drupal\Core\Field\FieldTypePluginManagerInterface;
8 use Drupal\Core\StringTranslation\StringTranslationTrait;
9 use Drupal\Core\StringTranslation\TranslationInterface;
12 * Prevents uninstallation of modules providing active field storage.
14 class FieldUninstallValidator implements ModuleUninstallValidatorInterface {
16 use StringTranslationTrait;
19 * The field storage config storage.
21 * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
23 protected $fieldStorageConfigStorage;
26 * The field type plugin manager.
28 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
30 protected $fieldTypeManager;
33 * Constructs a new FieldUninstallValidator.
35 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
37 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
38 * The string translation service.
39 * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
40 * The field type plugin manager.
42 public function __construct(EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation, FieldTypePluginManagerInterface $field_type_manager) {
43 $this->fieldStorageConfigStorage = $entity_type_manager->getStorage('field_storage_config');
44 $this->stringTranslation = $string_translation;
45 $this->fieldTypeManager = $field_type_manager;
51 public function validate($module) {
53 if ($field_storages = $this->getFieldStoragesByModule($module)) {
54 // Provide an explanation message (only mention pending deletions if there
55 // remain no actual, non-deleted fields.)
57 foreach ($field_storages as $field_storage) {
58 if (!$field_storage->isDeleted()) {
59 $fields_in_use[$field_storage->getType()][] = $field_storage->getLabel();
62 if (!empty($fields_in_use)) {
63 foreach ($fields_in_use as $field_type => $field_storages) {
64 $field_type_label = $this->getFieldTypeLabel($field_type);
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)]);
69 $reasons[] = $this->t('Fields pending deletion');
76 * Returns all field storages for a specified module.
78 * @param string $module
79 * The module to filter field storages by.
81 * @return \Drupal\field\FieldStorageConfigInterface[]
82 * An array of field storages for a specified module.
84 protected function getFieldStoragesByModule($module) {
85 return $this->fieldStorageConfigStorage->loadByProperties(['module' => $module, 'include_deleted' => TRUE]);
89 * Returns the label for a specified field type.
91 * @param string $field_type
95 * The field type label.
97 protected function getFieldTypeLabel($field_type) {
98 return $this->fieldTypeManager->getDefinitions()[$field_type]['label'];