3 namespace Drupal\{{ machine_name }};
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;
12 * Prevents uninstalling of modules providing used block plugins.
14 class {{ class }} implements ModuleUninstallValidatorInterface {
16 use StringTranslationTrait;
19 * The block plugin manager.
21 * @var \Drupal\Component\Plugin\PluginManagerInterface
23 protected $blockManager;
26 * The block entity storage.
28 * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
30 protected $blockStorage;
33 * Constructs a new FilterUninstallValidator.
35 * @param \Drupal\Component\Plugin\PluginManagerInterface $block_manager
36 * The filter plugin manager.
37 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
39 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
40 * The string translation service.
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;
51 public function validate($module) {
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(),
63 $reasons[] = $this->t('Provides a block plugin that is in use in the following block: <a href=":url">@block_id</a>', $message_arguments);