annotate vendor/chi-teck/drupal-code-generator/templates/d8/service/uninstall-validator.twig @ 5:12f9dff5fda9 tip

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