Problem/Motivation

If an image field is configured to be displayed as a field block in Layout Builder, and the image field has no value and no default image set, the field block is still rendered as div wrappers around empty content.

This occurs because of a combination of changes introduced in #3119786: Default values are not displayed for image fields placed in Layout Builder and #3039185: Allow field blocks to display the configuration label when set in Layout Builder. The logic that checks whether an image field has no default image is in Drupal\layout_builder\Plugin\Block\FieldBlock::blockAccess():

if ($field->getFieldDefinition()->getType() === 'image' && $field->getFieldDefinition()->getSetting('default_image')) {

However, the value of the default_image array when there is no default image set is:

[
  'uuid' => '',
  'alt' => '',
  'title' => '',
  'width' => NULL,
  'height' => NULL,
]

Whether a default image is actually set does not affect the evaluation of the condition.

In addition, the render array for a field block returned by build() now has an additional layer of nesting (https://www.drupal.org/node/3367821), so the Element::isEmpty($content) check in Drupal\layout_builder\EventSubscriber\BlockComponentRenderArray::onBuildRender() returns FALSE. Previous to this change, while the access check returned the wrong value, the isEmpty check would prevent any markup from being rendered for the field block.

With the resulting field block content not being empty, template or preprocess display logic that checks for the presence of the image field block needs might need to be reworked.

Steps to reproduce

  1. Install standard profile
  2. Enable layout builder
  3. Use Layout Builder on Article content type default display
  4. Create new Article: enter title and body text, leave image field empty, and save
  5. Inspect node content in browser (or view source)
  6. Confirm there is an element like this:
          <div class="block block-layout-builder block-field-blocknodearticlefield-image">
            <div class="block__content"></div>
          </div>
        

Proposed resolution

In Drupal\layout_builder\Plugin\Block\FieldBlock::blockAccess(), change this:

if ($field->getFieldDefinition()->getType() === 'image' && $field->getFieldDefinition()->getSetting('default_image')) {

to

if ($field->getFieldDefinition()->getType() === 'image' && !empty($field->getFieldDefinition()->getSetting('default_image')['uuid'])) {

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

CommentFileSizeAuthor
#7 patch-3398196-7.patch969 bytesjaroslav červený

Issue fork drupal-3398196

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

godotislate created an issue. See original summary.

godotislate’s picture

Status: Active » Needs review

Put in MR with proposed solution and test.

godotislate’s picture

Issue summary: View changes
godotislate’s picture

Issue summary: View changes
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Was going to rebase to run the test-only feature

1) Drupal\Tests\layout_builder\Functional\LayoutBuilderDefaultValuesTest::testDefaultValues
Behat\Mink\Exception\ExpectationException: An element matching css ".block-field-blocknodetest-node-typefield-image-no-default" appears on this page, but it should not.
/builds/issue/drupal-3398196/vendor/behat/mink/src/WebAssert.php:794
/builds/issue/drupal-3398196/vendor/behat/mink/src/WebAssert.php:443
/builds/issue/drupal-3398196/core/modules/layout_builder/tests/src/Functional/LayoutBuilderDefaultValuesTest.php:174
/builds/issue/drupal-3398196/core/modules/layout_builder/tests/src/Functional/LayoutBuilderDefaultValuesTest.php:104
/builds/issue/drupal-3398196/vendor/phpunit/phpunit/src/Framework/TestResult.php:728
ERRORS!
Tests: 1, Assertions: 7, Errors: 1.

Tested manually and can confirm the issue and the MR fixes the issue.

Fix seems simple enough

jaroslav červený’s picture

StatusFileSize
new969 bytes

I created a patch as recommended above.

it works as expected for me.

quietone’s picture

I'm triaging RTBC issues. I read the IS, the comment and the MR. I didn't see anything more to do here.

I updated credit.

Leaving at RTBC.

danielveza’s picture

Status: Reviewed & tested by the community » Needs work

Left a review, just a couple of small suggestions.

godotislate’s picture

Status: Needs work » Needs review

Updated per review and rebased.

danielveza’s picture

Status: Needs review » Reviewed & tested by the community

MR feedback addressed, this looks good to me!

  • catch committed 9540258e on 10.2.x
    Issue #3398196 by godotislate, DanielVeza, smustgrave: Field block for...

  • catch committed 0da90dae on 10.3.x
    Issue #3398196 by godotislate, DanielVeza, smustgrave: Field block for...

  • catch committed 63eddaef on 11.x
    Issue #3398196 by godotislate, DanielVeza, smustgrave: Field block for...
catch’s picture

Version: 11.x-dev » 10.2.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 11.x, cherry-picked to 10.3.x and 10.2.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

cilefen’s picture