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

Created on 26 September 2019, about 5 years ago
Updated 31 January 2023, almost 2 years ago

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.

πŸ› Bug report
Status

Needs work

Version

10.1 ✨

Component
Layout builderΒ  β†’

Last updated about 12 hours ago

Created by

πŸ‡¨πŸ‡³China weynhamz Ningbo

Live updates comments and jobs are added and updated live.
  • Blocks-Layouts

    Blocks and Layouts Initiative. See the #2811175 Add layouts to Drupal issue.

  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

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

  • πŸ‡ΈπŸ‡ͺSweden twod Sweden
    +++ 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?

  • πŸ‡·πŸ‡΄Romania bbu23

    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, but that does not fix my issue. A workaround for me is to have the formatter set in the "full" view mode as well.

    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.

  • πŸ‡§πŸ‡ͺBelgium nightlife2008

    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.

  • πŸ‡§πŸ‡ͺBelgium Tim Lammar

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

  • Assigned to Tim Lammar
Production build 0.71.5 2024