class PluginManagerBase

Same name in other branches
  1. 9 core/lib/Drupal/Component/Plugin/PluginManagerBase.php \Drupal\Component\Plugin\PluginManagerBase
  2. 8.9.x core/lib/Drupal/Component/Plugin/PluginManagerBase.php \Drupal\Component\Plugin\PluginManagerBase
  3. 11.x core/lib/Drupal/Component/Plugin/PluginManagerBase.php \Drupal\Component\Plugin\PluginManagerBase

Base class for plugin managers.

Hierarchy

Expanded class hierarchy of PluginManagerBase

10 files declare their use of PluginManagerBase
DefaultPluginManager.php in core/lib/Drupal/Core/Plugin/DefaultPluginManager.php
EntityDisplayFormBase.php in core/modules/field_ui/src/Form/EntityDisplayFormBase.php
FieldLayoutEntityFormDisplayEditForm.php in core/modules/field_layout/src/Form/FieldLayoutEntityFormDisplayEditForm.php
FieldLayoutEntityViewDisplayEditForm.php in core/modules/field_layout/src/Form/FieldLayoutEntityViewDisplayEditForm.php
FilteredPluginManagerTraitTest.php in core/tests/Drupal/Tests/Core/Plugin/FilteredPluginManagerTraitTest.php

... See full list

File

core/lib/Drupal/Component/Plugin/PluginManagerBase.php, line 11

Namespace

Drupal\Component\Plugin
View source
abstract class PluginManagerBase implements PluginManagerInterface {
    use DiscoveryTrait;
    
    /**
     * The object that discovers plugins managed by this manager.
     *
     * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface
     */
    protected $discovery;
    
    /**
     * The object that instantiates plugins managed by this manager.
     *
     * @var \Drupal\Component\Plugin\Factory\FactoryInterface
     */
    protected $factory;
    
    /**
     * The object that returns the preconfigured plugin instance appropriate for a particular runtime condition.
     *
     * @var \Drupal\Component\Plugin\Mapper\MapperInterface|null
     */
    protected $mapper;
    
    /**
     * Gets the plugin discovery.
     *
     * @return \Drupal\Component\Plugin\Discovery\DiscoveryInterface
     */
    protected function getDiscovery() {
        return $this->discovery;
    }
    
    /**
     * Gets the plugin factory.
     *
     * @return \Drupal\Component\Plugin\Factory\FactoryInterface
     */
    protected function getFactory() {
        return $this->factory;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getDefinition($plugin_id, $exception_on_invalid = TRUE) {
        return $this->getDiscovery()
            ->getDefinition($plugin_id, $exception_on_invalid);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getDefinitions() {
        return $this->getDiscovery()
            ->getDefinitions();
    }
    
    /**
     * {@inheritdoc}
     */
    public function createInstance($plugin_id, array $configuration = []) {
        // If this PluginManager has fallback capabilities catch
        // PluginNotFoundExceptions.
        if ($this instanceof FallbackPluginManagerInterface) {
            try {
                return $this->getFactory()
                    ->createInstance($plugin_id, $configuration);
            } catch (PluginNotFoundException $e) {
                return $this->handlePluginNotFound($plugin_id, $configuration);
            }
        }
        else {
            return $this->getFactory()
                ->createInstance($plugin_id, $configuration);
        }
    }
    
    /**
     * Allows plugin managers to specify custom behavior if a plugin is not found.
     *
     * @param string $plugin_id
     *   The ID of the missing requested plugin.
     * @param array $configuration
     *   An array of configuration relevant to the plugin instance.
     *
     * @return object
     *   A fallback plugin instance.
     *
     * @throws \BadMethodCallException
     *   When ::getFallbackPluginId() is not implemented in the concrete plugin
     *   manager class.
     */
    protected function handlePluginNotFound($plugin_id, array $configuration) {
        $fallback_id = $this->getFallbackPluginId($plugin_id, $configuration);
        return $this->getFactory()
            ->createInstance($fallback_id, $configuration);
    }
    
    /**
     * Gets a fallback id for a missing plugin.
     *
     * This method should be implemented in extending classes that also implement
     * FallbackPluginManagerInterface. It is called by
     * PluginManagerBase::handlePluginNotFound on the abstract class, and
     * therefore should be defined as well on the abstract class to prevent static
     * analysis errors.
     *
     * @param string $plugin_id
     *   The ID of the missing requested plugin.
     * @param array $configuration
     *   An array of configuration relevant to the plugin instance.
     *
     * @return string
     *   The id of an existing plugin to use when the plugin does not exist.
     *
     * @throws \BadMethodCallException
     *   If the method is not implemented in the concrete plugin manager class.
     */
    protected function getFallbackPluginId($plugin_id, array $configuration = []) {
        throw new \BadMethodCallException(static::class . '::getFallbackPluginId() not implemented.');
    }
    
    /**
     * {@inheritdoc}
     */
    public function getInstance(array $options) {
        if (!$this->mapper) {
            throw new \BadMethodCallException(sprintf('%s does not support this method unless %s::$mapper is set.', static::class, static::class));
        }
        return $this->mapper
            ->getInstance($options);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DiscoveryTrait::doGetDefinition protected function Gets a specific plugin definition.
DiscoveryTrait::hasDefinition public function
PluginManagerBase::$discovery protected property The object that discovers plugins managed by this manager.
PluginManagerBase::$factory protected property The object that instantiates plugins managed by this manager.
PluginManagerBase::$mapper protected property The object that returns the preconfigured plugin instance appropriate for a particular runtime condition.
PluginManagerBase::createInstance public function 15
PluginManagerBase::getDefinition public function Overrides DiscoveryTrait::getDefinition
PluginManagerBase::getDefinitions public function Overrides DiscoveryTrait::getDefinitions
PluginManagerBase::getDiscovery protected function Gets the plugin discovery. 1
PluginManagerBase::getFactory protected function Gets the plugin factory. 1
PluginManagerBase::getFallbackPluginId protected function Gets a fallback id for a missing plugin. 6
PluginManagerBase::getInstance public function 6
PluginManagerBase::handlePluginNotFound protected function Allows plugin managers to specify custom behavior if a plugin is not found. 1

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