In Drupal\Core\Plugin\Factory\ContainerFactory we find this snippet:

  public function createInstance($plugin_id, array $configuration) {
    $plugin_definition = $this->discovery->getDefinition($plugin_id);
    $plugin_class = static::getPluginClass($plugin_id, $plugin_definition);
    // ...

Problem. getDefinition() is defined to allow a NULL to be returned if the plugin is not properly defined. That results, down in getPluginClass() somewhere, in this error:

Drupal\Component\Plugin\Exception\PluginException: The plugin (drupalgotchi_set_attention) did not specify an instance class. in Drupal\Component\Plugin\Factory\DefaultFactory::getPluginClass() (line 62 of /stor/sandboxes/garfield/wscci/www/wscci/core/lib/Drupal/Component/Plugin/Factory/DefaultFactory.php).

That's very unhelpful. ContainerFactory should handle "no plugin defined, sorry" itself rather than assuming the return is not NULL when NULL is a legal return.

There's 2 options:
1) Throw a meaningful exception with a useful message that points people toward what they probably did wrong. (new PluginNotDefinedException('That plugin ID was not found, perhaps you screwed up X and Y?'))

2) return NULL right here.

I favor the first option, as the second doesn't do anything but shift the responsibility for handling the NULL up to the caller, who shouldn't have to deal with that. The plugin not being defined is an exceptional case. Meaningful error messages FTW.


jlindsey15’s picture

Assigned: Unassigned » jlindsey15

Got it.

tim.plunkett’s picture

Status: Active » Closed (duplicate)