Problem/Motivation
Because of the way context mappings are currently handled, you cannot reuse a value to satisfy multiple required contexts.
Take for example, a plugin that needs two different user contexts.
If you were on a /user/{user} page, the contexts could be mapped to the user viewing the page, and the user whose page you are viewing, and that will work.
But if both contexts were mapped to the same user, we'll lose all but one of the mappings due to an array_flip()
.
Proposed resolution
Flip the context mapping handling so that it is always keyed by a unique value, not one that could be duplicated.
Remaining tasks
N/A
User interface changes
N/A
API changes
ContextAwarePluginInterface::getContextMapping() and ContextAwarePluginInterface::setContextMapping() are now flipped.
Before they were keyed by the Context object's name and the values were the plugin context definition names.
Now they are keyed by the plugin context definition names with the values of the Context object's names.
Similarly, the optional $mappings parameter for ContextHandlerInterface::applyContextMapping() is flipped.
Comment | File | Size | Author |
---|---|---|---|
#8 | 2378585-interdiff.txt | 1.39 KB | EclipseGc |
#8 | 2378585-8.patch | 10.64 KB | EclipseGc |
#7 | 2378585-interdiff.txt | 1.32 KB | EclipseGc |
#7 | 2378585-7.patch | 10.51 KB | EclipseGc |
#3 | 2378585-context-3.patch | 10.26 KB | tim.plunkett |
Comments
Comment #1
tim.plunkettThe tests add a condition that takes two users as context.
Comment #3
tim.plunkettWas accidentally running each test twice. Refactored that a bit (I was borrowing unnecessary parts from UserRoleConditionTest).
Comment #4
tim.plunkettThis is the crux of the patch. array_flip is unsafe here since the values may not be unique, and to work around that removal we iterate over the expected context names, instead of over the actual context objects.
Comment #5
EclipseGc CreditAttribution: EclipseGc commentedThis certainly addresses a bunch of my concerns. RTBC
Eclipse
Comment #6
alexpottWe've updated the names in #2339151: Conditions / context system does not allow for multiple configurable contexts, eg. language types but we didn't update this help text. Let's do this here as we're changing it.
current_user
should beuser.current_user
Unrelated change
Comment #7
EclipseGc CreditAttribution: EclipseGc commentedUpdated the documentation per your request. The changes to the tests are absolutely related since the mapping going on there has been flip-flipped by this patch.
Eclipse
Comment #8
EclipseGc CreditAttribution: EclipseGc commentedscrewed up the 80 char limit.
Eclipse
Comment #9
tim.plunkettYeah for #6.2 it may look like I'm just changing the syntax, but I'm also flipping the key/value, because of the removed array_flip.
Nice catch on the docs update.
Comment #10
alexpottThis issue addresses a major bug and is allowed per https://www.drupal.org/core/beta-changes. Committed fda020e and pushed to 8.0.x. Thanks!