Extra field blocks render even when empty

Created on 16 June 2020, over 4 years ago
Updated 6 February 2023, about 2 years ago

Summary

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.

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.

Expected

On Step 6, the field should not render at all.

Actual

On Step 6, an empty div of that extra field will be on the page.

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:

🐛 Bug report
Status

Needs work

Version

9.5

Component
Layout builder 

Last updated about 3 hours ago

Created by

🇺🇸United States fskreuz

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

Merge Requests

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

    This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

    For this issue to move forward next it will need a test case showing the problem.

    Thanks.

  • Status changed to Needs review about 2 years ago
  • 🇧🇪Belgium herved

    Here is a minimal test which should highlight clearly the issue.
    Test only patch should fail of course, and the other one includes the pre_render from #5.

    About #2 and #10: Extra fields are used in many places now. Modules like https://www.drupal.org/project/extra_field rely on it.
    If the render array is NULL or only contains #cache, it is only logical to hide the container div completely. Those empty divs can be really problematic when asserting the emptiness of paragraphs for example.

  • Status changed to Needs work about 2 years ago
  • 🇺🇸United States smustgrave

    So tried replicating

    On Drupal 10.1 with standard install
    Enabled layout builder for Basic page
    Added section
    Added Moderated Control block
    Create a basic page node (published)
    The section markup appears with and without the patch
    I do not see an empty div for blocks with or without the patch

    Am I missing a step?

  • 🇧🇪Belgium herved

    Are you sure?
    I just tried with 9.5.x and 10.1.x and was able to reproduce in both cases.
    Indeed the section markup still shows up but this issue only resolves the extra field markup.
    For sections I think this is up to layouts to check for emptiness and not render if that's the case.

    The markup for a published node is as follows:

    <div class="layout layout--onecol">
      <div class="layout__region layout__region--content">
        <!-- START: this is what the patch removes -->
        <div class="block block-layout-builder block-extra-field-blocknodearticlecontent-moderation-control">
          <div class="block__content">
          </div>
        </div>
        <!-- END -->
      </div>
    </div>
    
  • 🇺🇸United States smustgrave

    So tried retesting and seems like this does work but doesn't fix existing pages. Should we have an upgrade path for those?

  • 🇧🇪Belgium herved

    There is no upgrade path needed, only a clear cache is required.
    Did you clear caches after applying the patch?
    Thanks

  • Status changed to RTBC about 2 years ago
  • 🇺🇸United States smustgrave

    Cleared cache a few times and it finally took.

    Thanks for baring with me on that one haha.

  • 🇧🇪Belgium herved

    Setting back to RTBC.

    It looks like the testbot had some issues a few days ago which seem completely unrelated to this patch... after re-queuing the patch the errors disappeared.
    https://www.drupal.org/pift-ci-job/2604971 for reference.

  • Status changed to Needs work almost 2 years ago
  • 🇺🇸United States tim.plunkett Philadelphia

    An empty post_update hook will force the caches to be cleared, it's worth adding that IMO

  • 🇧🇷Brazil pfeiffer

    I rerolled the patch on comment #11 to Drupal 10.2

  • 🇺🇸United States chrisolof

    Adding interdif.

  • Status changed to Needs review about 1 year ago
  • 🇺🇸United States chrisolof

    I tried applying the patch from #22, but ran into errors due to the removal of Drupal\Core\Security\TrustedCallbackInterface. I think the removal of TrustedCallbackInterface was likely unintentional.

    Attached is a re-roll of the patch in #11 against 10.2.x, with the requested post_update hook added so a cache clear is forced.

  • last update about 1 year ago
    Custom Commands Failed
  • Status changed to Needs work about 1 year ago
  • 🇺🇸United States smustgrave

    Manually triggered #24 bur appears to have a failure. Didn't do a full review but recommend switching to MR.

  • First commit to issue fork.
  • Merge request !6384Converting to MR → (Open) created by Hardik_Patel_12
  • Pipeline finished with Success
    about 1 year ago
    Total: 603s
    #84726
  • Status changed to Needs review about 1 year ago
  • Pipeline finished with Failed
    about 1 year ago
    Total: 165s
    #84757
  • Status changed to Needs work about 1 year ago
  • The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. 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.

  • Pipeline finished with Success
    about 1 year ago
    Total: 625s
    #85049
  • Status changed to Needs review about 1 year ago
  • Status changed to Needs work about 1 year ago
  • 🇺🇸United States smustgrave

    Left a nitpicky but a comment on the tests if possible to also get a positive assertion.

  • Status changed to Needs review about 1 year ago
  • 🇺🇸United States smustgrave

    Didn’t mean to change status

  • Status changed to Needs work about 1 year ago
  • 🇺🇸United States smustgrave

    Been a few days so moving to NW for the small comment.

  • First commit to issue fork.
  • Status changed to Needs review 11 months ago
  • 🇷🇸Serbia vaish

    I resolved the merge conflict and addressed the nitpicks. Moving to needs review.

  • Pipeline finished with Success
    11 months ago
    Total: 991s
    #156270
  • Status changed to Needs work 11 months ago
  • 🇺🇸United States smustgrave

    Cleaned up the issue summary some.

    Appears close but moving to NW to update the missing pieces of the summary, left TBD in there.

  • Status changed to Needs review 8 months ago
  • 🇺🇸United States mlncn Minneapolis, MN, USA

    Updated summary.

  • Status changed to RTBC 8 months ago
  • 🇺🇸United States smustgrave

    Feedback appears to be addressed.

  • 🇳🇿New Zealand quietone

    Triaging the RTBC queue. I didn't find any unanswered questions and the comments in the MR are clear.

    Leaving at RTBC.

  • 🇫🇷France nod_ Lille

    Code seems reasonable, not a subject I'm comfortable committing though. RTBC +1

  • The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. 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.

  • Assigned to shalini_jha
  • Status changed to Needs work 4 months ago
  • Pipeline finished with Failed
    4 months ago
    Total: 244s
    #343449
  • Pipeline finished with Failed
    4 months ago
    Total: 464s
    #343460
  • Pipeline finished with Success
    4 months ago
    Total: 441s
    #343472
  • 🇮🇳India shalini_jha

    I have checked this MR and found some conflicts, which I have addressed. The pipeline was failing due to an unknown word. After reviewing the existing test method, I added it to the cspell:ignore list, and the pipeline passed successfully. After that, I revalidated the test coverage, and it is working as expected. Let me know if you need any further adjustments!
    Kindly review.

    Failing test :

    There was 1 failure:
    
    1) Drupal\Tests\layout_builder\Functional\LayoutBuilderTest::testExtraFields
    Behat\Mink\Exception\ExpectationException: An element matching css ".block-extra-field-blocknodebundle-with-section-fieldlayout-builder-test-empty" appears on this page, but it should not.
    
    /var/www/html/vendor/behat/mink/src/WebAssert.php:888
    /var/www/html/vendor/behat/mink/src/WebAssert.php:492
    /var/www/html/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php:505
    
  • 🇮🇳India shalini_jha

    Moving this for NR.

  • 🇺🇸United States smustgrave

    Rebase seems fine.

  • First commit to issue fork.
  • Pipeline finished with Success
    2 months ago
    Total: 9237s
    #393665
  • 🇬🇧United Kingdom alexpott 🇪🇺🌍

    I think we need to add a positive test in some way - at least the test fails without the fix but this is very fragile as we're depending on the CSS class names for the test - which are not API - therefore I think we need to go to the effort of a positive test case somehow - suggestion left on MR

  • Pipeline finished with Success
    about 2 months ago
    Total: 338s
    #396342
Production build 0.71.5 2024