Problem/Motivation

In TypedData land, you deal with DataDefinition objects:

DataDefinition::create('string');
EntityDataDefinition::create('node');

With ContextDefinitions, you don't have that luxury:

new ContextDefinition('string');
new ContextDefinition('entity:node');

With #2671964: ContextHandler cannot validate constraints, there is new entity-specific logic that should not be intermixed with the generic ContextDefinition class.

Proposed resolution

Deprecate using the constructor directly (is this possible?)
Add ::create()
Use EntityDataDefinition where possible
Remove all strpos() checks for 'entity:'

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#67 Screen Shot 2018-07-16 at 13.20.25.png23.69 KBmtodor
#61 interdiff-2932462-58-61.txt1.85 KBphenaproxima
#61 2932462-61.patch64.48 KBphenaproxima
#60 interdiff.txt3.31 KBtim.plunkett
#59 interdiff-2932462-56-58.txt28.84 KBphenaproxima
#58 2932462-58.patch67.06 KBphenaproxima
#56 2932462-entitycontext-56-interdiff.txt664 bytestim.plunkett
#56 2932462-entitycontext-56.patch63.66 KBtim.plunkett
#52 interdiff-2932462-48-52.txt2.09 KBphenaproxima
#52 2932462-52.patch66.97 KBphenaproxima
#48 interdiff-2932462-46-48.txt4.09 KBphenaproxima
#48 2932462-48.patch66.19 KBphenaproxima
#46 interdiff-2932462-44-46.txt9.42 KBphenaproxima
#46 2932462-46.patch64.42 KBphenaproxima
#44 interdiff-2932462-42-44.txt13.76 KBphenaproxima
#44 2932462-44.patch63.64 KBphenaproxima
#42 interdiff-2932462-40-42.txt2.56 KBphenaproxima
#42 2932462-42.patch62.71 KBphenaproxima
#40 interdiff-2932462-37-40.txt1.04 KBphenaproxima
#40 2932462-40.patch60.27 KBphenaproxima
#37 interdiff-2932462-33-37.txt12.84 KBphenaproxima
#37 2932462-37.patch59.1 KBphenaproxima
#33 interdiff-2932462-28-33.txt8.88 KBphenaproxima
#33 2932462-33.patch44.64 KBphenaproxima
#28 interdiff-2932462-27-28.txt1.33 KBphenaproxima
#28 2932462-28.patch39.58 KBphenaproxima
#27 interdiff-2932462-22-27.txt5.63 KBphenaproxima
#27 2932462-27.patch41.11 KBphenaproxima
#22 interdiff-2932462-19-22.txt24.69 KBphenaproxima
#22 2932462-22.patch36.13 KBphenaproxima
#19 interdiff-2932462-18-19.txt5.18 KBphenaproxima
#19 2932462-19.patch14.26 KBphenaproxima
#18 interdiff-2932462-14-18.txt1.83 KBphenaproxima
#18 2932462-18.patch10.68 KBphenaproxima
#14 2932462-13-14-interdiff.txt970 bytesjaperry
#14 2932462-14.patch9.45 KBjaperry
#13 2932462-13.patch8.52 KBjaperry
#12 2932462-5-10-interdiff.txt3.15 KBjaperry
#12 2932462-10.patch10.66 KBjaperry
#9 2932462-9.patch9.43 KBjaperry
#5 2932462-5.patch7.75 KBphenaproxima
#4 2932462-4.patch69.79 KBphenaproxima
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tim.plunkett created an issue. See original summary.

larowlan’s picture

Blocker is in

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

phenaproxima’s picture

Status: Active » Needs review
Issue tags: +DX (Developer Experience)
FileSize
69.79 KB

Here's a first, very rough attempt. Needs a lot of work, but I want @tim.plunkett or someone to validate the approach.

phenaproxima’s picture

Ugh, sorry, my changes to composer.json were in there too. Here's the real thing.

tim.plunkett’s picture

Issue tags: +Needs tests

This looks GREAT.
I think it could use some dedicated tests, and it needs docblocks and such.
Only one point below

  1. +++ b/core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php
    @@ -348,14 +348,6 @@ protected function getSampleValues() {
    -    // @todo Move the entity specific logic out of this class in
    -    //   https://www.drupal.org/node/2932462.
    -    // If the data type is an entity, manually add one to the constraints array.
    -    if (strpos($this->getDataType(), 'entity:') === 0) {
    -      $entity_type_id = substr($this->getDataType(), 7);
    -      $constraint_definitions['EntityType'] = ['type' => $entity_type_id];
    -    }
    -
    

    <3

  2. +++ b/core/lib/Drupal/Core/Plugin/Context/EntityContextDefinition.php
    @@ -0,0 +1,39 @@
    +    return new static($entity->getEntityType());
    

    Shouldn't this be return static::fromEntityType($entity->getEntityType());?

  3. +++ b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php
    @@ -150,9 +149,7 @@ public function buildMultiple(array $entities) {
    -        $contexts['layout_builder.entity'] = new Context(new ContextDefinition("entity:{$entity->getEntityTypeId()}", new TranslatableMarkup('@entity being viewed', ['@entity' => $entity->getEntityType()->getLabel()])), $entity);
    +        $contexts['layout_builder.entity'] = EntityContext::fromEntity($entity);
    

    <3

The last submitted patch, 4: 2932462-4.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 5: 2932462-5.patch, failed testing. View results

japerry’s picture

Status: Needs work » Needs review
FileSize
9.43 KB

Fixes from Tim's suggestions.

tim.plunkett’s picture

  1. +++ b/core/lib/Drupal/Core/Plugin/Context/EntityContext.php
    @@ -0,0 +1,39 @@
    +   * Get a context definition from an entity type.
    ...
    +   * Get a context object from an entity.
    

    Gets

  2. +++ b/core/lib/Drupal/Core/Plugin/Context/EntityContext.php
    @@ -0,0 +1,39 @@
    +   * @param $entity_type
    +   *   Entity type in which a definition will be derived.
    ...
    +  public static function fromEntityType($entity_type) {
    

    Is this a string or an object? Or both?

  3. +++ b/core/lib/Drupal/Core/Plugin/Context/EntityContextDefinition.php
    @@ -0,0 +1,58 @@
    +    if (!($entity_type instanceof EntityTypeInterface)) {
    +      $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type);
    +    }
    

    Oh, both. Hmm. This might be a bit of overkill.

    If this switches to the object, please typehint EntityTypeInterface

Status: Needs review » Needs work

The last submitted patch, 9: 2932462-9.patch, failed testing. View results

japerry’s picture

Status: Needs work » Needs review
FileSize
10.66 KB
3.15 KB
japerry’s picture

bah sorry 10 is wrong here is what the interdiff was actually made against.

japerry’s picture

The last submitted patch, 12: 2932462-10.patch, failed testing. View results

The last submitted patch, 13: 2932462-13.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 14: 2932462-14.patch, failed testing. View results

phenaproxima’s picture

Status: Needs work » Needs review
FileSize
10.68 KB
1.83 KB

This should bring the tests back to green.

phenaproxima’s picture

And this moves the entity-specific stuff out of getSampleValues(), thus cleaning up ContextDefinition completely. Hopefully tests pass.

phenaproxima’s picture

Issue tags: -Needs tests

Given the changes I had to make to the tests, we are now making a couple of explicit calls to EntityContextDefinition and EntityContext. I think that is probably good enough for test coverage.

tim.plunkett’s picture

Issue tags: +Needs change record
  1. +++ b/core/lib/Drupal/Core/Plugin/Context/EntityContextDefinition.php
    @@ -0,0 +1,90 @@
    +    $this->addConstraint('EntityType', [
    +      'type' => $this->getEntityTypeId(),
    +    ]);
    

    Should this check if (!$this->getConstraint('EntityType')) first?

  2. +++ b/core/lib/Drupal/Core/Plugin/Context/EntityContextDefinition.php
    @@ -0,0 +1,90 @@
    +    return new EntityAdapter($definition);
    

    This needs to be a yield, this method has no return value

  3. +++ b/core/lib/Drupal/Core/Plugin/Context/EntityContextDefinition.php
    @@ -0,0 +1,90 @@
    +   *   The entity type providing the context
    

    Missing trailing .

  4. +++ b/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php
    --- a/core/tests/Drupal/Tests/Core/Plugin/Context/ContextDefinitionIsSatisfiedTest.php
    +++ b/core/tests/Drupal/Tests/Core/Plugin/Context/ContextDefinitionIsSatisfiedTest.php
    

    This test is covering \Drupal\Core\Plugin\Context\ContextDefinition, but all the entity-specific stuff has moved to a new class. So IMO a new test is needed, and anything entity-related should be removed from this test

Also, I think this deserves a CR.

phenaproxima’s picture

Thanks! All feedback fixed, except for the change record.

tim.plunkett’s picture

Status: Needs review » Reviewed & tested by the community

Looks great! Thanks.
Just need the CR now.

phenaproxima’s picture

Issue tags: -Needs change record

Change record written: https://www.drupal.org/node/2976400. Cheers!

andypost’s picture

+++ b/core/lib/Drupal/Core/Plugin/Context/EntityContextDefinition.php
@@ -0,0 +1,93 @@
+    yield new EntityAdapter($definition);
+    return;
+  }

return could be removed

webchick’s picture

Status: Reviewed & tested by the community » Needs work

@phenaproxima and I grepped contrib for instances of Context(ContextDefinition('entity and there are quite a few instances, including one in GDPR module (not like anyone's gonna be using that! ;))

Unfortunately this patch introduces a BC break because consumers of the API aren't sub-classes and therefore can't use protected methods. So we need some kind of BC shim along with this patch to avoid breaking modules in 8.6. Sorry. :(

phenaproxima’s picture

Status: Needs work » Needs review
FileSize
41.11 KB
5.63 KB

Here's an attempt to implement a BC layer.

phenaproxima’s picture

Oops, forgot to revert my changes to the Context class. I don't think we need a BC layer in Context (yet), because EntityContext is just a factory. The context definition is the important thing.

phenaproxima’s picture

I think we'll need to update the CR to explain the deprecation.

tim.plunkett’s picture

The BC looks good.

  1. +++ b/core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php
    @@ -105,6 +121,18 @@ public static function create($data_type = 'any') {
    +    if (strpos($data_type, 'entity:') === 0) {
    

    I think this needs a !instanceof check for EntityContextDefinition, otherwise, when the new class is used, this code will still fire.

  2. +++ b/core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php
    @@ -309,33 +337,10 @@ public function isSatisfiedBy(ContextInterface $context) {
    -      return;
    +    if ($this->entityContextDefinition) {
    +      yield $this->entityContextDefinition->getSampleValues();
         }
    

    There is a missing return after the first yield.

  3. +++ b/core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php
    @@ -72,6 +67,27 @@ class ContextDefinition implements ContextDefinitionInterface {
    +   * This property should be kept private so that it is only accessible to this
    +   * class for backwards compatibility reasons. It will be removed in Drupal 9.
    

    I think this needs @deprecated or something, so that we remember to actually remove it

  4. +++ b/core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php
    @@ -72,6 +67,27 @@ class ContextDefinition implements ContextDefinitionInterface {
    +   * @see https://www.drupal.org/project/drupal/issues/2932462
    +   * @see https://www.drupal.org/node/2976400
    

    Please pick one format for the d.o links

The last submitted patch, 27: 2932462-27.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 28: 2932462-28.patch, failed testing. View results

phenaproxima’s picture

Status: Needs work » Needs review
FileSize
44.64 KB
8.88 KB

Expanded the test coverage and (hopefully) added serialization support.

tim.plunkett’s picture

+++ b/core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php
@@ -302,7 +303,12 @@
-      $values = $definition->getSampleValues();
+      if ($this->entityContextDefinition) {
+        $values = $this->entityContextDefinition->getSampleValues();
+      }
+      else {
+        $values = $definition->getSampleValues();
+      }

@@ -337,10 +343,6 @@
   protected function getSampleValues() {
-    if ($this->entityContextDefinition) {
-      yield $this->entityContextDefinition->getSampleValues();
-    }
-
     yield $this->getTypedDataManager()->create($this->getDataDefinition());

This change feels wrong to me. Because now every call to getSampleValues would need this...

What's the reason we can't use the old way? Or yield from if that's needed?

phenaproxima’s picture

What's the reason we can't use the old way? Or yield from if that's needed?

We cannot use yield from because it's PHP 7 only. I tried to do things the old way, with a test, but I simply could not get it to delegate to the inner generator. It would always return a generator-within-a-generator, which means the calling code would need to have a nested foreach loop to iterate over it. There's a library out there on GitHub that polyfills generator delegation in PHP 5, but it'd be another core Composer dependency, so that's a no-go.

Thus, after a solid hour of trying, I settled on this being the best way to go forward. If you have other ideas, though, I'm happy to try them.

Because now every call to getSampleValues would need this...

getSampleValues() is protected, and there's only one call to it in ContextDefinition. The BC layer is a private member, so anything that is subclassing ContextDefinition can't interact with it anyhow. So I'm not sure this is something we need to be concerned about.

Status: Needs review » Needs work

The last submitted patch, 33: 2932462-33.patch, failed testing. View results

phenaproxima’s picture

Status: Needs work » Needs review
FileSize
59.1 KB
12.84 KB

Removed as many calls to new ContextDefinition('entity: from core as I could find.

Status: Needs review » Needs work

The last submitted patch, 37: 2932462-37.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 37: 2932462-37.patch, failed testing. View results

phenaproxima’s picture

Status: Needs work » Needs review
FileSize
60.27 KB
1.04 KB

Forgot to fix the ContextDefinition annotation class.

Status: Needs review » Needs work

The last submitted patch, 40: 2932462-40.patch, failed testing. View results

phenaproxima’s picture

Status: Needs work » Needs review
FileSize
62.71 KB
2.56 KB

Okay, let's see how this does.

tim.plunkett’s picture

I think the implementation here is great.
I have a couple questions/suggestions for DX (since that's the whole point of the issue).

  1. +++ b/core/lib/Drupal/Core/Plugin/Context/ContextProviderInterface.php
    @@ -62,7 +62,7 @@ public function getRuntimeContexts(array $unqualified_context_ids);
    -   *   $context = new Context(new ContextDefinition('entity:node'));
    +   *   $context = new Context(new EntityContextDefinition('entity:node'));
    

    Shouldn't this change to EntityContext::fromEntityTypeId('node')?

    EDIT: we don't have that. But that seems useful, no?

  2. +++ b/core/modules/layout_builder/src/Plugin/Derivative/FieldBlockDeriver.php
    @@ -118,9 +118,7 @@ public function getDerivativeDefinitions($base_plugin_definition) {
    -          $context_definition = new ContextDefinition('entity:' . $entity_type_id, $entity_type_labels[$entity_type_id], TRUE);
    +          $context_definition = new EntityContextDefinition('entity:' . $entity_type_id, $entity_type_labels[$entity_type_id], TRUE);
    
    +++ b/core/modules/layout_builder/tests/src/Kernel/FieldBlockTest.php
    @@ -207,7 +207,7 @@ protected function getTestBlock(ProphecyInterface $entity_prophecy, array $confi
    -        'entity' => new ContextDefinition('entity:entity_test', 'Test', TRUE),
    +        'entity' => new EntityContextDefinition('entity:entity_test', 'Test', TRUE),
    

    I would think this could be new EntityContextDefinition($entity_type_id, ...)
    without requiring the entity: prefix.

  3. +++ b/core/modules/node/src/ContextProvider/NodeRouteContext.php
    @@ -65,7 +66,7 @@ public function getRuntimeContexts(array $unqualified_context_ids) {
    -    $context = new Context(new ContextDefinition('entity:node', $this->t('Node from URL')));
    +    $context = new EntityContext(new EntityContextDefinition('entity:node', $this->t('Node from URL')));
    

    This as well, ideally you could do EntityContext::fromEntityTypeId('node', $this->t('Node from URL'));

Overall my point is that we should look at the usages of this and be sure we really nailed the new class to provide good DX for the majority of use cases.

phenaproxima’s picture

Okay, let's see how this does.

In this patch...

  • All three factory methods for EntityContext now accept an optional $label parameter.
  • EntityContext can be created from an entity type ID.
  • new EntityContextDefinition() will accept a data type that is just an entity type ID, optionally prefixed with 'entity:'.
tim.plunkett’s picture

  1. +++ b/core/lib/Drupal/Core/Plugin/Context/EntityContextDefinition.php
    @@ -16,6 +16,18 @@
    +    if (!preg_match('/^entity:/', $data_type)) {
    

    strpos($data_type, 'entity:') !== 0
    and skip out on the slower regex

  2. +++ b/core/modules/block/tests/modules/block_test/src/ContextProvider/MultipleStaticContext.php
    @@ -46,11 +46,8 @@
    -    $context2 = EntityContext::fromEntity($current_user);
    -    $context2->getContextDefinition()->setLabel('User B');
    ...
    +    $context2 = EntityContext::fromEntity($current_user, 'User B');
    

    👍

  3. +++ b/core/modules/layout_builder/tests/src/Kernel/FieldBlockTest.php
    @@ -207,7 +207,7 @@
    +        'entity' => new EntityContextDefinition('entity_test', 'Test', TRUE),
    
    +++ b/core/modules/node/src/ContextProvider/NodeRouteContext.php
    @@ -40,7 +40,7 @@
    +    $context_definition = EntityContextDefinition::create('node')->setRequired(FALSE);
    

    This is a confusing difference, IMO.
    Wait, is ::create() even a method?

phenaproxima’s picture

Addressed the feedback above.

@tim.plunkett and I are in agreement that one of the biggest problems with Drupal's API is that there is always about eleventy kazillion ways to do things, and this is reflected in all the tests which are creating entity context definitions -- some call the static factory methods, some use the constructor, at least one uses ::create() (which is inherited from ContextDefinition), and it's all confusing as hell. Therefore, this patch normalizes everything to use the new EntityContextDefinition::fromEntityTypeId() method. It's obviously still possible to use create() or constructors (which is good for hacking, not for DX), but now we're endorsing one particular way to do it. And that is good for DX.

Status: Needs review » Needs work

The last submitted patch, 46: 2932462-46.patch, failed testing. View results

phenaproxima’s picture

Status: Needs work » Needs review
FileSize
66.19 KB
4.09 KB

This should fix the test failures. In the kernel tests, I just enabled the modules which provide the actual entity types under test -- I forget where I heard the testing mantra that "core should work with itself", but it seems to me that testing with the real entity types where possible is the best way to ensure that. :)

phenaproxima’s picture

Issue tags: +Blocks-Layouts

Tagging as part of the Layout Initiative.

phenaproxima’s picture

Change record updated.

tim.plunkett’s picture

Thanks for those changes.
This is really close!

  1. +++ b/core/lib/Drupal/Core/Annotation/ContextDefinition.php
    @@ -116,7 +116,16 @@ public function __construct(array $values) {
    +    if (isset($values['class'])) {
    +      $class = $values['class'];
    +    }
    +    elseif (strpos($values['value'], 'entity:') === 0) {
    +      $class = 'Drupal\Core\Plugin\Context\EntityContextDefinition';
    +    }
    +    else {
    +      $class = 'Drupal\Core\Plugin\Context\ContextDefinition';
    +    }
    

    I think this deserves some code comments.
    Also could switch to the ::class versions

  2. +++ b/core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php
    @@ -72,6 +70,32 @@ class ContextDefinition implements ContextDefinitionInterface {
    +   * @deprecated
    
    @@ -111,6 +135,11 @@ public function __construct($data_type = 'any', $label = NULL, $required = TRUE,
    +      @trigger_error('Constructing a ContextDefinition object for an entity type is deprecated in Drupal 8.6.0. Use ' . __NAMESPACE__ . '\EntityContextDefinition instead. See https://www.drupal.org/node/2976400 for more information.', E_USER_DEPRECATED);
    

    The @deprecated needs to be expanded

    A @deprecated PHPdoc tag that indicates when the code was deprecated, when it will be removed, and what to use instead, and an @see link to the change record for the change.

  3. +++ b/core/lib/Drupal/Core/Plugin/Context/EntityContextDefinition.php
    @@ -0,0 +1,117 @@
    +    return substr($this->getDataType(), 7);
    

    This could use a comment about the 7 corresponding to the length of 'entity:'

  4. +++ b/core/modules/layout_builder/tests/src/Kernel/FieldBlockTest.php
    @@ -207,7 +207,7 @@ protected function getTestBlock(ProphecyInterface $entity_prophecy, array $confi
    -        'entity' => new ContextDefinition('entity:entity_test', 'Test', TRUE),
    +        'entity' => EntityContextDefinition::fromEntityTypeId('entity_test')->setLabel('Test'),
    

    Thought we lost something here, but that , TRUE matches the default value so I think it's fine.
    Just calling out in case someone else notices.

phenaproxima’s picture

Fixed all feedback in #51, except:

Also could switch to the ::class versions

We can do that for EntityContextDefinition, but not ContextDefinition, since the class which contains this logic is called ContextDefinition. So rather than make it inconsistent, I left them as strings.

tim.plunkett’s picture

Status: Needs review » Reviewed & tested by the community

Could have aliased the import, but not really worth it in the long-run.
I think this is good to go!

alexpott’s picture

+++ b/core/tests/Drupal/Tests/Core/Plugin/Context/EntityContextDefinitionIsSatisfiedTest.php
@@ -0,0 +1,318 @@
+namespace Drupal\Core\Validation;
+
+if (!function_exists('t')) {
+
+  function t($string, array $args = []) {
+    return strtr($string, $args);
+  }
+
+}

:( This is kinda sad. Can we get an issue to fix \Drupal\Core\Validation\DrupalTranslator::trans()? That method is sad because it does $id instanceof TranslatableMarkup and t() on the same line.

I've opened #2979944: Remove t() usage in Drupal\Core\Validation to address this. It'd be nice to land that first.

tim.plunkett’s picture

This is being copied from \Drupal\Tests\Core\Plugin\Context\ContextDefinitionIsSatisfiedTest.
It'd be a shame to hold up this on something that unrelated.

Also in

\Drupal\Tests\Core\Render\Element\MachineNameTest
\Drupal\Tests\views\Unit\EntityViewsDataTest

And the !function_exists pattern in tests is in 17 other tests as well.

tim.plunkett’s picture

Rerolled without function_exists.

larowlan’s picture

Status: Reviewed & tested by the community » Needs review
  1. +++ b/core/lib/Drupal/Core/Annotation/ContextDefinition.php
    @@ -116,7 +116,20 @@ public function __construct(array $values) {
    +    // If the annotation specifies a specific context definition class, use
    +    // that. Otherwise, use EntityContextDefinition if the data type starts with
    +    // 'entity:', because it contains special entity-specific logic. Otherwise,
    +    // fall back to the generic ContextDefinition class.
    +    if (isset($values['class'])) {
    +      $class = $values['class'];
    +    }
    +    elseif (strpos($values['value'], 'entity:') === 0) {
    +      $class = 'Drupal\Core\Plugin\Context\EntityContextDefinition';
    +    }
    +    else {
    +      $class = 'Drupal\Core\Plugin\Context\ContextDefinition';
    +    }
    

    if we moved this to a protected method ::getDefinitionClass we could do away with the if/elseif/else by way of early returns. I think that would make it a bit easier to parse.

  2. +++ b/core/lib/Drupal/Core/Plugin/Context/EntityContextDefinition.php
    @@ -0,0 +1,118 @@
    +    $label = new TranslatableMarkup('@entity being viewed', [
    

    Can we assume that? What about the 'current user' context?

phenaproxima’s picture

Addressed both points. I'm not 100% pleased with the standard label of the entity context definition, but I can't think of a better alternative.

phenaproxima’s picture

FileSize
28.84 KB

I have no idea why interdiff spat out such a big file -- I guess my git is not set up the same way as @tim.plunkett's. Anyway, here's a way-too-big interdiff in which only the first couple of kilobytes are relevant. ¯\_(ツ)_/¯

tim.plunkett’s picture

Status: Needs review » Needs work
FileSize
3.31 KB
  1. +++ b/core/lib/Drupal/Core/Annotation/ContextDefinition.php
    @@ -116,10 +116,36 @@ public function __construct(array $values) {
    +    elseif (strpos($values['value'], 'entity:') === 0) {
    

    Deja vu, but this can be an if. (see #57.1)

  2. +++ b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php
    @@ -150,9 +149,7 @@ public function buildMultiple(array $entities) {
    -        $contexts['layout_builder.entity'] = new Context(new ContextDefinition("entity:{$entity->getEntityTypeId()}", new TranslatableMarkup('@entity being viewed', ['@entity' => $entity->getEntityType()->getLabel()])), $entity);
    +        $contexts['layout_builder.entity'] = EntityContext::fromEntity($entity);
    

    This is now a UX regression for Layout Builder, as we lose the " being viewed" portion that was all that allowed users to differentiate between other contexts of the same entity type.

Also, here's an interdiff for the above. No idea how you got it to be 28k

phenaproxima’s picture

Status: Needs work » Needs review
FileSize
64.48 KB
1.85 KB

Bah! Both fixed.

tim.plunkett’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 61: 2932462-61.patch, failed testing. View results

tim.plunkett’s picture

Status: Needs work » Reviewed & tested by the community

BS selenium failure.

catch’s picture

Status: Reviewed & tested by the community » Fixed

This looks like really good clean-up.

I winced slightly at the substr() calls around entity:ENTITY_TYPE but cannot think of a way around those and not really a new issue.

Committed 099f414 and pushed to 8.6.x. Thanks!

  • catch committed 099f414 on 8.6.x
    Issue #2932462 by phenaproxima, japerry, tim.plunkett: Add...
mtodor’s picture

I have noticed that with this commit, I'm getting multiple field blocks in the list, more or less for all content types.
I'm not sure is that ok or not?

Here is screenshot for layouting of Layout Page - custom layout for single content entity:

tim.plunkett’s picture

Status: Fixed » Closed (fixed)

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

jonathan1055’s picture

Regarding the commit in #66 this has broken Rules daily testing at 8.6 since 14 July.
#2989417: Argument 1 passed to \EntityContext::fromEntity() must implement EntityInterface

[edit] I found the change record, thanks!

Wim Leers’s picture

Just published the change record. 🙈

tim.plunkett’s picture

#3126747: ContextDefinition::create() can no longer be used with an entitytype-specific datatype (like entity:user) was opened recently. Bringing this to the attention of those following this issue.