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
- Install standard profile
- Enable layout builder
- Use Layout Builder on Article content type default display
- Create new Article: enter title and body text, leave image field empty, and save
- Inspect node content in browser (or view source)
- 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
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | patch-3398196-7.patch | 969 bytes | jaroslav červený |
Issue fork drupal-3398196
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:
- 3398196-field-block-for
changes, plain diff MR !5204
Comments
Comment #3
godotislatePut in MR with proposed solution and test.
Comment #4
godotislateComment #5
godotislateComment #6
smustgrave commentedWas going to rebase to run the test-only feature
Tested manually and can confirm the issue and the MR fixes the issue.
Fix seems simple enough
Comment #7
jaroslav červený commentedI created a patch as recommended above.
it works as expected for me.
Comment #8
quietone commentedI'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.
Comment #9
danielvezaLeft a review, just a couple of small suggestions.
Comment #10
godotislateUpdated per review and rebased.
Comment #11
danielvezaMR feedback addressed, this looks good to me!
Comment #15
catchCommitted/pushed to 11.x, cherry-picked to 10.3.x and 10.2.x, thanks!
Comment #18
cilefen commentedThere is a report of a regression: #3435906: Default Images not rendered in layout builder.