ContentUninstallValidator.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
  4. use Drupal\Core\Extension\ModuleUninstallValidatorInterface;
  5. use Drupal\Core\StringTranslation\StringTranslationTrait;
  6. use Drupal\Core\StringTranslation\TranslationInterface;
  7. use Drupal\Core\Url;
  8. /**
  9. * Validates module uninstall readiness based on existing content entities.
  10. */
  11. class ContentUninstallValidator implements ModuleUninstallValidatorInterface {
  12. use StringTranslationTrait;
  13. use DeprecatedServicePropertyTrait;
  14. /**
  15. * {@inheritdoc}
  16. */
  17. protected $deprecatedProperties = ['entityManager' => 'entity.manager'];
  18. /**
  19. * The entity type manager service.
  20. *
  21. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  22. */
  23. protected $entityTypeManager;
  24. /**
  25. * Constructs a new ContentUninstallValidator.
  26. *
  27. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  28. * The entity type manager service.
  29. * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
  30. * The string translation service.
  31. */
  32. public function __construct(EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation) {
  33. if ($entity_type_manager instanceof EntityManagerInterface) {
  34. @trigger_error('Passing the entity.manager service to ContentUninstallValidator::__construct() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Pass the new dependencies instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
  35. $this->entityTypeManager = \Drupal::entityTypeManager();
  36. }
  37. else {
  38. $this->entityTypeManager = $entity_type_manager;
  39. }
  40. $this->stringTranslation = $string_translation;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function validate($module) {
  46. $entity_types = $this->entityTypeManager->getDefinitions();
  47. $reasons = [];
  48. foreach ($entity_types as $entity_type) {
  49. if ($module == $entity_type->getProvider() && $entity_type instanceof ContentEntityTypeInterface && $this->entityTypeManager->getStorage($entity_type->id())->hasData()) {
  50. $reasons[] = $this->t('There is content for the entity type: @entity_type. <a href=":url">Remove @entity_type_plural</a>.', [
  51. '@entity_type' => $entity_type->getLabel(),
  52. '@entity_type_plural' => $entity_type->getPluralLabel(),
  53. ':url' => Url::fromRoute('system.prepare_modules_entity_uninstall', ['entity_type_id' => $entity_type->id()])->toString(),
  54. ]);
  55. }
  56. }
  57. return $reasons;
  58. }
  59. }