diff --git a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php index e9c4ce9..bf6e6ec 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 instance could not be created. + * + * @var string + */ + protected $fallbackPluginId; + + /** * Creates the discovery object. * * @param string|bool $subdir @@ -162,6 +170,16 @@ protected function alterInfo($alter_hook) { } /** + * Sets the fallback plugin ID. + * + * @param string $fallback_plugin_id + * The plugin ID to be falled back to. + */ + public function setFallbackPluginId($fallback_plugin_id) { + $this->fallbackPluginId = $fallback_plugin_id; + } + + /** * {@inheritdoc} */ public function getDefinition($plugin_id) { @@ -277,4 +295,40 @@ 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 ($fallback_plugin_id = $this->getFallbackPluginId($plugin_id, $configuration)) { + // Allow implementations show the exception message. + $configuration['_exception'] = $exception; + $instance = $this->factory->createInstance($this->getFallbackPluginId($plugin_id, $configuration), $configuration); + } + else { + throw $exception; + } + } + return $instance; + } + + /** + * Returns the fallback plugin ID to be used. + * + * @param string $original_plugin_id + * The plugin ID of the non-instantiable plugin. + * @param array $configuration + * The original configuration of the plugin. + * + * @return string|null + * The plugin ID of the fallback instance or NULL if there is no fallback + * set. + */ + protected function getFallbackPluginId($original_plugin_id, array $configuration) { + return $this->fallbackPluginId; + } + } diff --git a/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php b/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php index 4862ac7..e3cae95 100644 --- a/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php @@ -7,13 +7,19 @@ namespace Drupal\Tests\Core\Plugin; +use Drupal\Component\Plugin\Exception\PluginException; +use Drupal\Component\Plugin\Factory\DefaultFactory; use Drupal\Core\Language\Language; +use Drupal\plugin_test\Plugin\plugin_test\fruit\Apple; use Drupal\Tests\UnitTestCase; /** * Tests the DefaultPluginManager. * * @group Plugin + * @group Drupal + * + * @coversDefaultClass Drupal\Core\Plugin\DefaultPluginManager */ class DefaultPluginManagerTest extends UnitTestCase { @@ -69,6 +75,8 @@ protected function setUp() { /** * Tests the plugin manager with a disabled module. + * + * @covers ::getDefinition */ public function testDefaultPluginManagerWithDisabledModule() { $definitions = $this->expectedDefinitions; @@ -94,6 +102,9 @@ public function testDefaultPluginManagerWithDisabledModule() { /** * Tests the plugin manager with no cache and altering. + * + * @covers ::getDefinitions + * @covers ::getDefinition */ public function testDefaultPluginManager() { $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions); @@ -103,6 +114,9 @@ public function testDefaultPluginManager() { /** * Tests the plugin manager with no cache and altering. + * + * @covers ::getDefinitions + * @covers ::getDefinition */ public function testDefaultPluginManagerWithAlter() { $module_handler = $this->getMockBuilder('Drupal\Core\Extension\ModuleHandler') @@ -123,6 +137,9 @@ public function testDefaultPluginManagerWithAlter() { /** * Tests the plugin manager with caching and altering. + * + * @covers ::getDefinitions + * @covers ::getDefinition */ public function testDefaultPluginManagerWithEmptyCache() { $cid = $this->randomName(); @@ -155,6 +172,8 @@ public function testDefaultPluginManagerWithEmptyCache() { /** * Tests the plugin manager with caching and altering. + * + * @covers ::getDefinitions */ public function testDefaultPluginManagerWithFilledCache() { $cid = $this->randomName(); @@ -185,6 +204,9 @@ public function testDefaultPluginManagerWithFilledCache() { /** * Tests the plugin manager cache clear with tags. + * + * @covers ::getFallbackPluginId + * @covers ::createInstance */ public function testCacheClearWithTags() { $cid = $this->randomName(); @@ -214,6 +236,46 @@ public function testCacheClearWithTags() { $plugin_manager->clearCachedDefinitions(); } + /** + * Tests the plugin manager without a fallback plugin. + * + * @expectedException \Drupal\Component\Plugin\Exception\PluginException + */ + public function testCreateInstanceWithoutFallback() { + $factory = $this->getMock('\Drupal\Component\Plugin\Factory\FactoryInterface'); + $factory->expects($this->once()) + ->method('createInstance') + ->with('non_existing', array()) + ->will($this->throwException(new PluginException())); + + $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, NULL, NULL); + $plugin_manager->setFactory($factory); + $plugin_manager->createInstance('non_existing'); + } + + /** + * Tests the plugin manager with a fallback plugin. + */ + public function testCreateInstanceWithFallback() { + $factory = $this->getMock('\Drupal\Component\Plugin\Factory\FactoryInterface'); + $apple = new Apple(); + $exception = new PluginException(); + $factory->expects($this->at(0)) + ->method('createInstance') + ->with('non_existing', array('key' => 'value')) + ->will($this->throwException($exception)); + $factory->expects($this->at(1)) + ->method('createInstance') + ->with('apple', array('_exception' => $exception, 'key' => 'value')) + ->will($this->returnValue($apple)); + + $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions); + $plugin_manager->setFallbackPluginId('apple'); + $plugin_manager->setFactory($factory); + $plugin = $plugin_manager->createInstance('non_existing', array('key' => 'value')); + $this->assertSame($apple, $plugin); + } + } if (!defined('DRUPAL_ROOT')) { diff --git a/core/tests/Drupal/Tests/Core/Plugin/TestPluginManager.php b/core/tests/Drupal/Tests/Core/Plugin/TestPluginManager.php index a7ea79d..4619640 100644 --- a/core/tests/Drupal/Tests/Core/Plugin/TestPluginManager.php +++ b/core/tests/Drupal/Tests/Core/Plugin/TestPluginManager.php @@ -8,6 +8,7 @@ namespace Drupal\Tests\Core\Plugin; use Drupal\Component\Plugin\Discovery\StaticDiscovery; +use Drupal\Component\Plugin\Factory\FactoryInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Plugin\DefaultPluginManager; @@ -28,6 +29,8 @@ class TestPluginManager extends DefaultPluginManager { * (optional) The module handler to invoke the alter hook with. * @param string $alter_hook * (optional) Name of the alter hook. + * @param string $fallback_plugin_id + * (optional) The fallback plugin ID. */ public function __construct(\Traversable $namespaces, array $definitions, ModuleHandlerInterface $module_handler = NULL, $alter_hook = NULL) { // Create the object that can be used to return definitions for all the @@ -48,4 +51,14 @@ public function __construct(\Traversable $namespaces, array $definitions, Module } } + /** + * Sets the plugin factory. + * + * @param \Drupal\Component\Plugin\Factory\FactoryInterface $factory + * (optional) The plugin factory. + */ + public function setFactory(FactoryInterface $factory) { + $this->factory = $factory; + } + }