3 namespace Drupal\Core\Field;
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\Entity\FieldableEntityInterface;
7 use Drupal\Core\Entity\FieldableEntityStorageInterface;
8 use Drupal\Core\Extension\ModuleUninstallValidatorInterface;
9 use Drupal\Core\StringTranslation\StringTranslationTrait;
10 use Drupal\Core\StringTranslation\TranslationInterface;
13 * Validates module uninstall readiness based on defined storage definitions.
15 * @todo Remove this once we support field purging for base fields. See
16 * https://www.drupal.org/node/2282119.
18 class FieldModuleUninstallValidator implements ModuleUninstallValidatorInterface {
19 use StringTranslationTrait;
22 * Constructs the object.
24 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
26 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
27 * The string translation service.
29 public function __construct(EntityManagerInterface $entity_manager, TranslationInterface $string_translation) {
30 $this->entityManager = $entity_manager;
31 $this->stringTranslation = $string_translation;
37 public function validate($module_name) {
40 // We skip fields provided by the Field module as it implements field
42 if ($module_name != 'field') {
43 foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
44 // We skip entity types defined by the module as there must be no
45 // content to be able to uninstall them anyway.
46 // See \Drupal\Core\Entity\ContentUninstallValidator.
47 if ($entity_type->getProvider() != $module_name && $entity_type->entityClassImplements(FieldableEntityInterface::class)) {
48 foreach ($this->entityManager->getFieldStorageDefinitions($entity_type_id) as $storage_definition) {
49 if ($storage_definition->getProvider() == $module_name) {
50 $storage = $this->entityManager->getStorage($entity_type_id);
51 if ($storage instanceof FieldableEntityStorageInterface && $storage->countFieldData($storage_definition, TRUE)) {
52 $reasons[] = $this->t('There is data for the field @field-name on entity type @entity_type', [
53 '@field-name' => $storage_definition->getName(),
54 '@entity_type' => $entity_type->getLabel(),