3 namespace Drupal\entity\BundlePlugin;
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Extension\ModuleUninstallValidatorInterface;
7 use Drupal\Core\StringTranslation\StringTranslationTrait;
8 use Drupal\Core\StringTranslation\TranslationInterface;
11 * Prevents uninstalling modules with bundle plugins in case of found data.
13 class BundlePluginUninstallValidator implements ModuleUninstallValidatorInterface {
15 use StringTranslationTrait;
18 * The entity type manager.
20 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
22 protected $entityTypeManager;
25 * Constructs the object.
27 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
28 * The entity type manager.
29 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
30 * The string translation service.
32 public function __construct(EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation) {
33 $this->entityTypeManager = $entity_type_manager;
34 $this->stringTranslation = $string_translation;
40 public function validate($module) {
43 foreach (entity_get_bundle_plugin_entity_types() as $entity_type) {
44 /** @var \Drupal\entity\BundlePlugin\BundlePluginHandler $bundle_handler */
45 $bundle_handler = $this->entityTypeManager->getHandler($entity_type->id(), 'bundle_plugin');
46 $bundles = $bundle_handler->getBundleInfo();
48 // We find all bundles which have to be removed due to the uninstallation.
49 $bundles_filtered_by_module = array_filter($bundles, function ($bundle_info) use ($module) {
50 return $module === $bundle_info['provider'];
53 if (!empty($bundles_filtered_by_module)) {
54 $bundle_keys_with_content = array_filter(array_keys($bundles_filtered_by_module), function ($bundle) use ($entity_type) {
55 $result = $this->entityTypeManager->getStorage($entity_type->id())->getQuery()
56 ->condition($entity_type->getKey('bundle'), $bundle)
59 return !empty($result);
62 $bundles_with_content = array_intersect_key($bundles_filtered_by_module, array_flip($bundle_keys_with_content));
64 foreach ($bundles_with_content as $bundle) {
65 $reasons[] = $this->t('There is data for the bundle @bundle on the entity type @entity_type. Please remove all content before uninstalling the module.', [
66 '@bundle' => $bundle['label'],
67 '@entity_type' => $entity_type->getLabel(),