Security update for Core, with self-updated composer
[yaffs-website] / web / core / lib / Drupal / Core / Field / FieldModuleUninstallValidator.php
1 <?php
2
3 namespace Drupal\Core\Field;
4
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;
11
12 /**
13  * Validates module uninstall readiness based on defined storage definitions.
14  *
15  * @todo Remove this once we support field purging for base fields. See
16  *   https://www.drupal.org/node/2282119.
17  */
18 class FieldModuleUninstallValidator implements ModuleUninstallValidatorInterface {
19   use StringTranslationTrait;
20
21   /**
22    * Constructs the object.
23    *
24    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
25    *   The entity manager.
26    * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
27    *   The string translation service.
28    */
29   public function __construct(EntityManagerInterface $entity_manager, TranslationInterface $string_translation) {
30     $this->entityManager = $entity_manager;
31     $this->stringTranslation = $string_translation;
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public function validate($module_name) {
38     $reasons = [];
39
40     // We skip fields provided by the Field module as it implements field
41     // purging.
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(),
55                 ]);
56               }
57             }
58           }
59         }
60       }
61     }
62
63     return $reasons;
64   }
65
66 }