Hi Team,

We use hook_entity_view_mode_alter() to change the view mode of an entity based on some conditions, but we also require the ability to override it with Layout Builder on the entity level. It seems that Layout Builder doesn't know about the hook; thus, it gets the layout from the default full view mode,

Expected Behavior

If the view mode has been changed by hook_entity_view_mode_alter(), the Layout Builder override should get the layout from the new view mode.

Issue fork drupal-3083754

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

weynhamz created an issue. See original summary.

weynhamz’s picture

Status: Active » Needs review
StatusFileSize
new1.28 KB
weynhamz’s picture

StatusFileSize
new2.12 KB

Drupal core assumes only `full` is the view mode used for the canonical route, so it does not pick up the override we made per node for the custom view mode. Is this canonical mode check really necessary? Attached patch currently disables the canonical mode check to temporarily resolve this issue.

The related broader issue is to make the layout builder override work for all custom view mode, that should address and resolve this specific use case.

weynhamz’s picture

StatusFileSize
new2.12 KB

Silly mistake.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.9 was released on November 6 and is the final full bugfix release for the Drupal 8.7.x series. Drupal 8.7.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.8.0 on December 4, 2019. (Drupal 8.8.0-beta1 is available for testing.)

Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.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.

tim.plunkett’s picture

Version: 8.8.x-dev » 8.9.x-dev
Status: Needs review » Needs work
Issue tags: +Blocks-Layouts, +Needs tests

NW for tests

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.

avpaderno’s picture

Title: Layout Buider Overrides does not aware of 'hook_entity_view_mode_alter' » Layout Buider doesn't respect the view mode set in a hook_entity_view_mode_alter() implementation
Issue summary: View changes

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.

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.

danflanagan8’s picture

Title: Layout Buider doesn't respect the view mode set in a hook_entity_view_mode_alter() implementation » Layout Builder doesn't respect the view mode set in a hook_entity_view_mode_alter() implementation

Fixed typo in title.

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.

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.

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

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mitchel432’s picture

Refactored patch for Drupal 9.5

mitchel432’s picture

StatusFileSize
new2.12 KB

Fixed EOL and Encoding on my patch file.

_utsavsharma’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work

Was previously tagged for tests in #6 which still needs to happen.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

twod’s picture

+++ b/core/modules/layout_builder/src/Form/LayoutBuilderEntityViewDisplayForm.php
@@ -83,7 +83,7 @@ public function form(array $form, FormStateInterface $form_state) {
-    if ($this->isCanonicalMode($this->entity->getMode())) {
+    if (TRUE || $this->isCanonicalMode($this->entity->getMode())) {

Leftover debug override?

bbu23’s picture

hook_entity_view_mode_alter in combination with Layout Builder + a formatter set in the "new view mode" that contains an ajax form crashes. I've seen multiple issues related to layout builder and other view modes besides full. I've tried a combination of patches, including the one from here (with the observation from previous comment manually modified), but that does not fix my issue.

A workaround for me is manually building the form prior to layout builder
Details:
1. I have a field formatter that displays an ajax form that whenever is submitted it reveals the processed value(s) of that field. But it requires this user interaction through ajax.
2. I have multiple "full" view modes that depend on specific entity values. So the "full" view mode is altered.
3. The ajax form does not work on the altered view modes, only on "full". It gives the "javascript is disabled" error which is described anyways in the ajax url documentation that it can happen.

Workaround:
1. If the form is loaded earlier forcefully, without storing it or anything, just form builder load with any potential params, then the ajax form works.

I feel like the solution for this lies in multiple issues combined. I doubt that anyone or maybe just a few people have this setup, but I wanted to share.

nightlife2008’s picture

I discovered a side effect of the patch in #16, namely that my theme is prematurely initialized, and the default theme is used instead of the admin theme, because the route match hasn't been determined yet at that point, since it's currently determining it :D

Steps in code when this happens:

1. Find route match for path /layout_builder/add/block/overrides/node.391/0/<region>/<pluginId>
2. Routing parses the route params
3. Causes section storage's method deriveContextsFromRoute to be triggered
4. Rendering the entity causes the Theme negotiators to run on a NullRoute, which at that point always returns the default theme.
5. The page is rendered in the default theme instead of using the admin_theme as it should according to the _admin_route: TRUE flag.

tim@lammar.be’s picture

StatusFileSize
new1.15 KB

Rework of #16 with removal of first (I think debug?) change.
Tested on local D10.2.4.

tim@lammar.be’s picture

Assigned: Unassigned » tim@lammar.be
waropd’s picture

Version: 11.x-dev » 11.3.x-dev
StatusFileSize
new1.14 KB

Updated patch for D11.3

avpaderno’s picture

Version: 11.3.x-dev » 11.x-dev
Assigned: tim@lammar.be » Unassigned

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

voleger’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests

Added test case of hook implementation with display mode set.

smustgrave’s picture

Status: Needs review » Needs work

left comments on the MR.

Thanks.

voleger’s picture

Status: Needs work » Needs review
needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new91 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

danielveza’s picture

Given the changes a quick sweep, I'm not too sure about the property_exists call, it feels a bit brittle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

voleger’s picture

So we need to choose either to introduce an API change or to use a brittle solution based on the property_exists check.
Are there any other options to resolve the issue?

keshavv’s picture

StatusFileSize
new1.43 KB

Re-rolled #25 to fix the fatal it introduces, and to answer the question left open in #33/#35.

Why #25 and MR !14183 don't work as-is

LayoutEntityHelperTrait neither declares $entityTypeManager nor requires its users to provide one. In 11.3.x, LayoutOverrideFieldHelper uses the trait and injects only two services:

public function __construct(
protected SectionStorageManagerInterface $sectionStorageManager,
protected LayoutTempstoreRepositoryInterface $layoutTempstoreRepository,
) {}
So $this->entityTypeManager in #25 is an undefined property there, and the path is reachable from an ordinary entity save:

LayoutBuilderHooks::entityUpdate() #[Hook('entity_update')]
→ LayoutOverrideFieldHelper::updateTempstoreEntityContext()
→ LayoutEntityHelperTrait::getOverridesSectionStorageForEntity() (line 167)
→ LayoutEntityHelperTrait::getSectionStorageContextsForEntity() (line 181)
→ Error: Undefined property
With #25 applied, saving any entity that has the layout override field fatals. Contrib hits the same thing — page_manager's LayoutBuilderDisplayVariant and layout_builder_st's ComponentPluginTranslate also use the trait with no entity type manager.

MR !14183 avoids the fatal with property_exists($this, 'entityTypeManager'), but that skips the new code entirely for exactly those classes, LayoutOverrideFieldHelper included — so the reported bug stays unfixed on the path that actually reaches it. That is the brittleness #33 is pointing at.

Proposed approach — neither an API change nor property_exists

The trait already solves this for its other dependency, two methods up at line 155:

private function sectionStorageManager() {
return $this->sectionStorageManager ?? \Drupal::service('plugin.manager.layout_builder.section_storage');
}
The attached patch uses the same null-coalescing container fallback:

$entity_type_manager = $this->entityTypeManager ?? \Drupal::entityTypeManager();
?? uses isset() semantics, so it is safe both when the property is absent and when it is declared but uninitialised.

I deliberately did not add an entityTypeManager() accessor mirroring sectionStorageManager(). LayoutBuilderEntityViewDisplay also uses this trait and inherits EntityBase::entityTypeManager(); a trait method of that name would take precedence over the inherited one, and reducing it to private would be a fatal visibility error.

Also changed != to !== — both operands are strings.