diff --git a/core/lib/Drupal/Component/Plugin/Discovery/DerivativeDiscoveryDecorator.php b/core/lib/Drupal/Component/Plugin/Discovery/DerivativeDiscoveryDecorator.php index 2428109..62f3dd3 100644 --- a/core/lib/Drupal/Component/Plugin/Discovery/DerivativeDiscoveryDecorator.php +++ b/core/lib/Drupal/Component/Plugin/Discovery/DerivativeDiscoveryDecorator.php @@ -35,7 +35,7 @@ public function __construct(DiscoveryInterface $decorated) { /** * Implements Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinition(). */ - public function getDefinition($plugin_id, $exception = TRUE) { + public function getDefinition($plugin_id, $invalid_behavior = self::EXCEPTION_ON_INVALID_ID) { list($base_plugin_id, $derivative_id) = $this->decodePluginId($plugin_id); @@ -45,7 +45,7 @@ public function getDefinition($plugin_id, $exception = TRUE) { if ($derivative_fetcher) { $plugin_definition = $derivative_fetcher->getDerivativeDefinition($derivative_id, $plugin_definition); if (!isset($plugin_definition)) { - if ($exception) { + if ($invalid_behavior === self::EXCEPTION_ON_INVALID_ID) { // @todo will this handle nested derivatives gracefuly? throw new DerivativeNotFoundException(sprintf('Derivative plugin "%s", derived from base plugin "%s", could not be found.', $base_plugin_id, $derivative_id)); } diff --git a/core/lib/Drupal/Component/Plugin/Discovery/DiscoveryInterface.php b/core/lib/Drupal/Component/Plugin/Discovery/DiscoveryInterface.php index f1c55d6..5659b60 100644 --- a/core/lib/Drupal/Component/Plugin/Discovery/DiscoveryInterface.php +++ b/core/lib/Drupal/Component/Plugin/Discovery/DiscoveryInterface.php @@ -15,6 +15,9 @@ */ interface DiscoveryInterface { + const EXCEPTION_ON_INVALID_ID = 1; + const NULL_ON_INVALID_ID = 2; + /** * Gets a specific plugin definition. * @@ -29,7 +32,7 @@ * * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException */ - public function getDefinition($plugin_id, $exception = TRUE); + public function getDefinition($plugin_id, $invalid_behavior = self::EXCEPTION_ON_INVALID_ID); /** * Gets the definition of all plugins for this type. diff --git a/core/lib/Drupal/Component/Plugin/Discovery/StaticDiscovery.php b/core/lib/Drupal/Component/Plugin/Discovery/StaticDiscovery.php index 56bb240..080854f 100644 --- a/core/lib/Drupal/Component/Plugin/Discovery/StaticDiscovery.php +++ b/core/lib/Drupal/Component/Plugin/Discovery/StaticDiscovery.php @@ -25,11 +25,11 @@ class StaticDiscovery implements DiscoveryInterface { /** * Implements Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinition(). */ - public function getDefinition($base_plugin_id, $exception = TRUE) { + public function getDefinition($base_plugin_id, $invalid_behavior = self::EXCEPTION_ON_INVALID_ID) { if (isset($this->definitions[$base_plugin_id])) { return $this->definitions[$base_plugin_id]; } - else if ($exception) { + else if ($invalid_behavior === self::EXCEPTION_ON_INVALID_ID) { throw new PluginNotFoundException(sprintf('The plugin (%s) could not be found.', $base_plugin_id));; } } diff --git a/core/lib/Drupal/Component/Plugin/PluginManagerBase.php b/core/lib/Drupal/Component/Plugin/PluginManagerBase.php index ee9115e..85af54c 100644 --- a/core/lib/Drupal/Component/Plugin/PluginManagerBase.php +++ b/core/lib/Drupal/Component/Plugin/PluginManagerBase.php @@ -49,16 +49,22 @@ /** * Implements Drupal\Component\Plugin\PluginManagerInterface::getDefinition(). */ - public function getDefinition($plugin_id, $exception = TRUE) { + public function getDefinition($plugin_id, $invalid_behavior = self::EXCEPTION_ON_INVALID_ID) { try { - $definition = $this->discovery->getDefinition($plugin_id, $exception); + $definition = $this->discovery->getDefinition($plugin_id, $invalid_behavior); } catch (PluginNotFoundException $e) { - throw new PluginNotFoundException(sprintf('Plugin manager "%s" was unable to find plugin with id "%s".', get_class($this), $plugin_id), E_RECOVERABLE_ERROR, $e); + $refl = new \ReflectionClass($e); + // Ensure we reproduce the exact same exception type, even if a child class. + // @todo this is horrible. can we make this better? + throw $refl->newInstance(sprintf('Plugin manager "%s" was unable to find plugin with id "%s".', get_class($this), $plugin_id), E_RECOVERABLE_ERROR, $e); + // throw new PluginNotFoundException(sprintf('Plugin manager "%s" was unable to find plugin with id "%s".', get_class($this), $plugin_id), E_RECOVERABLE_ERROR, $e); } - $this->processDefinition($definition, $plugin_id); - return $definition; + if (!is_null($definition)) { + $this->processDefinition($definition, $plugin_id); + return $definition; + } } /** diff --git a/core/lib/Drupal/Core/Plugin/Discovery/AlterDecorator.php b/core/lib/Drupal/Core/Plugin/Discovery/AlterDecorator.php index f7f3aa1..fab047f 100644 --- a/core/lib/Drupal/Core/Plugin/Discovery/AlterDecorator.php +++ b/core/lib/Drupal/Core/Plugin/Discovery/AlterDecorator.php @@ -46,12 +46,12 @@ public function __construct(DiscoveryInterface $decorated, $hook) { /** * Implements Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinition(). */ - public function getDefinition($plugin_id, $exception = TRUE) { + public function getDefinition($plugin_id, $invalid_behavior = self::EXCEPTION_ON_INVALID_ID) { $definitions = $this->getDefinitions(); if (isset($definitions[$plugin_id])) { return $definitions[$plugin_id]; } - else if ($exception) { + else if ($invalid_behavior === self::EXCEPTION_ON_INVALID_ID) { throw new PluginNotFoundException(sprintf('The plugin (%s) could not be found.', $plugin_id));; } } diff --git a/core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php b/core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php index 2891e68..74380f5 100644 --- a/core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php +++ b/core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php @@ -32,12 +32,12 @@ function __construct($owner, $type) { /** * Implements Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinition(). */ - public function getDefinition($plugin_id, $exception = TRUE) { + public function getDefinition($plugin_id, $invalid_behavior = self::EXCEPTION_ON_INVALID_ID) { $definitions = $this->getDefinitions(); if (isset($definitions[$plugin_id])) { return $definitions[$plugin_id]; } - else if ($exception) { + else if ($invalid_behavior === self::EXCEPTION_ON_INVALID_ID) { throw new PluginNotFoundException(sprintf('The plugin (%s) could not be found.', $plugin_id));; } } diff --git a/core/lib/Drupal/Core/Plugin/Discovery/CacheDecorator.php b/core/lib/Drupal/Core/Plugin/Discovery/CacheDecorator.php index 641b2d3..b482567 100644 --- a/core/lib/Drupal/Core/Plugin/Discovery/CacheDecorator.php +++ b/core/lib/Drupal/Core/Plugin/Discovery/CacheDecorator.php @@ -65,12 +65,12 @@ public function __construct(DiscoveryInterface $decorated, $cache_key, $cache_bi /** * Implements Drupal\Component\Plugin\Discovery\DicoveryInterface::getDefinition(). */ - public function getDefinition($plugin_id, $exception = TRUE) { + public function getDefinition($plugin_id, $invalid_behavior = self::EXCEPTION_ON_INVALID_ID) { $definitions = $this->getDefinitions(); if (isset($definitions[$plugin_id])) { return $definitions[$plugin_id]; } - else if ($exception) { + else if ($invalid_behavior === self::EXCEPTION_ON_INVALID_ID) { throw new PluginNotFoundException(sprintf('The plugin (%s) could not be found.', $plugin_id));; } } diff --git a/core/lib/Drupal/Core/Plugin/Discovery/HookDiscovery.php b/core/lib/Drupal/Core/Plugin/Discovery/HookDiscovery.php index 10a2462..b7c6d3c 100644 --- a/core/lib/Drupal/Core/Plugin/Discovery/HookDiscovery.php +++ b/core/lib/Drupal/Core/Plugin/Discovery/HookDiscovery.php @@ -36,12 +36,12 @@ function __construct($hook) { /** * Implements Drupal\Component\Plugin\Discovery\DicoveryInterface::getDefinition(). */ - public function getDefinition($plugin_id, $exception = TRUE) { + public function getDefinition($plugin_id, $invalid_behavior = self::EXCEPTION_ON_INVALID_ID) { $definitions = $this->getDefinitions(); if (isset($definitions[$plugin_id])) { return $definitions[$plugin_id]; } - else if ($exception) { + else if ($invalid_behavior === self::EXCEPTION_ON_INVALID_ID) { throw new PluginNotFoundException(sprintf('The plugin (%s) could not be found.', $plugin_id));; } } diff --git a/core/lib/Drupal/Core/Plugin/Discovery/InfoHookDecorator.php b/core/lib/Drupal/Core/Plugin/Discovery/InfoHookDecorator.php index deddb31..04b4418 100644 --- a/core/lib/Drupal/Core/Plugin/Discovery/InfoHookDecorator.php +++ b/core/lib/Drupal/Core/Plugin/Discovery/InfoHookDecorator.php @@ -45,12 +45,12 @@ public function __construct(DiscoveryInterface $decorated, $hook) { /** * Implements Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinition(). */ - public function getDefinition($plugin_id, $exception = TRUE) { + public function getDefinition($plugin_id, $invalid_behavior = self::EXCEPTION_ON_INVALID_ID) { $definitions = $this->getDefinitions(); if (isset($definitions[$plugin_id])) { return $definitions[$plugin_id]; } - else if ($exception) { + else if ($invalid_behavior === self::EXCEPTION_ON_INVALID_ID) { throw new PluginNotFoundException(sprintf('The plugin (%s) could not be found.', $plugin_id));; } } diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterPluginManager.php b/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterPluginManager.php index 6c3840e..ee81484 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterPluginManager.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterPluginManager.php @@ -8,6 +8,7 @@ namespace Drupal\field\Plugin\Type\Formatter; use Drupal\Component\Plugin\PluginManagerBase; +use Drupal\Component\Plugin\Discovery\DiscoveryInterface; use Drupal\Core\Plugin\Discovery\CacheDecorator; use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery; use Drupal\field\Plugin\Type\Formatter\FormatterLegacyDiscoveryDecorator; @@ -45,7 +46,7 @@ public function getInstance(array $options) { $instance = $options['instance']; $type = $options['type']; - $definition = $this->getDefinition($type, FALSE); + $definition = $this->getDefinition($type, DiscoveryInterface::NULL_ON_INVALID_ID); $field = field_info_field($instance['field_name']); // Switch back to default formatter if either: diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/LegacyDiscoveryDecorator.php b/core/modules/field/lib/Drupal/field/Plugin/Type/LegacyDiscoveryDecorator.php index b74c8db..dbedd66 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/LegacyDiscoveryDecorator.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/LegacyDiscoveryDecorator.php @@ -47,12 +47,12 @@ public function __construct(DiscoveryInterface $decorated) { /** * Implements Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinition(). */ - public function getDefinition($plugin_id, $exception = TRUE) { + public function getDefinition($plugin_id, $invalid_behavior = self::EXCEPTION_ON_INVALID_ID) { $definitions = $this->getDefinitions(); if (isset($definitions[$plugin_id])) { return $definitions[$plugin_id]; } - else if ($exception) { + else if ($invalid_behavior === self::EXCEPTION_ON_INVALID_ID) { throw new PluginNotFoundException(sprintf('The plugin (%s) could not be found.', $plugin_id));; } } diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetPluginManager.php b/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetPluginManager.php index 99f379e..26c10b6 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetPluginManager.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetPluginManager.php @@ -8,6 +8,7 @@ namespace Drupal\field\Plugin\Type\Widget; use Drupal\Component\Plugin\PluginManagerBase; +use Drupal\Component\Plugin\Discovery\DiscoveryInterface; use Drupal\Core\Plugin\Discovery\CacheDecorator; use Drupal\Core\Plugin\Discovery\AlterDecorator; use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery; @@ -46,7 +47,7 @@ public function getInstance(array $options) { $instance = $options['instance']; $type = $options['type']; - $definition = $this->getDefinition($type, TRUE); + $definition = $this->getDefinition($type, DiscoveryInterface::NULL_ON_INVALID_ID); $field = field_info_field($instance['field_name']); // Switch back to default widget if either: diff --git a/core/modules/system/lib/Drupal/system/Tests/Plugin/CacheDecoratorTest.php b/core/modules/system/lib/Drupal/system/Tests/Plugin/CacheDecoratorTest.php index 327e46e..eb5098c 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Plugin/CacheDecoratorTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Plugin/CacheDecoratorTest.php @@ -122,7 +122,7 @@ public function testClearCachedDefinitions() { $this->discovery->setDefinition('banana', $this->expectedDefinitions['banana']); // Check that the new definition is not found. - $definition = $this->discovery->getDefinition('banana'); + $definition = $this->discovery->getDefinition('banana', FALSE); $this->assertNull($definition, 'Newly added definition is not found.'); // Clear cached definitions, and check that the new definition is found.