FilterUninstallValidator.php in Drupal 10
File
core/modules/filter/src/FilterUninstallValidator.php
View source
<?php
namespace Drupal\filter;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleUninstallValidatorInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
class FilterUninstallValidator implements ModuleUninstallValidatorInterface {
use StringTranslationTrait;
protected $filterManager;
protected $filterStorage;
public function __construct(PluginManagerInterface $filter_manager, EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation) {
$this->filterManager = $filter_manager;
$this->filterStorage = $entity_type_manager
->getStorage('filter_format');
$this->stringTranslation = $string_translation;
}
public function validate($module) {
$reasons = [];
if ($filter_plugins = $this
->getFilterDefinitionsByProvider($module)) {
$used_in = [];
foreach ($this
->getEnabledFilterFormats() as $filter_format) {
$filters = $filter_format
->filters();
foreach ($filter_plugins as $filter_plugin) {
if ($filters
->has($filter_plugin['id']) && $filters
->get($filter_plugin['id'])->status) {
$used_in[] = $filter_format
->label();
break;
}
}
}
if (!empty($used_in)) {
$reasons[] = $this
->t('Provides a filter plugin that is in use in the following filter formats: %formats', [
'%formats' => implode(', ', $used_in),
]);
}
}
return $reasons;
}
protected function getFilterDefinitionsByProvider($provider) {
return array_filter($this->filterManager
->getDefinitions(), function ($definition) use ($provider) {
return $definition['provider'] == $provider;
});
}
protected function getEnabledFilterFormats() {
return $this->filterStorage
->loadByProperties([
'status' => TRUE,
]);
}
}