Problem/Motivation
PluginManagerBase:
public function getInstance(array $options) {
return $this->mapper->getInstance($options);
}
Try drush ev '\Drupal::service("plugin.manager.action")->getInstance([])'
and you will get
PHP Fatal error: Call to a member function getInstance() on null in /home/chx/www/d8/core/lib/Drupal/Component/Plugin/PluginManagerBase.php on line 97
By no means this is just the action manager. There are only 7 plugin managers overriding this, all the others will get this.
Proposed resolution
As a quick placebo, implement a MapperInterface class which throws an exception and set that as the default mapper in PluginManagerBase. That still leads to a fatal but it's less mysterious and more catchable.
Remaining tasks
User interface changes
API changes
Data model changes
Comment | File | Size | Author |
---|---|---|---|
#40 | interdiff.txt | 2.48 KB | longwave |
#40 | 2625554-40.patch | 4 KB | longwave |
Comments
Comment #2
dawehnerIMHO we could implement a basic implementation which calls out to
createInstance()
if possible (when the ID is set).Comment #3
tim.plunkettSee also #1894130: Make \Drupal\Core\Plugin\Mapper\MapperInterface optional for plugin managers
Comment #4
XanoThis is not doable. We don't know anything about the structure of
$options
. Writing code that pretends we know this, is just giving a bad example.This patch implements @chx's original suggestion, and slightly improves the documentation on
PluginManagerBase
.Comment #5
tim.plunkettSure, I think that's an improvement. But it's not a major bug.
Comment #6
chx CreditAttribution: chx at Smartsheet commentedPluginManagerInterface::class, what about get_called_class() ?
Comment #7
tim.plunkettstatic::class
could work, but I assume you meant PluginManagerInterface::class because that's where the docs live?Comment #8
Wim LeersComment #9
Wim LeersMarked #2579235: Resource plugin manager needlessly calls wrong method to instantiate plugins and #2503355: Remove ResourcePluginManager::getInstance() as duplicates of this. Note that we should also remove
\Drupal\rest\Plugin\Type\ResourcePluginManager::getInstance()
as part of this, i.e. the changes in #2579235: Resource plugin manager needlessly calls wrong method to instantiate plugins should be included here.Comment #11
Wim LeersStill a problem.
Comment #12
XanoI hardcoded the class name exactly for that reason. I have updated the message a little to clear up the confusion.
@Wim Leers: in #2503355-16: Remove ResourcePluginManager::getInstance() you said removing it breaks BC, which I agree with, so I think we should keep it as it is.
Comment #13
twistor CreditAttribution: twistor as a volunteer commentedShould we use
BadMethodCallException
?Comment #14
dawehnerIN my world it seems that getInstance is something which would be deprecated and removed from the generic interface in D9. Conceptually
getInstance()
requires domain specific knowledge and just, as we see here, be implemented in a generic way. Given that, adding domain specific methods onto plugin managers seems to be the next logical step in a future design.Comment #15
Xano@dawehner: Are you suggesting we do that in follow-up issues?
Comment #20
andypostComment #21
borisson_I'm not sure if we should add the exception in code like that or we should add a trigger error like we usually do. This is something that's really broken.
Comment #22
dawehnerShouldn't we check the mapper first?
Comment #24
jhedstromNW for #21 and #22.
Comment #25
XanoIs that different from just allowing the current code to trigger a fatal error? Do we want to allow continued code execution?
This patch implements the feedback from #13 and #22. I also replaced the URL to the other issue with the URL to the change record I just created. I used the term "discouraged" since I don't think we can fully deprecate this (on account of
MapperInterface
being meh, but not actually deprecated), but perhaps we can improve the wording over time :)Comment #26
borisson_That's a reasonable explenation, this is a lot better than the generic error that previously happened.
Comment #28
XanoRe-testing since this looks like an infra failure.
Comment #29
alexpottThe exception can be tested and then we can be sure the the helpful information of which class is pointed out. Also linking to an issue in the exception message feels odd. Is there not something better we can say / link to?
Comment #30
XanoComment #31
longwaveShift key error in wilLReturn.
Comment #32
alexpottWe don't use this annotation any more.
Comment #33
googletorp CreditAttribution: googletorp as a volunteer and at Reveal IT commentedFixed issues raised from #31 and #32
Comment #35
googletorp CreditAttribution: googletorp as a volunteer and at Reveal IT commentedAdd missing PhpunitCompatibilityTrait from 33.
Comment #37
googletorp CreditAttribution: googletorp as a volunteer and at Reveal IT commentedFinally dawned on me, that test errors are due to issue with the way I created the patch, where the new file with StubPluginManagerBaseWithMapper was missing in the diff, should hopefully work now.
Comment #38
borisson_All issues that were raised are fixed now, to RTBC this goes.
Comment #39
alexpottHmmm re-reading this exception message gives me pause. The method is definitely implemented. I think better language would be
does not support this method
Let's not use this trait in component code. Every where in component tests we do an if. This introduces a dependency on Drupal's test code that we don't want.
Comment #40
longwaveComment #41
borisson_The remarks in #39 were fixed.
Comment #42
googletorp CreditAttribution: googletorp as a volunteer and at Reveal IT commented+1 Would have done this myself, just not had the time for it. This should be RTBC.
Comment #43
alexpottCommitted and pushed 56554f5da5 to 8.7.x and e863ce259a to 8.6.x. Thanks!
Backported to 8.6.x since, whilst this does throw a new exception, it is only in the case that would have errored already and the new error is more helpful.
Fixed unused use on commit.
Comment #47
quietone CreditAttribution: quietone at PreviousNext commentedpublish change record