Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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:
- 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.
- 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.
Comment | File | Size | Author |
---|---|---|---|
#21 | render_field_default_value-2613144-21.patch | 561 bytes | ThomasDik |
#20 | render_image_field_default_value-2613144.patch | 777 bytes | bernig |
Comments
Comment #2
marcvangendThanks 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.Comment #3
marcvangendHere's the patch to test. BTW it should apply to all default values, not just image fields.
Comment #4
marcvangendOops, file didn't upload.
Comment #5
marcvangendPerhaps we should change
$field->access('view', $account, TRUE)
to$field->access('view', $account)
as per #2665114: access check could be much better.Comment #6
NewZeal CreditAttribution: NewZeal at Passing Phase Web Development commentedThe patch fixes it for me. Default image now displays where previously it didn't. Thanks.
Comment #7
marcvangendThanks for your feedback. Let's consider this RTBC then, I'll try to commit and release asap.
Comment #9
Sutharsan CreditAttribution: Sutharsan commentedComment #10
dgDatpasst CreditAttribution: dgDatpasst commentedThis 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.
Comment #11
marcvangend@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.
Comment #12
Sutharsan CreditAttribution: Sutharsan at LimoenGroen commented@dgDatpasst, Can you describe the situation that is causing the problem? Just to be sure we are talking about the same.
Comment #13
dgDatpasst CreditAttribution: dgDatpasst commented@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?
Comment #14
marcvangendI 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:
$field->isEmpty()
$fieldDefinition->getDefaultValue()
[]
(empty array)[ 0 => [ 'value' => 'my default value' ] ]
This means we have two problems when handling default values in our blocks:
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.
Comment #15
marcvangendPatch for reverting commit 010d52a.
Comment #16
Sutharsan CreditAttribution: Sutharsan at LimoenGroen commentedThis reverts the #4 patch.
Comment #18
Sutharsan CreditAttribution: Sutharsan at LimoenGroen commentedComment #19
marcvangendNow 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.
Comment #20
bernig CreditAttribution: bernig as a volunteer commentedHi, 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.
Comment #21
ThomasDik CreditAttribution: ThomasDik at Synetic commentedAs the patch for the default value no longer applied for the default value I've updated the patch.