Problem/Motivation
When adding an empty extra field in Layout Builder, the resulting page will always render the wrapping div of ExtraFieldBlock even when the extra field has empty content.
Details
ExtraFieldBlock.php
's build()
method will always return a placeholder render array if an extra field is present. While BlockComponentRenderArray.php
's onBuildRender()
methods attempts to screen out empty blocks, the extra field block will always pass because of the placeholder. This causes it to add the block for rendering ($build = ['#theme' => 'block', ...
). When time comes to render the contents, since core/lib/Drupal/Core/Render/Renderer.php
's doRender()
method will always render arrays with a #theme
present, it renders the wrapper div, even when it contains no content.
When rendering the page without using Layout Builder, the extra field just doesn't exist in the render array and no wrapper div is rendered.
Unsure whether this is expected behavior or not for Layout Builder. But I do expect that it would behave similar to how it did without using Layout Builder.
References:
- https://github.com/drupal/drupal/blob/9.0.0/core/modules/layout_builder/...
- https://github.com/drupal/drupal/blob/9.0.0/core/modules/layout_builder/...
- https://github.com/drupal/drupal/blob/9.0.0/core/lib/Drupal/Core/Render/...
Steps to reproduce
- Add an extra field on a node using
hook_entity_extra_field_info()
. - Add the
hook_ENTITY_TYPE_view()
for the extra field, but don't add anything to the$build
array. - Create a node type with Layout Builder enabled on Full Content.
- Add that field into the type's layout.
- Create a page of that node type.
- View the page.
Proposed resolution
Add a pre-render callback to ensure empty extra_field blocks are not rendered.
Remaining tasks
Update summary
User interface changes
No empty div is printed when extra field is empty.
API changes
None.
Data model changes
None.
Release notes snippet
N/A