diff --git a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php index 99abd89..6a3bbe9 100644 --- a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php +++ b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php @@ -8,6 +8,7 @@ namespace Drupal\Core\Plugin; use Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface; +use Drupal\Component\Plugin\Exception\PluginException; use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator; use Drupal\Component\Plugin\PluginManagerBase; use Drupal\Component\Plugin\PluginManagerInterface; @@ -99,6 +100,13 @@ class DefaultPluginManager extends PluginManagerBase implements PluginManagerInt protected $defaults = array(); /** + * Defines a fallback ID in case the plugin could not be created. + * + * @var string + */ + protected $fallbackPluginId; + + /** * Creates the discovery object. * * @param string|bool $subdir @@ -112,12 +120,13 @@ class DefaultPluginManager extends PluginManagerBase implements PluginManagerInt * (optional) The name of the annotation that contains the plugin definition. * Defaults to 'Drupal\Component\Annotation\Plugin'. */ - public function __construct($subdir, \Traversable $namespaces, ModuleHandlerInterface $module_handler, $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin') { + public function __construct($subdir, \Traversable $namespaces, ModuleHandlerInterface $module_handler, $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin', $fallback_plugin_id = NULL) { $this->subdir = $subdir; $this->discovery = new AnnotatedClassDiscovery($subdir, $namespaces, $plugin_definition_annotation_name); $this->discovery = new ContainerDerivativeDiscoveryDecorator($this->discovery); $this->factory = new ContainerFactory($this); $this->moduleHandler = $module_handler; + $this->fallbackPluginId = $fallback_plugin_id; } /** @@ -277,4 +286,25 @@ protected function findDefinitions() { return $definitions; } + /** + * {@inheritdoc} + */ + public function createInstance($plugin_id, array $configuration = array()) { + + try { + $instance = $this->factory->createInstance($plugin_id, $configuration); + } + catch (PluginException $exception) { + if (isset($this->fallbackPluginId)) { + // Allow implementations show the exception message. + $configuration['_exception'] = $exception; + $instance = $this->factory->createInstance($this->fallbackPluginId, $configuration); + } + else { + throw $exception; + } + } + return $instance; + } + }