Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / service / uninstall-validator.twig
1 <?php
2
3 namespace Drupal\{{ machine_name }};
4
5 use Drupal\Component\Plugin\PluginManagerInterface;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Drupal\Core\Extension\ModuleUninstallValidatorInterface;
8 use Drupal\Core\StringTranslation\StringTranslationTrait;
9 use Drupal\Core\StringTranslation\TranslationInterface;
10
11 /**
12  * Prevents uninstalling of modules providing used block plugins.
13  */
14 class {{ class }} implements ModuleUninstallValidatorInterface {
15
16   use StringTranslationTrait;
17
18   /**
19    * The block plugin manager.
20    *
21    * @var \Drupal\Component\Plugin\PluginManagerInterface
22    */
23   protected $blockManager;
24
25   /**
26    * The block entity storage.
27    *
28    * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
29    */
30   protected $blockStorage;
31
32   /**
33    * Constructs a new FilterUninstallValidator.
34    *
35    * @param \Drupal\Component\Plugin\PluginManagerInterface $block_manager
36    *   The filter plugin manager.
37    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
38    *   The entity manager.
39    * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
40    *   The string translation service.
41    */
42   public function __construct(PluginManagerInterface $block_manager, EntityTypeManagerInterface $entity_manager, TranslationInterface $string_translation) {
43     $this->blockManager = $block_manager;
44     $this->blockStorage = $entity_manager->getStorage('block');
45     $this->stringTranslation = $string_translation;
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public function validate($module) {
52     $reasons = [];
53
54     foreach ($this->blockStorage->loadMultiple() as $block) {
55       /** @var \Drupal\block\BlockInterface $block */
56       $definition = $block->getPlugin()
57         ->getPluginDefinition();
58       if ($definition['provider'] == $module) {
59         $message_arguments = [
60           ':url' => $block->toUrl('edit-form')->toString(),
61           '@block_id' => $block->id(),
62         ];
63         $reasons[] = $this->t('Provides a block plugin that is in use in the following block: <a href=":url">@block_id</a>', $message_arguments);
64       }
65     }
66
67     return $reasons;
68   }
69
70 }