Problem

copy-paste from #14:
Unfortunately, Drupal core is inconsistent in it's implementation of default field values. I compared an image field with a default value to a text field with a default value and this is what I found:

image field with default value text field with default value
when a new node is created default value remains in field config default value is inserted in DB as field value
when default value is changed existing content shows new default value existing content is not updated
result of $field->isEmpty() TRUE FALSE
result of $fieldDefinition->getDefaultValue() [] (empty array) [ 0 => [ 'value' => 'my default value' ] ]

This means we have two problems when handling default values in our blocks:

  1. For fields such as the text field, we cannot tell the difference between a default value and a user-entered value. We can check if the field is empty but we cannot offer an option to hide the block when the field contains the default value.
  2. For fields such as the image field (probably file fields too), the field API (getDefaultValue()) does not allow us to retrieve the default value when the field is empty. (Maybe it would be possible if FileFieldItemList would implement FieldItemListInterface::processDefaultValue, but that would be a core issue.) In theory we could add workarounds specifically for image/file fields, but I'm not keen on that.

Solution

To be decided.

Original bug report

If image field has default image set the default image does not get displayed in the field block.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

terotik created an issue. See original summary.

marcvangend’s picture

Status: Active » Needs work
Issue tags: +DevDaysMilan

Thanks for the report. You're right, the code is checking if a field is empty (ListInterface::isEmpty()) to check if the block needs to be rendered at all, but a field that only has a default value is considered to be empty by Drupal. I can probably remove the isEmpty check, but I'll have to test if that doesn't break something down the road.

marcvangend’s picture

Title: Image field default image does not show in the block » Field default value does not show in the block
Status: Needs work » Needs review

Here's the patch to test. BTW it should apply to all default values, not just image fields.

marcvangend’s picture

Oops, file didn't upload.

marcvangend’s picture

Perhaps we should change $field->access('view', $account, TRUE) to $field->access('view', $account) as per #2665114: access check could be much better.

NewZeal’s picture

The patch fixes it for me. Default image now displays where previously it didn't. Thanks.

marcvangend’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for your feedback. Let's consider this RTBC then, I'll try to commit and release asap.

  • marcvangend authored 010d52a on 8.x-2.x
    Issue #2613144 by marcvangend: Field default value does not show in the...
Sutharsan’s picture

Status: Reviewed & tested by the community » Fixed
dgDatpasst’s picture

This patch is a regression for me.
Cause the block is always filled and so the region I use the block in.
I can no longer decide if the region is empty or not.
Drupal standard should be, that empty blocks are not rendered.

marcvangend’s picture

Status: Fixed » Needs work

@dgDatpasst Thanks for the feedback, I see your point. I guess we can add a switch/checkbox to control if default values should be rendered. IMO the default configuration should always try to mimic the normal rendering of the field inside its entity. Of course empty blocks should never be rendered.

Sutharsan’s picture

Cause the block is always filled ...

@dgDatpasst, Can you describe the situation that is causing the problem? Just to be sure we are talking about the same.

dgDatpasst’s picture

@Sutharsan In my situation $field->isEmpty() is true and if the block is rendered, the content var in twig template is filled with white space or theming information (if debugging enabled).
But what really lead to my regression is, that the condition {% if page.region_name_here is empty %} I use in twig cant be true anymore, if all blocks in that region are field blocks and their fields are empty.
Do you need more information?

marcvangend’s picture

I looked into the problem and the patch is obviously not behaving as it should. Other users are already reporting the new behavior as a bug (#2968663: Hide if empty option).

Unfortunately, Drupal core is inconsistent in it's implementation of default field values. I compared an image field with a default value to a text field with a default value and this is what I found:

image field with default value text field with default value
when a new node is created default value remains in field config default value is inserted in DB as field value
when default value is changed existing content shows new default value existing content is not updated
result of $field->isEmpty() TRUE FALSE
result of $fieldDefinition->getDefaultValue() [] (empty array) [ 0 => [ 'value' => 'my default value' ] ]

This means we have two problems when handling default values in our blocks:

  1. For fields such as the text field, we cannot tell the difference between a default value and a user-entered value. We can check if the field is empty but we cannot offer an option to hide the block when the field contains the default value.
  2. For fields such as the image field (probably file fields too), the field API (getDefaultValue()) does not allow us to retrieve the default value when the field is empty. (Maybe it would be possible if FileFieldItemList would implement FieldItemListInterface::processDefaultValue, but that would be a core issue.) In theory we could add workarounds specifically for image/file fields, but I'm not keen on that.

Until we have a better solution, I propose to revert the patch and keep using the result of `$field->isEmpty()` to decide if the block should be displayed or not.

marcvangend’s picture

Status: Needs work » Needs review
FileSize
592 bytes

Patch for reverting commit 010d52a.

Sutharsan’s picture

Status: Needs review » Reviewed & tested by the community

This reverts the #4 patch.

  • Sutharsan committed 6c22c34 on 8.x-2.x authored by marcvangend
    REVERTS issue #2613144 by marcvangend: Field default value does not show...
Sutharsan’s picture

Status: Reviewed & tested by the community » Fixed
marcvangend’s picture

Issue summary: View changes
Status: Fixed » Needs work

Now that the patch has been reverted, we should not regard this issue as fixed. On the contrary, the original problem has not been fixed and we're back at square one.

bernig’s picture

Hi, for those who need to show default images, here is a simple patch.
This is specific to image fields, and will show the block whether the image field has a default image or not.

EDIT : This is not an all-around solution, and may cause a regression if you have image field blocks that should be hidden when the field is empty.

ThomasDik’s picture

As the patch for the default value no longer applied for the default value I've updated the patch.