class FilterUninstallValidator

Same name in this branch
  1. 8.9.x core/modules/filter/src/ProxyClass/FilterUninstallValidator.php \Drupal\filter\ProxyClass\FilterUninstallValidator
Same name in other branches
  1. 9 core/modules/filter/src/ProxyClass/FilterUninstallValidator.php \Drupal\filter\ProxyClass\FilterUninstallValidator
  2. 9 core/modules/filter/src/FilterUninstallValidator.php \Drupal\filter\FilterUninstallValidator
  3. 10 core/modules/filter/src/ProxyClass/FilterUninstallValidator.php \Drupal\filter\ProxyClass\FilterUninstallValidator
  4. 10 core/modules/filter/src/FilterUninstallValidator.php \Drupal\filter\FilterUninstallValidator
  5. 11.x core/modules/filter/src/ProxyClass/FilterUninstallValidator.php \Drupal\filter\ProxyClass\FilterUninstallValidator
  6. 11.x core/modules/filter/src/FilterUninstallValidator.php \Drupal\filter\FilterUninstallValidator

Prevents uninstallation of modules providing used filter plugins.

Hierarchy

Expanded class hierarchy of FilterUninstallValidator

1 string reference to 'FilterUninstallValidator'
filter.services.yml in core/modules/filter/filter.services.yml
core/modules/filter/filter.services.yml
1 service uses FilterUninstallValidator
filter.uninstall_validator in core/modules/filter/filter.services.yml
Drupal\filter\FilterUninstallValidator

File

core/modules/filter/src/FilterUninstallValidator.php, line 14

Namespace

Drupal\filter
View source
class FilterUninstallValidator implements ModuleUninstallValidatorInterface {
    use StringTranslationTrait;
    
    /**
     * The filter plugin manager.
     *
     * @var \Drupal\Component\Plugin\PluginManagerInterface
     */
    protected $filterManager;
    
    /**
     * The filter entity storage.
     *
     * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
     */
    protected $filterStorage;
    
    /**
     * Constructs a new FilterUninstallValidator.
     *
     * @param \Drupal\Component\Plugin\PluginManagerInterface $filter_manager
     *   The filter plugin manager.
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager.
     * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
     *   The string translation service.
     */
    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;
    }
    
    /**
     * {@inheritdoc}
     */
    public function validate($module) {
        $reasons = [];
        // Get filter plugins supplied by this module.
        if ($filter_plugins = $this->getFilterDefinitionsByProvider($module)) {
            $used_in = [];
            // Find out if any filter formats have the plugin enabled.
            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;
    }
    
    /**
     * Returns all filter definitions that are provided by the specified provider.
     *
     * @param string $provider
     *   The provider of the filters.
     *
     * @return array
     *   The filter definitions for the specified provider.
     */
    protected function getFilterDefinitionsByProvider($provider) {
        return array_filter($this->filterManager
            ->getDefinitions(), function ($definition) use ($provider) {
            return $definition['provider'] == $provider;
        });
    }
    
    /**
     * Returns all enabled filter formats.
     *
     * @return \Drupal\filter\FilterFormatInterface[]
     */
    protected function getEnabledFilterFormats() {
        return $this->filterStorage
            ->loadByProperties([
            'status' => TRUE,
        ]);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
FilterUninstallValidator::$filterManager protected property The filter plugin manager.
FilterUninstallValidator::$filterStorage protected property The filter entity storage.
FilterUninstallValidator::getEnabledFilterFormats protected function Returns all enabled filter formats.
FilterUninstallValidator::getFilterDefinitionsByProvider protected function Returns all filter definitions that are provided by the specified provider.
FilterUninstallValidator::validate public function Determines the reasons a module can not be uninstalled. Overrides ModuleUninstallValidatorInterface::validate
FilterUninstallValidator::__construct public function Constructs a new FilterUninstallValidator.
StringTranslationTrait::$stringTranslation protected property The string translation service.
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.