Upgraded imagemagick and manually altered pdf to image module to handle changes....
[yaffs-website] / web / modules / contrib / entity / src / BundlePlugin / BundlePluginUninstallValidator.php
1 <?php
2
3 namespace Drupal\entity\BundlePlugin;
4
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Extension\ModuleUninstallValidatorInterface;
7 use Drupal\Core\StringTranslation\StringTranslationTrait;
8 use Drupal\Core\StringTranslation\TranslationInterface;
9
10 /**
11  * Prevents uninstalling modules with bundle plugins in case of found data.
12  */
13 class BundlePluginUninstallValidator implements ModuleUninstallValidatorInterface {
14
15   use StringTranslationTrait;
16
17   /**
18    * The entity type manager.
19    *
20    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
21    */
22   protected $entityTypeManager;
23
24   /**
25    * Constructs the object.
26    *
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.
31    */
32   public function __construct(EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation) {
33     $this->entityTypeManager = $entity_type_manager;
34     $this->stringTranslation = $string_translation;
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public function validate($module) {
41     $reasons = [];
42
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();
47
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'];
51       });
52
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)
57             ->range(0, 1)
58             ->execute();
59           return !empty($result);
60         });
61
62         $bundles_with_content = array_intersect_key($bundles_filtered_by_module, array_flip($bundle_keys_with_content));
63   
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(),
68           ]);
69         }
70       }
71     }
72
73     return $reasons;
74   }
75
76 }