Cannot use Entity View block in Layout Builder when there is no context

Created on 29 January 2019, about 6 years ago
Updated 15 September 2024, 7 months ago

Problem/Motivation

When using Layout Builder (currently in core 8.6) on the Full view mode of a User entity, trying to use Entity View block to add a different view mode of the same user to the sidebar region in the layout. For example, we have a "Summary" view mode for a user, which we want to show in the sidebar of the user's full profile view mode. We have a component-based design system where each view mode is a tested component and want to reuse the code, rather than just dragging the specific fields manually into the sidebar and re-theming it all.

The problem is, when adding the Entity View (user) block to the sidebar, and selecting the "User" context, it gives the error:

The website encountered an unexpected error. Please try again later.Drupal\Core\Entity\EntityMalformedException: The "user" entity cannot have a URI as it does not have an ID in Drupal\Core\Entity\Entity->toUrl() (line 189 of core/lib/Drupal/Core/Entity/Entity.php). Drupal\Core\Entity\Entity->urlInfo() (Line: 363)
rdf_preprocess_user(Array, 'user', Array) (Line: 287)
Drupal\Core\Theme\ThemeManager->render('user', Array) (Line: 437)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 490)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 93)
__TwigTemplate_dbe2c465bc72e215d3f3f9cbb17e00984a4f49964adb22e917c24410dc54db6b->block_content(Array, Array) (Line: 215)
Twig_Template->displayBlock('content', Array, Array) (Line: 60)
__TwigTemplate_dbe2c465bc72e215d3f3f9cbb17e00984a4f49964adb22e917c24410dc54db6b->doDisplay(Array, Array) (Line: 428)
Twig_Template->displayWithErrorHandling(Array, Array) (Line: 399)
Twig_Template->display(Array) (Line: 407)
Twig_Template->render(Array) (Line: 64)
twig_render_template('themes/custom/vnext/templates/block/block--entity-view--user.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('block', Array) (Line: 437)
...

If "current user" context is selected instead, then it works. So I think the problem is that it doesn't know what user context to use when the block is being displayed within the Layout Builder UI, since there isn't any User ID in the URL.

Proposed resolution

Need to either define a placeholder context when there isn't any existing context, or detect when in the Layout Builder UI to render a placeholder block. Core has a new "Fallback" placeholder text (see Views Block) when the block being rendered is empty, so just returning empty block content when there isn't any proper User id context would also allow this to work in the Layout Builder UI.

🐛 Bug report
Status

Needs review

Version

3.0

Component

CTools Blocks

Created by

🇺🇸United States mpotter

Live updates comments and jobs are added and updated live.
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.

Production build 0.71.5 2024