ContentUninstallValidator.php 1.8 KB

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