Security update to Drupal 8.4.6
[yaffs-website] / web / core / modules / filter / src / FilterUninstallValidator.php
1 <?php
2
3 namespace Drupal\filter;
4
5 use Drupal\Component\Plugin\PluginManagerInterface;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Extension\ModuleUninstallValidatorInterface;
8 use Drupal\Core\StringTranslation\StringTranslationTrait;
9 use Drupal\Core\StringTranslation\TranslationInterface;
10
11 /**
12  * Prevents uninstallation of modules providing used filter plugins.
13  */
14 class FilterUninstallValidator implements ModuleUninstallValidatorInterface {
15
16   use StringTranslationTrait;
17
18   /**
19    * The filter plugin manager.
20    *
21    * @var \Drupal\Component\Plugin\PluginManagerInterface
22    */
23   protected $filterManager;
24
25   /**
26    * The filter entity storage.
27    *
28    * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
29    */
30   protected $filterStorage;
31
32   /**
33    * Constructs a new FilterUninstallValidator.
34    *
35    * @param \Drupal\Component\Plugin\PluginManagerInterface $filter_manager
36    *   The filter plugin manager.
37    * @param \Drupal\Core\Entity\EntityManagerInterface $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 $filter_manager, EntityManagerInterface $entity_manager, TranslationInterface $string_translation) {
43     $this->filterManager = $filter_manager;
44     $this->filterStorage = $entity_manager->getStorage('filter_format');
45     $this->stringTranslation = $string_translation;
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public function validate($module) {
52     $reasons = [];
53     // Get filter plugins supplied by this module.
54     if ($filter_plugins = $this->getFilterDefinitionsByProvider($module)) {
55       $used_in = [];
56       // Find out if any filter formats have the plugin enabled.
57       foreach ($this->getEnabledFilterFormats() as $filter_format) {
58         $filters = $filter_format->filters();
59         foreach ($filter_plugins as $filter_plugin) {
60           if ($filters->has($filter_plugin['id']) && $filters->get($filter_plugin['id'])->status) {
61             $used_in[] = $filter_format->label();
62             break;
63           }
64         }
65       }
66       if (!empty($used_in)) {
67         $reasons[] = $this->t('Provides a filter plugin that is in use in the following filter formats: %formats', ['%formats' => implode(', ', $used_in)]);
68       }
69     }
70     return $reasons;
71   }
72
73   /**
74    * Returns all filter definitions that are provided by the specified provider.
75    *
76    * @param string $provider
77    *   The provider of the filters.
78    *
79    * @return array
80    *   The filter definitions for the specified provider.
81    */
82   protected function getFilterDefinitionsByProvider($provider) {
83     return array_filter($this->filterManager->getDefinitions(), function ($definition) use ($provider) {
84       return $definition['provider'] == $provider;
85     });
86   }
87
88   /**
89    * Returns all enabled filter formats.
90    *
91    * @return \Drupal\filter\FilterFormatInterface[]
92    */
93   protected function getEnabledFilterFormats() {
94     return $this->filterStorage->loadByProperties(['status' => TRUE]);
95   }
96
97 }