Problem/Motivation

A plugin discovered by any means may have in it's definition a 'derivative' key that should represent a valid class that implments Drupal\Component\Plugin\Derivative\DerivativeInterface

Currently neither \Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator nor \Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator make sure that the fetchers implement this interface.

Proposed resolution

Add a check that each derivative fetcher implements the interface before instantiating it.

API changes

None

Comments

pwolanin’s picture

Status: Active » Needs review
StatusFileSize
new2.59 KB
pwolanin’s picture

StatusFileSize
new3.54 KB

feedback from dawehner - make a helper method instead of inlining the code in each class.

dawehner’s picture

+++ b/core/lib/Drupal/Component/Plugin/Discovery/DerivativeDiscoveryDecorator.php
@@ -148,6 +150,27 @@ protected function getDerivativeFetcher($base_plugin_id, array $base_definition)
+   * @throws Drupal\Component\Plugin\Exception\PluginException

Let's add leading "\" here as well.

neclimdul’s picture

Might be a good place for an explicit exception that extends the base plugin exception.

pwolanin’s picture

StatusFileSize
new4.71 KB
new6.44 KB

Here's fixes, a new Exception and an added test, though maybe the test should be in the base class test

neclimdul’s picture

+++ b/core/lib/Drupal/Component/Plugin/Discovery/DerivativeDiscoveryDecorator.php
@@ -152,10 +152,13 @@ protected function getDerivativeFetcher($base_plugin_id, array $base_definition)
+   * @throws \Drupal\Component\Plugin\Exception\PluginException
    *   Thrown if the class in the definition does not implement the expected

Woops, wrong exception name.

pwolanin’s picture

Issue tags: +PHPUnit
StatusFileSize
new5.97 KB
new7.43 KB

Fixes documentation and moves the testing to a new unit test for the base DerivativeDiscoveryDecorator class.

twistor’s picture

+++ b/core/lib/Drupal/Component/Plugin/Exception/InvalidDerivativeClassException.php
@@ -0,0 +1,13 @@
+/**
+ * Exception class to be thrown if a plugin tries to use derivative class that
+ * is invalid, such as not implementing the expected interface.
+ */
+class InvalidDerivativeClassException extends PluginException { }

*tries to use an invalid derivative class

pwolanin’s picture

StatusFileSize
new740 bytes
new7.37 KB

thanks, fixed.

dawehner’s picture

+++ b/core/lib/Drupal/Component/Plugin/Discovery/DerivativeDiscoveryDecorator.php
@@ -2,11 +2,13 @@
- * Definition of Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator.
+ * Definition of \Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator.

+++ b/core/lib/Drupal/Component/Plugin/Exception/InvalidDerivativeClassException.php
@@ -0,0 +1,12 @@
+ * Definition of \Drupal\Component\Plugin\Exception\InvalidDerivativeClassException.

Fixing this is kind of out of scope, but if we do, we should do it properly: "Contains \..."

pwolanin’s picture

StatusFileSize
new608 bytes
new7.36 KB

Seems silly to have a separate doc issue if we're changing other parts of the class.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Sure sure, just wanted to do it properly here.

pwolanin’s picture

Issue tags: -PHPUnit

#11: 2078879-11.patch queued for re-testing.

pwolanin’s picture

Issue tags: +PHPUnit

#11: 2078879-11.patch queued for re-testing.

neclimdul’s picture

Don't have an answer but should be public method be clear that it can throw an exception since we're not catching it internally?

pwolanin’s picture

Yes, we should have an @throws I think.

So you mean on the getDefinitions() method?

neclimdul’s picture

Status: Reviewed & tested by the community » Needs work

Yes, that's what I meant. Lets go ahead and do it then.

dawehner’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new1.33 KB
new8.34 KB

So we need kind of something like this?

dawehner’s picture

Status: Reviewed & tested by the community » Needs review

.

neclimdul’s picture

Status: Needs review » Reviewed & tested by the community

Exactly. Making assumptions about the bot return since this is a doc change and bumping back to RTBC. Everything thing else looks awesome.

pwolanin’s picture

Status: Reviewed & tested by the community » Needs work

I think "class in the definition" is unclear, the "derivative fetcher class in a plugin definition"?

e.g.

+   * @throws \Drupal\Component\Plugin\Exception\InvalidDerivativeClassException
+   *   Thrown if the derivative fetcher class in the definition does not implement the
+   *   expected \Drupal\Component\Plugin\Derivative\DerivativeInterface
dawehner’s picture

Status: Needs work » Needs review
StatusFileSize
new1.3 KB
new8.5 KB
pwolanin’s picture

StatusFileSize
new2.65 KB
new8.76 KB

Let's make the doc consistent and slightly more compact.

neclimdul’s picture

Status: Needs review » Reviewed & tested by the community

Good enough for me. Back to RTBC.

pwolanin’s picture

Issue tags: -PHPUnit

#23: derivative-2078879-23.patch queued for re-testing.

pwolanin’s picture

Issue tags: +PHPUnit

#23: derivative-2078879-23.patch queued for re-testing.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

Patch no longer applies.

xano’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new9.02 KB

Re-roll.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Back to RTBC

xano’s picture

#28: drupal_2078879_28.patch queued for re-testing.

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.x, thanks!

tstoeckler’s picture

+++ b/core/lib/Drupal/Component/Plugin/Discovery/DerivativeDiscoveryDecorator.php
@@ -133,14 +143,18 @@ protected function encodePluginId($base_plugin_id, $derivative_id) {
+   * @return \Drupal\Component\Plugin\Derivative\DerivativeInterface|null
++   *   A DerivativeInterface or NULL if none exists for the plugin.
++   *
++   * @throws \Drupal\Component\Plugin\Exception\InvalidDerivativeClassException
++   *   Thrown if the 'derivative' class specified in the plugin definition does
++   *   not implement \Drupal\Component\Plugin\Derivative\DerivativeInterface.

Can we get a quick follow-up for the wrong patch-style documentation here? Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.