Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / Entity / ContentUninstallValidator.php
1 <?php
2
3 namespace Drupal\Core\Entity;
4
5 use Drupal\Core\Extension\ModuleUninstallValidatorInterface;
6 use Drupal\Core\StringTranslation\StringTranslationTrait;
7 use Drupal\Core\StringTranslation\TranslationInterface;
8 use Drupal\Core\Url;
9
10 /**
11  * Validates module uninstall readiness based on existing content entities.
12  */
13 class ContentUninstallValidator implements ModuleUninstallValidatorInterface {
14   use StringTranslationTrait;
15
16   /**
17    * The entity manager.
18    *
19    * @var \Drupal\Core\Entity\EntityManagerInterface
20    */
21   protected $entityManager;
22
23   /**
24    * Constructs a new ContentUninstallValidator.
25    *
26    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
27    *   The entity manager.
28    * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
29    *   The string translation service.
30    */
31   public function __construct(EntityManagerInterface $entity_manager, TranslationInterface $string_translation) {
32     $this->entityManager = $entity_manager;
33     $this->stringTranslation = $string_translation;
34   }
35
36   /**
37    * {@inheritdoc}
38    */
39   public function validate($module) {
40     $entity_types = $this->entityManager->getDefinitions();
41     $reasons = [];
42     foreach ($entity_types as $entity_type) {
43       if ($module == $entity_type->getProvider() && $entity_type instanceof ContentEntityTypeInterface && $this->entityManager->getStorage($entity_type->id())->hasData()) {
44         $reasons[] = $this->t('There is content for the entity type: @entity_type. <a href=":url">Remove @entity_type_plural</a>.', [
45           '@entity_type' => $entity_type->getLabel(),
46           '@entity_type_plural' => $entity_type->getPluralLabel(),
47           ':url' => Url::fromRoute('system.prepare_modules_entity_uninstall', ['entity_type_id' => $entity_type->id()])->toString(),
48         ]);
49       }
50     }
51     return $reasons;
52   }
53
54 }