Problem/Motivation

Following up #3500244: [2.0.0-rc2] Entity ContextException with ComponentLayout from UI Patterns project

we need to carefully ensure that DS entity view will inject the rendered entity inside the context, or we need to add a specific mechanism to allow a custom resolver to resolve the entity for ui patterns2.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

just_like_good_vibes created an issue. See original summary.

pdureau’s picture

Title: Compatibility with UI patterns 2 » ContextException with entity
Issue summary: View changes
just_like_good_vibes’s picture

we found the solution.

one needs to implement


function ui_patterns_ds_preprocess_ds_entity_view(&$variables) {
   if (isset($variables['content']) && isset($variables['content']['#entity']) && isset($variables['content']['#source_contexts'])
      && is_array($variables['content']['#source_contexts']) && !isset($variables['content']['#source_contexts']["entity"])) {
    $variables['content']['#source_contexts']["entity"] = \Drupal\Core\Plugin\Context\EntityContext::fromEntity($variables['content']['#entity']);
  }
}
dalemoore’s picture

This partially solves the issue for me. I put the code @just_like_good_vibes included above directly into a custom module, installed it, and I no longer get ContextException errors on the front end, but I do still get them in Layout Builder.

Steps:

  1. Put the code above in a custom module, or in the code for this new module, whatever you have to do to get it the codebase :)
  2. Create a block type, for example, Alert, and add a field on it of List (text) type called Variant. Add some variant types to the field list, e.g., default|Default, primary|Primary, danger|Danger, etc. and save... can add other fields too like Body to populate the alert slot.
  3. In the Alert block type, select an Alert component from the SDCs in the layout settings. Map the Variant field to the Variant prop.
  4. Go to a content type, like Page, and enable Layout Builder and add a block of the Alert block type to it, and select one of the variants and save the layout.
  5. The Alert block will have the proper variant on the frontend of the site, but in Layout Builder for the page content type, it won't. You'll still get the ContextExceptions in the logs.
just_like_good_vibes’s picture

Hello, thanks for reporting the issue, much valued, we indeed need to carefully investigate more.
i will update the proposed solution soon

  • sharique committed f18056c4 on 1.0.x
    Issue #3501396 by just_like_good_vibes: ContextException with entity,...

just_like_good_vibes’s picture

Status: Active » Fixed
just_like_good_vibes’s picture

Hello @dalemoore, It should be fixed now :)

dalemoore’s picture

Just tested, yep fixed! Thank you 🙇‍♂️

Status: Fixed » Closed (fixed)

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