class ContainerFactory

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

Plugin factory which passes a container to a create method.

Hierarchy

Expanded class hierarchy of ContainerFactory

11 files declare their use of ContainerFactory
BreakpointManager.php in core/modules/breakpoint/src/BreakpointManager.php
ComponentPluginManager.php in core/modules/sdc/src/ComponentPluginManager.php
ComponentPluginManager.php in core/lib/Drupal/Core/Theme/ComponentPluginManager.php
ConfigMapperManager.php in core/modules/config_translation/src/ConfigMapperManager.php
ConstraintFactory.php in core/lib/Drupal/Core/Validation/ConstraintFactory.php

... See full list

File

core/lib/Drupal/Core/Plugin/Factory/ContainerFactory.php, line 10

Namespace

Drupal\Core\Plugin\Factory
View source
class ContainerFactory extends DefaultFactory {
    
    /**
     * {@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);
        // If the plugin provides a factory method, pass the container to it.
        if (is_subclass_of($plugin_class, 'Drupal\\Core\\Plugin\\ContainerFactoryPluginInterface')) {
            return $plugin_class::create(\Drupal::getContainer(), $configuration, $plugin_id, $plugin_definition);
        }
        // Otherwise, create the plugin directly.
        return new $plugin_class($configuration, $plugin_id, $plugin_definition);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ContainerFactory::createInstance public function Creates a plugin instance based on the provided ID and configuration. Overrides DefaultFactory::createInstance 1
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::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.