diff --git a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php index e1d0731..02605f9 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; @@ -20,6 +21,7 @@ use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery; use Drupal\Core\Plugin\Factory\ContainerFactory; +use \Symfony\Component\Debug\Exception\FlattenException; /** * Base class for plugin managers. @@ -83,6 +85,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 +207,6 @@ protected function setCachedDefinitions($definitions) { $this->definitions = $definitions; } - /** * Performs extra processing on plugin definitions. * @@ -241,4 +249,57 @@ protected function findDefinitions() { return $definitions; } + /** + * {@inheritdoc} + */ + public function createInstance($plugin_id, array $configuration = array()) { + try { + return $this->factory->createInstance($plugin_id, $configuration); + } + catch (PluginException $exception) { + if ($fallback_plugin_id = $this->getFallbackPluginId($plugin_id, $configuration)) { + // Allow implementations to show the exception message. + $configuration['_exception'] = new FlattenException($exception); + try { + return $this->factory->createInstance($this->getFallbackPluginId($plugin_id, $configuration), $configuration); + } + catch (PluginException $e) { + // We throw the original exception instead. + } + } + else { + throw $exception; + } + } + } + + /** + * 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. + * + * @return $this + */ + public function setFallbackPluginId($fallback_plugin_id) { + $this->fallbackPluginId = $fallback_plugin_id; + return $this; + } + } diff --git a/core/modules/filter/src/FilterPluginManager.php b/core/modules/filter/src/FilterPluginManager.php index 77a3c71..d99062d 100644 --- a/core/modules/filter/src/FilterPluginManager.php +++ b/core/modules/filter/src/FilterPluginManager.php @@ -37,19 +37,8 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac parent::__construct('Plugin/Filter', $namespaces, $module_handler, 'Drupal\filter\Annotation\Filter'); $this->alterInfo('filter_info'); $this->setCacheBackend($cache_backend, 'filter_plugins', array('filter_formats' => TRUE)); - } - /** - * {@inheritdoc} - */ - public function getDefinition($plugin_id, $exception_on_invalid = TRUE) { - $definitions = $this->getDefinitions(); - // Avoid using a ternary that would create a copy of the array. - if (isset($definitions[$plugin_id])) { - return $definitions[$plugin_id]; - } - // If the requested filter is missing, use the null filter. - return $definitions['filter_null']; + $this->setFallbackPluginId('filter_null'); } } diff --git a/core/modules/views/src/Plugin/ViewsHandlerManager.php b/core/modules/views/src/Plugin/ViewsHandlerManager.php index 7b7b76e..0f65c6b 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; @@ -97,26 +99,23 @@ 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. - } - } + } + else { + $definition = $item; + $definition['id'] = 'non_existing'; } - // Finally, use the 'broken' handler. - return $this->createInstance('broken', array('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 or the original handler. + try { + return $this->createInstance($plugin_id, $definition); + } + catch (PluginException $e) { + // If that fails, use the original handler or the broken fallback. + $result = $this->createInstance($definition['id'], $definition); + return $result; + } } /** diff --git a/core/modules/views/src/Plugin/views/BrokenHandlerTrait.php b/core/modules/views/src/Plugin/views/BrokenHandlerTrait.php index 4bd9948..0ecb10e 100644 --- a/core/modules/views/src/Plugin/views/BrokenHandlerTrait.php +++ b/core/modules/views/src/Plugin/views/BrokenHandlerTrait.php @@ -21,7 +21,7 @@ */ public function adminLabel($short = FALSE) { $args = array( - '@module' => $this->definition['original_configuration']['provider'], + '@module' => $this->definition['provider'], ); return t('Broken/missing handler (Module: @module) …', $args); } @@ -61,9 +61,9 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { $description_top = t('The handler for this item is broken or missing. The following details are available:'); $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 1eb0d26..500e6cd 100644 --- a/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php @@ -7,12 +7,18 @@ namespace Drupal\Tests\Core\Plugin; +use Drupal\Component\Plugin\Exception\PluginException; +use Drupal\plugin_test\Plugin\plugin_test\fruit\Apple; use Drupal\Tests\UnitTestCase; +use Symfony\Component\Debug\Exception\FlattenException; /** * Tests the DefaultPluginManager. * * @group Plugin + * @group Drupal + * + * @coversDefaultClass Drupal\Core\Plugin\DefaultPluginManager */ class DefaultPluginManagerTest extends UnitTestCase { @@ -58,6 +64,8 @@ protected function setUp() { /** * Tests the plugin manager with a disabled module. + * + * @covers ::getDefinition */ public function testDefaultPluginManagerWithDisabledModule() { $definitions = $this->expectedDefinitions; @@ -108,6 +116,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); @@ -117,6 +128,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') @@ -137,6 +151,9 @@ public function testDefaultPluginManagerWithAlter() { /** * Tests the plugin manager with caching and altering. + * + * @covers ::getDefinitions + * @covers ::getDefinition */ public function testDefaultPluginManagerWithEmptyCache() { $cid = $this->randomMachineName(); @@ -162,6 +179,8 @@ public function testDefaultPluginManagerWithEmptyCache() { /** * Tests the plugin manager with caching and altering. + * + * @covers ::getDefinitions */ public function testDefaultPluginManagerWithFilledCache() { $cid = $this->randomMachineName(); @@ -185,6 +204,9 @@ public function testDefaultPluginManagerWithFilledCache() { /** * Tests the plugin manager cache clear with tags. + * + * @covers ::getFallbackPluginId + * @covers ::createInstance */ public function testCacheClearWithTags() { $cid = $this->randomMachineName(); @@ -207,4 +229,43 @@ 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' => new FlattenException($exception), 'key' => 'value')) + ->will($this->returnValue($apple)); + + $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, NULL, NULL, 'apple'); + $plugin_manager->setFactory($factory); + $plugin = $plugin_manager->createInstance('non_existing', array('key' => 'value')); + $this->assertSame($apple, $plugin); + } + } diff --git a/core/tests/Drupal/Tests/Core/Plugin/TestPluginManager.php b/core/tests/Drupal/Tests/Core/Plugin/TestPluginManager.php index a7ea79d..cf189ab 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,8 +29,10 @@ 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) { + public function __construct(\Traversable $namespaces, array $definitions, ModuleHandlerInterface $module_handler = NULL, $alter_hook = NULL, $fallback_plugin_id = NULL) { // Create the object that can be used to return definitions for all the // plugins available for this type. Most real plugin managers use a richer // discovery implementation, but StaticDiscovery lets us add some simple @@ -46,6 +49,17 @@ public function __construct(\Traversable $namespaces, array $definitions, Module if ($alter_hook) { $this->alterInfo($alter_hook); } + $this->setFallbackPluginId($fallback_plugin_id); + } + + /** + * Sets the plugin factory. + * + * @param \Drupal\Component\Plugin\Factory\FactoryInterface $factory + * (optional) The plugin factory. + */ + public function setFactory(FactoryInterface $factory) { + $this->factory = $factory; } }