We need an additional logic check to prevent an unnecessary foreach statement.

CommentFileSizeAuthor
derivativefix.patch1.37 KBeclipsegc

Comments

eclipsegc’s picture

Issue tags: +Plugin system

adding tag

neclimdul’s picture

shouldn't it be foreach-ing over an empty array which is basically the same?

eclipsegc’s picture

Yeah, except then we expect every Discovery class and decorator to be perfect, which in a perfect world would be fine, but I figured I'd just anticipate people screwing it up.

aspilicious’s picture

The one thing I don't get is that it doens't return an empty array. I'm testing the block patch. Seems like a bug in getDerativeDefintions() method

aspilicious’s picture

+class CategoryBlock implements DerivativeInterface {
+ protected $derivatives;

Initialize this with an array() and problems probably are gone. We should learn peope to use the correct syntax...

boztek’s picture

I tested both approaches and initialising $derivatives works but for the purposes of http://drupal.org/node/1535868 it looks like we would need to initialise at least CategoryBlock, LanguageBlock and SystemMenuBlock in the same way.

i.e.

 class CategoryBlock implements DerivativeInterface {
-  protected $derivatives;
+  protected $derivatives = array();

 class LanguageBlock implements DerivativeInterface {
-  protected $derivatives;
+  protected $derivatives = array();

 class SystemMenuBlock implements DerivativeInterface {
-  protected $derivatives;
+  protected $derivatives = array();

With that in mind the original patch makes sense.

jibran’s picture

Issue tags: -Plugin system

derivativefix.patch queued for re-testing.

xano’s picture

Yeah, except then we expect every Discovery class and decorator to be perfect, which in a perfect world would be fine, but I figured I'd just anticipate people screwing it up.

It's not our problem if people implement an interface and don't stick to the required return value types. We should not make our code less readable to accommodate other people's possible mistakes, that will cause a Invalid argument supplied for foreach() error anyway.

dawehner’s picture

Issue summary: View changes
+++ b/core/lib/Drupal/Component/Plugin/Discovery/DerivativeDiscoveryDecorator.php
@@ -67,10 +67,11 @@ class DerivativeDiscoveryDecorator implements DiscoveryInterface {
-        $derivative_definitions = $derivative_fetcher->getDerivativeDefinitions($plugin_definition);
-        foreach ($derivative_definitions as $derivative_id => $derivative_definition) {
-          $plugin_id = $this->encodePluginId($base_plugin_id, $derivative_id);
-          $plugin_definitions[$plugin_id] = $derivative_definition;
+        if ($derivative_definitions = $derivative_fetcher->getDerivativeDefinitions($plugin_definition)) {
+          foreach ($derivative_definitions as $derivative_id => $derivative_definition) {
+            $plugin_id = $this->encodePluginId($base_plugin_id, $derivative_id);
+            $plugin_definitions[$plugin_id] = $derivative_definition;
+          }

Can we test this change?

dawehner’s picture

Status: Needs review » Needs work

.

xano’s picture

Status: Needs work » Closed (won't fix)

just anticipate people screwing it up.

Let's not hide it when people make coding errors. People should discover errors and fix them.