Problem/Motivation

EntityContextDefinition was introduced to allow easier DX when dealing with a specific entity or entity type.
However, it will not match a generic requirement for any entity.

Proposed resolution

Expand the data type checking to allow variants (aka derivatives) to satisfy a more generic context.

Remaining tasks

N/A

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tim.plunkett created an issue. See original summary.

tim.plunkett’s picture

Here's a failing test

tim.plunkett’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 2: 3008431-ecd-2.patch, failed testing. View results

phenaproxima’s picture

Status: Needs work » Needs review
FileSize
3.95 KB
1.44 KB

First attempt at a fix. It passes tests locally, but it sorta breaks the encapsulation of EntityContextDefinition. Sigh...this is a logical pickle.

phenaproxima’s picture

Refactored the logic a bit at @tim.plunkett's suggestion so that we can support fuzzy matching without hard-coding the 'entity' string into ContextDefinition.

tim.plunkett’s picture

Status: Needs review » Reviewed & tested by the community

This is beautiful :chef kisses fingers:

tim.plunkett’s picture

Assigned: Unassigned » tim.plunkett
Status: Reviewed & tested by the community » Needs work

Actually, unRTBCing for now as the test coverage is in the wrong place and the IS needs updating.

tim.plunkett’s picture

Title: A context object for a given entity will not match the requirement for any entity » A context object for a specific entity type will not match a generic requirement for any entity
Issue summary: View changes
Status: Needs work » Needs review
FileSize
5.7 KB
8.03 KB
6.12 KB

The last patch was actually a step too far, because if the requirement is for entity:node, then a user entity should not satisfy that. Only when the requirement is for the generic entity should the entity type not matter.

Fixed the logic, expanded the entity tests to include that reverse case, and wrote generic tests for a non-entity case.

The last submitted patch, 9: 3008431-context-9-FAIL.patch, failed testing. View results

tim.plunkett’s picture

Assigned: tim.plunkett » Unassigned

Can't reRTBC anymore, oh well :)

tim.plunkett’s picture

andypost’s picture

EclipseGc’s picture

Status: Needs review » Reviewed & tested by the community

This is a huge win and clearly a big improvement over the current logic.

RTBC!

Eclipse

Wim Leers’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php
    @@ -312,6 +312,29 @@ public function getDataDefinition() {
    +      $this_type === 'any' ||
    

    As a non-expert of the Context system: why is it okay for $this_type === 'any' but not $that_type === 'any'?

    I think it's because $this is a context definition and $that is a concrete context?

  2. +++ b/core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php
    @@ -319,7 +342,7 @@ public function isSatisfiedBy(ContextInterface $context) {
         // If the data types do not match, this context is invalid unless the
         // expected data type is any, which means all data types are supported.
    

    This comment now belongs on the new helper method.

phenaproxima’s picture

As a non-expert of the Context system: why is it okay for $this_type === 'any' but not $that_type === 'any'?

I'm not entirely sure, but I think your appraisal of it makes sense. In any event, we're just preserving the logic that was already there.

This comment now belongs on the new helper method.

+1 for this.

Wim Leers’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
1.51 KB
8.01 KB

#16: cool, done.

Moving the comment pretty much answers my own question :)

+++ b/core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php
@@ -312,6 +312,29 @@ public function getDataDefinition() {
+   * Checks if this definition's data type matches that of another context.

Nit: "another" implies $this is a context too, which it isn't. Clarified.

tim.plunkett’s picture

Thanks @Wim Leers for those clarifying changes. RTBC +1

alexpott’s picture

The patch look great; the test coverage also looks complete and the change makes sense. The only thing I pondered was whether we need to add another protected method here.

The only question I have is whether or not ContextDefinition often extended in contrib / custom? The reason I ask is because adding a new protected method is not strictly necessary but I do concede it makes the code easy to understand. Inlining the code will definitely lose the nice clarity of $this_type vs $that_type.

phenaproxima’s picture

Can't speak for custom code, but Russian Contrib Grep says it has been extended, albeit not often: http://grep.xnddx.ru/search?text=extends+ContextDefinition&filename=

So this will certainly allow subclasses some greater flexibility when it comes to matching other contexts.

tim.plunkett’s picture

I much prefer the readability of the method. I could also see it being useful for subclasses, though they may be rare.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Chose not to backport for the time being due to the minuscule risk of disruption due to the new method. If people feel otherwise then I might be convinced otherwise :)

Committed 9601416 and pushed to 8.7.x. Thanks!

  • alexpott committed 9601416 on 8.7.x
    Issue #3008431 by tim.plunkett, phenaproxima, Wim Leers: A context...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

tim.plunkett’s picture

Version: 8.7.x-dev » 8.6.x-dev
Status: Closed (fixed) » Reviewed & tested by the community
Issue tags: +Contributed project blocker

Upon reflection, this would be very nice to have backported. It will enable Lightning to backport parts of 8.7 Layout Builder.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 17: 3008431-context-17.patch, failed testing. View results

tim.plunkett’s picture

Status: Needs work » Reviewed & tested by the community

Retested the patch from #17 on 8.6

xjm’s picture

Version: 8.6.x-dev » 8.7.x-dev
Status: Reviewed & tested by the community » Postponed

So whether to backport this is a release management decision (the risk of the disruption vs. the benefit to contrib).

After discussing this with @tim.plunkett, I don't think we should backport these. The benefit to the affected conrib project is marginal -- it also still needs to maintain a patch for the chain of issues for #2976148: Layout-based entity rendering should delegate to the correct section storage instead of hardcoding to either defaults or overrides, which are quite disruptive and definitely not backportable.

So let's keep these in 8.7.x only unless there's something I've overlooked. Thanks!

xjm’s picture

Status: Postponed » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.