class DefaultFactory

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

Default plugin factory.

Instantiates plugin instances by passing the full configuration array as a single constructor argument. Plugin types wanting to support plugin classes with more flexible constructor signatures can do so by using an alternate factory such as Drupal\Component\Plugin\Factory\ReflectionFactory.

Hierarchy

Expanded class hierarchy of DefaultFactory

14 files declare their use of DefaultFactory
ArchiverManager.php in core/lib/Drupal/Core/Archiver/ArchiverManager.php
ContainerFactory.php in core/lib/Drupal/Core/Plugin/Factory/ContainerFactory.php
DefaultFactoryTest.php in core/tests/Drupal/Tests/Component/Plugin/DefaultFactoryTest.php
DefaultsTestPluginManager.php in core/modules/system/tests/modules/plugin_test/src/Plugin/DefaultsTestPluginManager.php
EntityDisplayFormBase.php in core/modules/field_ui/src/Form/EntityDisplayFormBase.php

... See full list

File

core/lib/Drupal/Component/Plugin/Factory/DefaultFactory.php, line 17

Namespace

Drupal\Component\Plugin\Factory
View source
class DefaultFactory implements FactoryInterface {
    
    /**
     * The object that retrieves the definitions of the plugins that this factory instantiates.
     *
     * The plugin definition includes the plugin class and possibly other
     * information necessary for proper instantiation.
     *
     * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface
     */
    protected $discovery;
    
    /**
     * Defines an interface each plugin should implement.
     *
     * @var string|null
     */
    protected $interface;
    
    /**
     * Constructs a Drupal\Component\Plugin\Factory\DefaultFactory object.
     *
     * @param \Drupal\Component\Plugin\Discovery\DiscoveryInterface $discovery
     *   The plugin discovery.
     * @param string|null $plugin_interface
     *   (optional) The interface each plugin should implement.
     */
    public function __construct(DiscoveryInterface $discovery, $plugin_interface = NULL) {
        $this->discovery = $discovery;
        $this->interface = $plugin_interface;
    }
    
    /**
     * {@inheritdoc}
     */
    public function createInstance($plugin_id, array $configuration = []) {
        $plugin_definition = $this->discovery
            ->getDefinition($plugin_id);
        $plugin_class = static::getPluginClass($plugin_id, $plugin_definition, $this->interface);
        return new $plugin_class($configuration, $plugin_id, $plugin_definition);
    }
    
    /**
     * Finds the class relevant for a given plugin.
     *
     * @param string $plugin_id
     *   The id of a plugin.
     * @param \Drupal\Component\Plugin\Definition\PluginDefinitionInterface|mixed[] $plugin_definition
     *   The plugin definition associated with the plugin ID.
     * @param string $required_interface
     *   (optional) The required plugin interface.
     *
     * @return string
     *   The appropriate class name.
     *
     * @throws \Drupal\Component\Plugin\Exception\PluginException
     *   Thrown when there is no class specified, the class doesn't exist, or
     *   the class does not implement the specified required interface.
     */
    public static function getPluginClass($plugin_id, $plugin_definition = NULL, $required_interface = NULL) {
        $missing_class_message = sprintf('The plugin (%s) did not specify an instance class.', $plugin_id);
        if (is_array($plugin_definition)) {
            if (empty($plugin_definition['class'])) {
                throw new PluginException($missing_class_message);
            }
            $class = $plugin_definition['class'];
        }
        elseif ($plugin_definition instanceof PluginDefinitionInterface) {
            if (!$plugin_definition->getClass()) {
                throw new PluginException($missing_class_message);
            }
            $class = $plugin_definition->getClass();
        }
        else {
            $plugin_definition_type = is_object($plugin_definition) ? get_class($plugin_definition) : gettype($plugin_definition);
            throw new PluginException(sprintf('%s can only handle plugin definitions that are arrays or that implement %s, but %s given.', __CLASS__, PluginDefinitionInterface::class, $plugin_definition_type));
        }
        if (!class_exists($class)) {
            throw new PluginException(sprintf('Plugin (%s) instance class "%s" does not exist.', $plugin_id, $class));
        }
        if ($required_interface && !is_subclass_of($class, $required_interface)) {
            throw new PluginException(sprintf('Plugin "%s" (%s) must implement interface %s.', $plugin_id, $class, $required_interface));
        }
        return $class;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DefaultFactory::$discovery protected property The object that retrieves the definitions of the plugins that this factory instantiates.
DefaultFactory::$interface protected property Defines an interface each plugin should implement.
DefaultFactory::createInstance public function Creates a plugin instance based on the provided ID and configuration. Overrides FactoryInterface::createInstance 2
DefaultFactory::getPluginClass public static function Finds the class relevant for a given plugin. 1
DefaultFactory::__construct public function Constructs a Drupal\Component\Plugin\Factory\DefaultFactory object.

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