Problem/Motivation
The documentation for the constructor in \Drupal\Component\Plugin\ContextAwarePluginBase
indicates that the context key can be used to define context values in the configuration array. The call to createContextFromConfiguration
supports this, however the functionality does not work as intended due to a typo.
I came across this issue while developing a custom context aware plugin type that makes use of derivatives each with a unique context. In order to supply the context I passed it in via the configuration array however this then lead to an unexpected error when trying to access the value that had apparently not been set.
Proposed resolution
Change the constructor in core/lib/Drupal/Component/Plugin/ContextAwarePluginBase.php
by removing the rogue "s" such that it reads $this->context
instead of $this->contexts
.
In the meantime until the bug is fixed developers will need to override the constructor to manually call createContextFromConfiguration
.
Comment | File | Size | Author |
---|---|---|---|
#15 | drupal_2623050_15.patch | 4.97 KB | Xano |
#10 | interdiff.txt | 2.35 KB | Xano |
Comments
Comment #2
rakesh.gectcrLet me check the , will the patch get failed or not ?
Comment #3
BerdirI can confirm that there clearly is a mismatch but apparently nothing in core uses this. We definitely need some tests to show how this is supposed to work.
Comment #4
XanoA related problem here is that plugin configuration is internal. Constructors shouldn't document its structure, as the allowed configuration is equal to whatever
ConfigurablePluginInterface::getConfiguration()
returns: an array with arbitrary content, as decided by the plugin itself. This is what caused this problem to be hidden in the first place.This patch builds upon @rakesh.gectcr's original solution, but also deprecates the aforementioned behavior, and documents the better alternative approach.
Comment #6
XanoComment #8
XanoComment #9
tim.plunkettThis is explicitly testing the constructor injection of context. Please revert.
Comment #10
XanoComment #12
cilefen CreditAttribution: cilefen commentedI don't think a Novice can review this.
Comment #15
XanoRe-roll.
Comment #17
benjifisherI noticed this typo, then found that this issue had already been posted. Fixing the typo would be a one-line change as in the original patch (although the test coverage is a lot more than that). So I am curious why you replace
with
This does the following:
unset()
from before the parent constructor to after (at which point it is irrelevant).$this->context
inside anif
block.Of these, (1) is the original point of this issue. Does (2) make a difference? You explained your reasons for (3) in #4. I do not know enough to agree or disagree with that decision, but it seems like scope creep for this issue. I also do not know whether (4) is a good idea, but it is a change, and might break some code.
The deprecation notice is not translatable. Also, there is a typo (or grammatical mistake). If we decide to keep it, I suggest something like
Comment #28
LendudeThe whole BC dance and fix was done here #3080631: Context values passed in configuration to ContextAwarePluginBase are disregarded.