3 namespace Drupal\Core\Entity;
5 use Drupal\Core\Extension\ModuleUninstallValidatorInterface;
6 use Drupal\Core\StringTranslation\StringTranslationTrait;
7 use Drupal\Core\StringTranslation\TranslationInterface;
11 * Validates module uninstall readiness based on existing content entities.
13 class ContentUninstallValidator implements ModuleUninstallValidatorInterface {
14 use StringTranslationTrait;
19 * @var \Drupal\Core\Entity\EntityManagerInterface
21 protected $entityManager;
24 * Constructs a new ContentUninstallValidator.
26 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
28 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
29 * The string translation service.
31 public function __construct(EntityManagerInterface $entity_manager, TranslationInterface $string_translation) {
32 $this->entityManager = $entity_manager;
33 $this->stringTranslation = $string_translation;
39 public function validate($module) {
40 $entity_types = $this->entityManager->getDefinitions();
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(),