class FilterPluginCollection

Same name in other branches
  1. 9 core/modules/filter/src/FilterPluginCollection.php \Drupal\filter\FilterPluginCollection
  2. 10 core/modules/filter/src/FilterPluginCollection.php \Drupal\filter\FilterPluginCollection
  3. 11.x core/modules/filter/src/FilterPluginCollection.php \Drupal\filter\FilterPluginCollection

A collection of filters.

Hierarchy

Expanded class hierarchy of FilterPluginCollection

5 files declare their use of FilterPluginCollection
EditorFileReferenceFilterTest.php in core/modules/editor/tests/src/Kernel/EditorFileReferenceFilterTest.php
FilterCaptionTwigDebugTest.php in core/modules/filter/tests/src/Kernel/FilterCaptionTwigDebugTest.php
FilterFormat.php in core/modules/filter/src/Entity/FilterFormat.php
FilterKernelTest.php in core/modules/filter/tests/src/Kernel/FilterKernelTest.php
MediaEmbedFilterTestBase.php in core/modules/media/tests/src/Kernel/MediaEmbedFilterTestBase.php

File

core/modules/filter/src/FilterPluginCollection.php, line 11

Namespace

Drupal\filter
View source
class FilterPluginCollection extends DefaultLazyPluginCollection {
    
    /**
     * All possible filter plugin IDs.
     *
     * @var array
     */
    protected $definitions;
    
    /**
     * {@inheritdoc}
     *
     * @return \Drupal\filter\Plugin\FilterInterface
     */
    public function &get($instance_id) {
        return parent::get($instance_id);
    }
    
    /**
     * Retrieves filter definitions and creates an instance for each filter.
     *
     * This is exclusively used for the text format administration page, on which
     * all available filter plugins are exposed, regardless of whether the current
     * text format has an active instance.
     *
     * @todo Refactor text format administration to actually construct/create and
     *   destruct/remove actual filter plugin instances, using a library approach
     *   à la blocks.
     */
    public function getAll() {
        // Retrieve all available filter plugin definitions.
        if (!$this->definitions) {
            $this->definitions = $this->manager
                ->getDefinitions();
            // Do not allow the null filter to be used directly, only as a fallback.
            unset($this->definitions['filter_null']);
        }
        // Ensure that there is an instance of all available filters.
        // Note that getDefinitions() are keyed by $plugin_id. $instance_id is the
        // $plugin_id for filters, since a single filter plugin can only exist once
        // in a format.
        foreach ($this->definitions as $plugin_id => $definition) {
            if (!isset($this->pluginInstances[$plugin_id])) {
                $this->initializePlugin($plugin_id);
            }
        }
        return $this->pluginInstances;
    }
    
    /**
     * {@inheritdoc}
     */
    protected function initializePlugin($instance_id) {
        // Filters have a 1:1 relationship to text formats and can be added and
        // instantiated at any time.
        // @todo $configuration is the whole filter plugin instance configuration,
        //   as contained in the text format configuration. The default
        //   configuration is the filter plugin definition. Configuration should not
        //   be contained in definitions. Move into a FilterBase::init() method.
        $configuration = $this->manager
            ->getDefinition($instance_id);
        // Merge the actual configuration into the default configuration.
        if (isset($this->configurations[$instance_id])) {
            $configuration = NestedArray::mergeDeep($configuration, $this->configurations[$instance_id]);
        }
        $this->configurations[$instance_id] = $configuration;
        parent::initializePlugin($instance_id);
    }
    
    /**
     * {@inheritdoc}
     */
    public function sort() {
        $this->getAll();
        return parent::sort();
    }
    
    /**
     * {@inheritdoc}
     */
    public function sortHelper($aID, $bID) {
        $a = $this->get($aID);
        $b = $this->get($bID);
        if ($a->status != $b->status) {
            return !empty($a->status) ? -1 : 1;
        }
        if ($a->weight != $b->weight) {
            return $a->weight < $b->weight ? -1 : 1;
        }
        if ($a->provider != $b->provider) {
            return strnatcasecmp($a->provider, $b->provider);
        }
        return parent::sortHelper($aID, $bID);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getConfiguration() {
        $configuration = parent::getConfiguration();
        // Remove configuration if it matches the defaults. In self::getAll(), we
        // load all available filters, in addition to the enabled filters stored in
        // configuration. In order to prevent those from bleeding through to the
        // stored configuration, remove all filters that match the default values.
        // Because filters are disabled by default, this will never remove the
        // configuration of an enabled filter.
        foreach ($configuration as $instance_id => $instance_config) {
            $default_config = [];
            $default_config['id'] = $instance_id;
            $default_config += $this->get($instance_id)
                ->defaultConfiguration();
            if ($default_config === $instance_config) {
                unset($configuration[$instance_id]);
            }
        }
        return $configuration;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DefaultLazyPluginCollection::$configurations protected property The initial configuration for each plugin in the collection.
DefaultLazyPluginCollection::$manager protected property The manager used to instantiate the plugins.
DefaultLazyPluginCollection::$originalOrder protected property The original order of the instances.
DefaultLazyPluginCollection::$pluginKey protected property The key within the plugin configuration that contains the plugin ID. 3
DefaultLazyPluginCollection::addInstanceId public function Adds an instance ID to the available instance IDs. Overrides LazyPluginCollection::addInstanceId
DefaultLazyPluginCollection::removeInstanceId public function Removes an instance ID. Overrides LazyPluginCollection::removeInstanceId
DefaultLazyPluginCollection::setConfiguration public function Sets the configuration for all plugins in this collection. Overrides LazyPluginCollection::setConfiguration
DefaultLazyPluginCollection::setInstanceConfiguration public function Updates the configuration for a plugin instance.
DefaultLazyPluginCollection::__construct public function Constructs a new DefaultLazyPluginCollection object. 1
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FilterPluginCollection::$definitions protected property All possible filter plugin IDs.
FilterPluginCollection::get public function Overrides LazyPluginCollection::get
FilterPluginCollection::getAll public function Retrieves filter definitions and creates an instance for each filter.
FilterPluginCollection::getConfiguration public function Gets the current configuration of all plugins in this collection. Overrides DefaultLazyPluginCollection::getConfiguration
FilterPluginCollection::initializePlugin protected function Initializes and stores a plugin. Overrides DefaultLazyPluginCollection::initializePlugin
FilterPluginCollection::sort public function Sorts all plugin instances in this collection. Overrides DefaultLazyPluginCollection::sort
FilterPluginCollection::sortHelper public function Provides uasort() callback to sort plugins. Overrides DefaultLazyPluginCollection::sortHelper
LazyPluginCollection::$instanceIds protected property Stores the IDs of all potential plugin instances.
LazyPluginCollection::$pluginInstances protected property Stores all instantiated plugins.
LazyPluginCollection::clear public function Clears all instantiated plugins. 1
LazyPluginCollection::count public function
LazyPluginCollection::getInstanceIds public function Gets all instance IDs.
LazyPluginCollection::getIterator public function
LazyPluginCollection::has public function Determines if a plugin instance exists.
LazyPluginCollection::remove public function Removes an initialized plugin. 1
LazyPluginCollection::set public function Stores an initialized plugin.

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