You are here

public function ContainerFactory::createInstance in Plug 7

Creates a pre-configured instance of a plugin.

Parameters

string $plugin_id: The ID of the plugin being instantiated.

array $configuration: An array of configuration relevant to the plugin instance.

Return value

object A fully configured plugin instance.

Throws

\Drupal\Component\Plugin\Exception\PluginException If the instance cannot be created, such as if the ID is invalid.

Overrides DefaultFactory::createInstance

File

lib/Drupal/Core/Plugin/Factory/ContainerFactory.php, line 19
Contains \Drupal\Core\Plugin\Factory\ContainerFactory.

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 = array()) {
  $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);
}