diff --git a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php index 4059e10..701b6f9 100644 --- a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php +++ b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php @@ -9,6 +9,7 @@ use Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface; use Drupal\Component\Plugin\Discovery\DiscoveryCachedTrait; +use Drupal\Component\Plugin\Exception\PluginException; use Drupal\Component\Plugin\Exception\PluginNotFoundException; use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator; use Drupal\Component\Plugin\PluginManagerBase; @@ -83,6 +84,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 @@ -198,7 +206,6 @@ protected function setCachedDefinitions($definitions) { $this->definitions = $definitions; } - /** * Performs extra processing on plugin definitions. * @@ -212,6 +219,7 @@ public function processDefinition(&$definition, $plugin_id) { } } + /** * Finds plugin definitions. * @@ -241,4 +249,50 @@ 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; + } + + /** + * Sets the fallback plugin ID. + * + * @param string $fallback_plugin_id + * The plugin ID to fall back to. + */ + public function setFallbackPluginId($fallback_plugin_id) { + $this->fallbackPluginId = $fallback_plugin_id; + } + } diff --git a/core/modules/views/src/Plugin/ViewsHandlerManager.php b/core/modules/views/src/Plugin/ViewsHandlerManager.php index c3651cf..acda778 100644 --- a/core/modules/views/src/Plugin/ViewsHandlerManager.php +++ b/core/modules/views/src/Plugin/ViewsHandlerManager.php @@ -11,6 +11,7 @@ use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Plugin\DefaultPluginManager; +use Drupal\views\Plugin\views\HandlerBase; use Drupal\views\ViewsData; use Symfony\Component\DependencyInjection\Container; @@ -55,6 +56,7 @@ public function __construct($handler_type, \Traversable $namespaces, ViewsData $ parent::__construct("Plugin/views/$handler_type", $namespaces, $module_handler, $plugin_definition_annotation_name); $this->setCacheBackend($cache_backend, "views:$handler_type", array('extension' => array(TRUE, 'views'))); + $this->setFallbackPluginId('broken'); $this->viewsData = $views_data; $this->handlerType = $handler_type; @@ -101,30 +103,26 @@ public function getHandler($item, $override = NULL) { } } } - - // @todo This is crazy. Find a way to remove the override functionality. - $plugin_id = $override ? : $definition['id']; - // Try to use the overridden handler. - try { - return $this->createInstance($plugin_id, $definition); - } - catch (PluginException $e) { - // If that fails, use the original handler. - try { - return $this->createInstance($definition['id'], $definition); - } - catch (PluginException $e) { - // Deliberately empty, this case is handled generically below. - } - } } - - if (!$optional) { - // debug(t("Missing handler: @table @field @type", array('@table' => $table, '@field' => $field, '@type' => $this->handlerType))); + else { + $definition = $item; + $definition['id'] = 'non_existing'; } - // Finally, use the 'broken' handler. - return $this->createInstance('broken', array('optional' => $optional, 'original_configuration' => $item)); + // @todo This is crazy. Find a way to remove the override functionality. + $plugin_id = $override ? : $definition['id']; + // Try to use the overridden handler. + try { + return $this->createInstance($plugin_id, $definition); + } + catch (PluginException $e) { + // If that fails, use the original handler / or the broken fallback. + $this->setFallbackPluginId('broken'); + $definition['optional'] = $optional; + $result = $this->createInstance($definition['id'], $definition); + $this->setFallbackPluginId(NULL); + return $result; + } } /** diff --git a/core/modules/views/src/Plugin/views/BrokenHandlerTrait.php b/core/modules/views/src/Plugin/views/BrokenHandlerTrait.php index e1623f7..9fc5b68 100644 --- a/core/modules/views/src/Plugin/views/BrokenHandlerTrait.php +++ b/core/modules/views/src/Plugin/views/BrokenHandlerTrait.php @@ -19,7 +19,7 @@ */ public function adminLabel($short = FALSE) { $args = array( - '@module' => $this->definition['original_configuration']['provider'], + '@module' => $this->definition['provider'], ); return $this->isOptional() ? t('Optional handler is missing (Module: @module) …', $args) : t('Broken/missing handler (Module: @module) …', $args); } @@ -64,9 +64,9 @@ public function buildOptionsForm(&$form, &$form_state) { } $items = array( - t('Module: @module', array('@module' => $this->definition['original_configuration']['provider'])), - t('Table: @table', array('@table' => $this->definition['original_configuration']['table'])), - t('Field: @field', array('@field' => $this->definition['original_configuration']['field'])), + t('Module: @module', array('@module' => $this->definition['provider'])), + t('Table: @table', array('@table' => $this->definition['table'])), + t('Field: @field', array('@field' => $this->definition['field'])), ); $description_bottom = t('Enabling the appropriate module will may solve this issue. Otherwise, check to see if there is a module update available.'); diff --git a/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php b/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php index b38da38..572fe60 100644 --- a/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php @@ -7,12 +7,17 @@ namespace Drupal\Tests\Core\Plugin; +use Drupal\Component\Plugin\Exception\PluginException; +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 +74,8 @@ protected function setUp() { /** * Tests the plugin manager with a disabled module. + * + * @covers ::getDefinition */ public function testDefaultPluginManagerWithDisabledModule() { $definitions = $this->expectedDefinitions; @@ -119,6 +126,9 @@ public function testDefaultPluginManagerWithObjects() { /** * Tests the plugin manager with no cache and altering. + * + * @covers ::getDefinitions + * @covers ::getDefinition */ public function testDefaultPluginManager() { $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions); @@ -128,6 +138,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') @@ -148,6 +161,9 @@ public function testDefaultPluginManagerWithAlter() { /** * Tests the plugin manager with caching and altering. + * + * @covers ::getDefinitions + * @covers ::getDefinition */ public function testDefaultPluginManagerWithEmptyCache() { $cid = $this->randomName(); @@ -173,6 +189,8 @@ public function testDefaultPluginManagerWithEmptyCache() { /** * Tests the plugin manager with caching and altering. + * + * @covers ::getDefinitions */ public function testDefaultPluginManagerWithFilledCache() { $cid = $this->randomName(); @@ -196,6 +214,9 @@ public function testDefaultPluginManagerWithFilledCache() { /** * Tests the plugin manager cache clear with tags. + * + * @covers ::getFallbackPluginId + * @covers ::createInstance */ public function testCacheClearWithTags() { $cid = $this->randomName(); @@ -218,6 +239,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; + } + }