function ContainerFactory::createInstance

Same name in other branches
  1. 9 core/lib/Drupal/Core/Plugin/Factory/ContainerFactory.php \Drupal\Core\Plugin\Factory\ContainerFactory::createInstance()
  2. 8.9.x core/lib/Drupal/Core/Plugin/Factory/ContainerFactory.php \Drupal\Core\Plugin\Factory\ContainerFactory::createInstance()
  3. 10 core/lib/Drupal/Core/Plugin/Factory/ContainerFactory.php \Drupal\Core\Plugin\Factory\ContainerFactory::createInstance()

Overrides DefaultFactory::createInstance

1 method overrides ContainerFactory::createInstance()
ConstraintFactory::createInstance in core/lib/Drupal/Core/Validation/ConstraintFactory.php
Creates a plugin instance based on the provided ID and configuration.

File

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

Class

ContainerFactory
Plugin factory which passes a container to a create method.

Namespace

Drupal\Core\Plugin\Factory

Code

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);
}

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