Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 293926

Extra field blocks render even when empty

$
0
0

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:

Steps to reproduce

  1. Add an extra field on a node using hook_entity_extra_field_info().
  2. Add the hook_ENTITY_TYPE_view() for the extra field, but don't add anything to the $build array.
  3. Create a node type with Layout Builder enabled on Full Content.
  4. Add that field into the type's layout.
  5. Create a page of that node type.
  6. 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


Viewing all articles
Browse latest Browse all 293926

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>