Problem/Motivation

Scenario:
-Content type with field_image added.
-Default image set for field on content type.
-Core layout builder is used to define display layout.
-Body field and image field are added in side by side columns.
-Page will render images explicitly added to field on node but not the default field image.

It's worth noting that the issue happens on both default images added to the node bundle scope AND to the field itself.

When this happens, there is NO html output for the image field at all, only the wrapping column.

When layout builder was disabled and I returned to the default display editor, the default image rendered.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3119786

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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rbox created an issue. See original summary.

Version: 8.8.2 » 8.8.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

tim.plunkett’s picture

Title: Drupal 8 Layout with image field causes content type image field default image to not render » Layout with image field causes content type image field default image to not render
Version: 8.9.x-dev » 9.1.x-dev
Issue summary: View changes
Issue tags: -image, -images, -Layout Builder, -default image +Blocks-Layouts, +Needs tests
emerham’s picture

Should also add that _any_ fields default values are not rendered on said nodes. Tested Body with summary, Link, email etc

JordiK’s picture

Had the same problem while using Layout Builder with the user entity and a user picture.

Narrowed down the problem to /core/modules/layout_builder/src/Plugin/Block/FieldBlock.php.
The function blockAccess denies access if the field is empty (using $field->isEmpty()) without a check for a default value.
The issue is that $field->isEmpty() returns TRUE and $fieldDefinition->getDefaultValue() returns an empty array.

Skipping the $field->isEmpty() check for image fields only allows the default image to be rendered.

The rest of the checks, namely whether the user has access to the entity and to the field itself remain untouched.

The patch provided solved the issue for me.

JordiK’s picture

Status: Active » Needs review
JordiK’s picture

Should also add that _any_ fields default values are not rendered on said nodes.

The field default value (e.g. for text fields) did not work for me on entities, which were created before this default value was set.
To check this, set a default value for a field of an entity - it will not render for existing entities neither in the entity edit form (empty field), nor in the display. New entities created after that will have the default value for the field populated on the create entity form - if saved, the value gets rendered also in Layout builder.

This is not a problem with Layout builder, but rather how the field system component behaves. Check also these issues:
#1430454: Differentiate NULL and empty for default values
#3003735: Add a way to use field's default value for existing content as well

tim.plunkett’s picture

Title: Layout with image field causes content type image field default image to not render » Default values are not displayed for fields placed in Layout Builder

It makes much more sense to be broken for all fields than for just images.

But #6 worries me.

$fieldDefinition->getDefaultValue() returns an empty array

If that's true, where does the default value come from?

JordiK’s picture

It makes much more sense to be broken for all fields than for just images.

It is broken only for image fields, as far as I can tell from the cases I tested.

For text fields:
$field->getFieldDefinition->getDefaultValue($entity) returns an array in the form [0][value] = 'some default value'
$field->getSettings() returns an array with settings, such as max-length, case-sensitive etc.

For image fields:
$field->getFieldDefinition->getDefaultValue($entity) returns an empty array.
$field->getSettings() returns an array with image settings, including the default image (if one is set).

Since the role of the blockAccess function is, well, to block access, the simple check for $field->isEmpty() is insufficient for images.
Whether excluding image fields from the check (like in the patch) or creating a more sophisticated IF-statement (e.g. checking that the getSettings() returns an array with a key 'handler' set to 'default:file') will not change the logic much.

tim.plunkett’s picture

But why is $field->getFieldDefinition->getDefaultValue($entity) empty for images?

JordiK’s picture

I guess because defaults for images (and also files) are stored differently (see the getSettings() return array) - the default_value variable is not used.
And this also is a question which goes beyond the scope and the logic of the layout builder module.
As far as this bug is concerned, for me it is clear that the blockAccess() function needs to be modified (one way or another).

tim.plunkett’s picture

Here's a fix that correctly uses the Field API (as I understand it), and a kernel test.
This still needs a functional test for a couple field types, including images.

Due to #10, it sounds like this won't fix images.

But this is the API that exists. Layout Builder should not have logic that addresses individual bugs in other parts of core. Either images will be fixable in this patch, or we'll need a follow-up/blocking issue.

The last submitted patch, 13: 3119786-defaultvalue-13-FAIL.patch, failed testing. View results

JordiK’s picture

Due to #10, it sounds like this won't fix images.

You are right, it won't.

Let me try to formulate a different question: why do we need a check for the field being empty?

tim.plunkett’s picture

See #2918500-61: Create a block which can render entity fields (Comment in 61 leads to new patch in 62, see the interdiff).

Berdir’s picture

yeah, images always had some pretty weird special handling for default value, could be related to file usage tracking, storing the default explicitly would result in lots and lots of usages being reported on that default image. And you can change the default, but only image fields are capable of doing that.

that said, without reading through that referenced comment closely, default values should otherwise just work. I default value should be explicitly stored in the field and shouldn't need to be checked at runtime. Adding/changing default value later on was never supported.

tim.plunkett’s picture

Status: Needs review » Needs work

Glad you chimed in @Berdir, I was debating about pinging you just now!

And you can change the default, but only image fields are capable of doing that.

That's... unexpected. I'm guessing this has been the case for a long time, but I'm surprised no other code has hit this special case before.

\Drupal\image\Plugin\Field\FieldFormatter\ImageFormatterBase::getEntitiesToView() seems to be the part that handles default images. And it also consults isEmpty(), which is not a good sign.

This also doesn't seem to be relevant for files, which might also be a setback. Since the $field object we have access to is Drupal\file\Plugin\Field\FieldType\FileFieldItemList, not anything image-specific.

JordiK’s picture

Thank you for the clarification in #16, @tim.plunkett!

Adding the condition !$field->getFieldDefinition()->getDefaultValue($entity) does not solve the issue with images (yes, they are weird).
Other field types (e.g. text fields) with default_value set do not render their default_value in Layout builder, only in the entity edit form.
And when the entity is saved after edit, the field value is not empty anymore.
The default_value is not shown on entity view displays either.

JordiK’s picture

andrewsuth’s picture

I can confirm what JordiK said regarding patch #13 not fixing the visualisation for default images.

As he flagged in another ticket, patch #3067982: Formatters for empty fields do not render with layout builder enabled works for this issue.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Mav_fly’s picture

I used the patch https://www.drupal.org/files/issues/2020-01-24/3067982-8.patch mentioned in https://www.drupal.org/project/drupal/issues/3067982
And the problem was solved for my image fields. But I have also fields of "Font Awesome Icon" and these are still not shown their default values.
This was tested on drupal 8.9.7

JoseCarlosss’s picture

JoseCarlosss’s picture

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

liquidcms’s picture

I was not getting the Picture field (core field on User entity) to show the default image when placing this field with Layout Builder. The patch in #13 against 9.2.8 seems to have fixed this. :)

My mistake; as reported above that this does not work for images - this does not work for Picutre default image.

Before this patch, I was using Views to create a block with that field - which does work.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

danflanagan8’s picture

Status: Needs work » Needs review
FileSize
12.51 KB
6.23 KB

Here's a functional test that exposes the bug, as requested in #13. This patch has the new functional test on top of the fail test from 13.

danflanagan8’s picture

Oops, git mishap in #29. Here's the correct patch (I hope!)

Status: Needs review » Needs work

The last submitted patch, 30: 3119786-30-FAIL.patch, failed testing. View results

danflanagan8’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
8.7 KB
646 bytes

The new functional test failed as I had hoped, so I'm removing the Needs tests tag.

Now here's the partial fix from #13 applied on top of the patch in #28. We'll see that the functional test still fails. So there's still some work to do here.

Status: Needs review » Needs work

The last submitted patch, 32: 3119786-32.patch, failed testing. View results

danflanagan8’s picture

Status: Needs work » Needs review
FileSize
10.91 KB
6.46 KB

Here's yet another patch that I'm expecting to pass. It adds some special logic for image fields since they store the default value differently from other fields.

I'm also adding a number of assertions to the functional test, rearranging, and commenting. Most notably, I add a second image field that does not have a default value, kind of as a control. These are all additions, so I don't feel the need to run yet it as a test only patch again.

Status: Needs review » Needs work

The last submitted patch, 34: 3119786-34.patch, failed testing. View results

danflanagan8’s picture

Status: Needs work » Needs review
FileSize
10.97 KB
884 bytes

Oof, too confident! Here's a slight update to the failing test such that it can handle the new call to getType() in FieldBlock. This should pass...

danflanagan8’s picture

Me again! I realized I messed up setting the default value in my test. Here is a much improved Functional test that I think covers all the bases. Without the fix part of the patch, the improved Functional test still fails only with an image field's default value. LB handles other default values just like normal field-based display does.

Webbeh’s picture

I'm only using #37 for a use-case of an empty image field, but so far it works as expected.

lobodakyrylo’s picture

Status: Needs review » Reviewed & tested by the community

Patch #37 works like a charm.

tim.plunkett’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/layout_builder/src/Plugin/Block/FieldBlock.php
@@ -204,8 +204,9 @@ protected function blockAccess(AccountInterface $account) {
+    // Check to see if the field has any values. Image fields store the default
+    // value in a unique way that we must account for here.
+    if ($field->isEmpty() && !$field->getFieldDefinition()->getDefaultValue($entity) && !($field->getFieldDefinition()->getType() === 'image' && $field->getFieldDefinition()->getSetting('default_image'))) {

I pushed back against this 2 years ago, and I still don't think this is correct now.

See #9 through #18.

And if we must, at least document how broken this is, making it clear that no one field type should be given this special treatment, and file a follow-up to fix it (with an in-code @todo pointing to it)

danflanagan8’s picture

Status: Needs work » Needs review
FileSize
13.35 KB
924 bytes

And if we must, at least document how broken this is, making it clear that no one field type should be given this special treatment, and file a follow-up to fix it (with an in-code @todo pointing to it)

I looked into this pretty deeply and came to the conclusion that image fields are currently unique and require unique treatment given the current state of APIs.

I did some searching juts now and found a core issue that looks like its goal is to attack the root of the problem: #3005528: Fields' "default value" functionality only works in formatters and widgets, not at the data level and hence not in HTTP APIs

Here's a copy of the patch from #37 with an additional @todo that references that issue. I'm telling it not to run tests because there's no point.

I don't have any strong personal feelings about whether a patch like this should be committed. On one hand, it fixes a bug. On the other hand, the bug should ideally be fixed somewhere else.

tim.plunkett’s picture

Thanks @danflanagan8! Good to see there's a dedicated issue.
I'm going to make one tweak to the code, which is to separate the temporary portion to it's own code block to make it easier to remove someday.

JordiK’s picture

I looked into this pretty deeply and came to the conclusion that image fields are currently unique and require unique treatment given the current state of APIs.

Thank you for digging deeper into it @danflanagan8 - the special treatment of image fields was exactly my point almost two years ago. Would be happy, if this will be (temporarily) solved here, until the big fix for the image fields comes one day.

tonytheferg’s picture

Patch works.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

jon.lund’s picture

Patch #42 seems to be working for me. I am using Drupal 9.4.2 Note: make sure to Clear Cache... THANK YOU

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Bug Smash Initiative

Tested the MR out following the steps in the summary (thank you for that)

Added an image field to the content type
Set a default image for the field.
Enabled layout builder for the content type
Created a node
Verified image was NOT there
Applied patch
Verified image is now there

crutch’s picture

Trying to move to Layout Builder from Panels, #42 isn't applying for 9.4.5

tim.plunkett’s picture

Title: Default values are not displayed for fields placed in Layout Builder » Default values are not displayed for image fields placed in Layout Builder
alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Nice fix and nice test coverage. It's great to see the follow-up to fix this in a more generic way.

Committed and pushed da8a916ea3 to 10.1.x and 65a6c2fb91 to 10.0.x and 348d20be24 to 9.5.x. Thanks!

As there are no API concerns here and the behaviour fix makes sense backporting the bug fix to 9.5.x.

  • alexpott committed da8a916 on 10.1.x
    Issue #3119786 by danflanagan8, tim.plunkett, JordiK, Berdir: Default...

  • alexpott committed 65a6c2f on 10.0.x
    Issue #3119786 by danflanagan8, tim.plunkett, JordiK, Berdir: Default...

  • alexpott committed 348d20b on 9.5.x
    Issue #3119786 by danflanagan8, tim.plunkett, JordiK, Berdir: Default...
alexpott’s picture

Version: 9.5.x-dev » 9.4.x-dev

Discussed with @longwave we agreed to backport to 9.4.x too.

  • alexpott committed 2a2a13c on 9.4.x
    Issue #3119786 by danflanagan8, tim.plunkett, JordiK, Berdir: Default...

Status: Fixed » Closed (fixed)

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

kruser’s picture

- deleted-