Problem/Motivation

In Display suite and similar, when preprocessing a layout, a theme could access the entity being rendered from a special #entity property
No such property is available for Layout Builder, meaning that implementing hook_preprocess_HOOK for layout theme hooks is severely hamstrung

Proposed resolution

Make the layout builder entity available for themers

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3111192

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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

larowlan created an issue. See original summary.

larowlan’s picture

larowlan’s picture

Title: Themes have no context of the entity being rendered in preprocessing a layout » Themes have no context of the entity being rendered in preprocessing a layout when using Layout builder

Google keywords

jibran’s picture

Status: Needs review » Needs work

Seems like an oversight. Thank you for figuring out a way to add this.

+++ b/core/modules/layout_builder/src/Section.php
@@ -88,6 +88,9 @@ public function toRenderArray(array $contexts = [], $in_preview = FALSE) {
+    if (isset($contexts['layout_builder.entity']) && $entity = $contexts['layout_builder.entity']->getContextValue()) {

Please add () around $entity = $contexts['layout_builder.entity']->getContextValue()

larowlan’s picture

re #4 can I ask why? its not needed if its the last statement

jibran’s picture

Status: Needs work » Reviewed & tested by the community

can I ask why?

Because it always trips me.

its not needed if its the last statement

Yeah, that's true.

tim.plunkett’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +Blocks-Layouts
FileSize
2.73 KB
1.2 KB

#4
Definitely not an oversight, as layouts themselves have nothing to do with entities. When Layout Builder is involved, sure, there is an entity.

That said, I think this is a worthwhile addition.
(Though that was a reeeeallly fast RTBC...)


However, see #3018782: Remove extraneous context mapping of layout_builder.entity. That context key is hopefully not long for this world...

Also, I don't think that patch is going to pass as-is.
Here's my approach that I was working on before #2 was posted

tim.plunkett’s picture

This currently tests one of four cases.
Overrides, View
Overrides, Layout UI
Defaults, View
Defaults, Layout UI

Previous patch would have broken on a couple of them. Here's an iteration. Didn't get to the tests, too late here.

The last submitted patch, 2: 3111192-fail.patch, failed testing. View results

The last submitted patch, 2: 3111192.patch, failed testing. View results

The last submitted patch, 7: 3111192-entity-7.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 8: 3111192-entity-8.patch, failed testing. View results

larowlan’s picture

+++ b/core/modules/layout_builder/src/Section.php
@@ -88,7 +89,19 @@ public function toRenderArray(array $contexts = [], $in_preview = FALSE) {
+    $build = $this->getLayout()->build($regions);
...
+          $build['#entity'] = $value;

its a shame we can't pass the context of the entity to the layout plugin too, but I get that layouts are not entity-specific

nicxvan’s picture

#8 seems to be working for me on 8.7.10

tim.plunkett’s picture

Status: Needs work » Needs review
FileSize
2.88 KB
1.3 KB

If we add in the #entity when the whole thing is empty, other stuff (like Quick Edit) will break.

Still needs tests.

nicxvan’s picture

I enabled quickedit to test the latest patch. I still receive the following error in console:

Error: Quick Edit could not associate the rendered entity field markup (with [data-quickedit-field-id="block_content/1/field_block_text_text/en/full"]) with the corresponding rendered entity markup: no parent DOM node found with [data-quickedit-entity-id="block_content/1"]. This is typically caused by the theme's template for this entity type forgetting to print the attributes.

tim.plunkett’s picture

nicxvan’s picture

I can confirm that the referenced issue fixed the Quick Edit issue. Thanks!

tim.plunkett’s picture

Status: Needs review » Needs work

Thanks for confirming @nicxvan!
NW for test coverage

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

larowlan’s picture

Category: Bug report » Feature request
Issue tags: +Bug Smash Initiative

In retrospect, this is a feature request, not a bug

tim.plunkett’s picture

To be clear about #8, the assertion added to LayoutBuilderTest::testLayoutBuilderUi() is testing that this works when viewing a node that does not have an overridden layout.

Within that same method, three other similar assertions should be added:

  1. Editing the default layout
  2. Viewing a node after an override is created
  3. Editing the overridden layout.

In the meantime, here's a reroll of the patch from #15, pushed up as a branch.

mohit_aghera made their first commit to this issue’s fork.

mohit_aghera’s picture

Status: Needs work » Needs review

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

larowlan’s picture

Issue tags: +#pnx-sprint

Tagging for internal sprints

pyrello’s picture

Adding a patch because we need this to be able to update to 9.2.x. I tried to create an interdiff but was having issues with that.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

larowlan’s picture

Hiding patch

larowlan’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests

This looks good to me

tim.plunkett’s picture

Should have changed the target branch before rebasing, sorry for the noise. Minor conflict with #3027653: Allow block and layout plugins to determine if they are being previewed, but straightforward resolution.

lauriii made their first commit to this issue’s fork.

lauriii’s picture

Status: Reviewed & tested by the community » Needs review

Posted a question on the MR about the algorithm used for finding the context

lauriii’s picture

Issue tags: +Needs change record

It would be also nice to have a change record for the new API 😇

amjad1233’s picture

Issue tags: -Needs change record

Draft CR : https://www.drupal.org/node/3278487 (first time so apologies in advance)

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mstrelan made their first commit to this issue’s fork.

joewhitsitt’s picture

Patch from #29 no longer applies for us. Generated patch from MR so we can update drupal core

joewhitsitt’s picture

hiding patches

tim.plunkett’s picture

Rebased the MR, it's still against 9.4.x but should be okay for both

tim.plunkett’s picture

Now updated for 9.5.x

nicxvan’s picture

Here is a static patch so a client can test it.

nicxvan’s picture

Status: Needs review » Reviewed & tested by the community

We've been using this patch for over 2 years and just retested the latest, moving to RTBC.

afogg-geisel’s picture

I'd like to see this get merged in as well. I've just tested this on my environment and everything is working correctly.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 44: 3111192-lb-entity-context-44.patch, failed testing. View results

tim.plunkett’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -#pnx-sprint

Random fail on the patch. The MR is still passing. Note you don't have to upload a file, you can append .patch to MR paths: https://git.drupalcode.org/project/drupal/-/merge_requests/608.patch

nicxvan’s picture

I always generate a patch file to use in composer, if you use the .patch method any changes to the MR will automatically be pulled in which could allow an untested / unreviewed commit to be pulled into a project.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed 3f9bc84fe6 to 10.1.x and 3be2eba61d to 10.0.x and 921f2563e6 to 9.5.x. Thanks!

  • alexpott committed 3f9bc84 on 10.1.x
    Issue #3111192 by tim.plunkett, larowlan, mohit_aghera, mstrelan,...

  • alexpott committed 3be2eba on 10.0.x
    Issue #3111192 by tim.plunkett, larowlan, mohit_aghera, mstrelan,...

  • alexpott committed 921f256 on 9.5.x
    Issue #3111192 by tim.plunkett, larowlan, mohit_aghera, mstrelan,...
nicxvan’s picture

Thank you for committing this, it's been indespensible for one of my client's site.

@alexpott I'd like to know if there was more I could have done to help on this issue in order to be credited for the work. I've been attempting to understand the core credit guidelines more closely https://www.drupal.org/core/maintainers/issue-credit I understand that comments like #14 do not qualify for credit, but I thought the interaction in #16-#18 and #45 would qualify.

I've been thinking about how to improve my contributions when it's not directly code related since it's been a topic on the show recently so any feedback you could provide would be highly appreciated.

For the record I understand it takes a lot of effort to review a years old ticket and determine who should be credited and I am not trying to add to that burden but I was talking with @hestenet on one of the recent episodes about credit and he suggested I ask as long as it's respectful, so I hope that's ok.

alexpott’s picture

Hi @nicxvan, thank you for your contributions on this issue. Here are my thoughts about credit and your comments. re #16 and #18 I thought that these were good comments but did not materially affect the patch so I didn't tick the box for them. Similar to #45 - yes that's useful information but in an of itself I don't think that that is a reason to get credit. So I concur that your participation on the issue has had value, I'm just not sure it worth credit.

Here are some examples of things that have a material affect on the patch and the issue:

  • Finding edge cases with the solution or improvements to it
  • A comment that displays that you've considered all aspects of the problem and the shown how the solution an improvement and will not cause unexpected issues once committed.
nicxvan’s picture

@alexpott Thanks! That's super helpful, I think one of the things I was doing here was comparing to the list of things that will not get credit rather than comparing more closely to the list of things that will receive credit and comparing there. It's a slight change but when I change that perspective it's clearer to me how to use that guide. I also appreciate you calling out that the comments had value but didn't rise to credit level, that was definitely one of the primary points of discussion I've been thinking of for the last few weeks.

Status: Fixed » Closed (fixed)

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