You are here

public function PluginManagerBase::createInstance in Service Container 7

Same name in this branch
  1. 7 src/Plugin/PluginManagerBase.php \Drupal\service_container\Plugin\PluginManagerBase::createInstance()
  2. 7 lib/Drupal/Component/Plugin/PluginManagerBase.php \Drupal\Component\Plugin\PluginManagerBase::createInstance()
Same name and namespace in other branches
  1. 7.2 lib/Drupal/Component/Plugin/PluginManagerBase.php \Drupal\Component\Plugin\PluginManagerBase::createInstance()

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 FactoryInterface::createInstance

File

lib/Drupal/Component/Plugin/PluginManagerBase.php, line 76
Contains \Drupal\Component\Plugin\PluginManagerBase.

Class

PluginManagerBase
Base class for plugin managers.

Namespace

Drupal\Component\Plugin

Code

public function createInstance($plugin_id, array $configuration = array()) {

  // If this PluginManager has fallback capabilities catch
  // PluginNotFoundExceptions.
  if ($this instanceof FallbackPluginManagerInterface) {
    try {
      return $this
        ->getFactory()
        ->createInstance($plugin_id, $configuration);
    } catch (PluginNotFoundException $e) {
      $fallback_id = $this
        ->getFallbackPluginId($plugin_id, $configuration);
      return $this
        ->getFactory()
        ->createInstance($fallback_id, $configuration);
    }
  }
  else {
    return $this
      ->getFactory()
      ->createInstance($plugin_id, $configuration);
  }
}