Closed (cannot reproduce)
Project:
Drupal core
Version:
8.5.x-dev
Component:
field system
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
6 Sep 2015 at 14:36 UTC
Updated:
8 Mar 2021 at 21:09 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
andypostLet's see what broken
Comment #3
stefan.r commentedComment #4
andypostTrying to check what's left with direct non-property access
Comment #6
zviryatko commentedAs a variant I've added check if
<?php $field->isEmpty(); ?>and run<?php $field->applyDefaultValue(); ?>, it resolve a problem with default field value on views and on node page.Comment #7
andypostComment #9
zviryatko commentedComment #10
daffie commentedFor the testbot
Comment #12
andypostthis could seriously affect performance
needs inline comment why we apply default value when field is empty
Comment #13
andypostComment #14
jibranIMO this is major WTF.
Comment #15
berdirDidn't look closely yet, but applying the default value for empty fields is *wrong*, we can not do that.
Having a non-required field with a default value and then not actually saving a value is a perfectly valid thing to do. get() and __get() cannot be different in regards to that?
Comment #16
zviryatko commentedI agree with that, but maybe we need way to get default value for field if it empty.
I've found this when tried to resolve this issue https://www.drupal.org/node/2348729, one of test failed, because views do not display default field value.
To reproduce this try to add node and after that add new field of type boolean to node content type, and show this field in views. Views must display default field value, but it doesn't.
Comment #17
andypost@Berdir That's exactly the same issue with fields as we have for #1191464: Inconsistent behaviour in personal contact form for users created before versus after contact module is enabled
Here the state when we add new field (boolean) with default value but existing entities have no way to display this default value cos processing of
get()and__get()is different.So depending on a way of access value (magic or getter) you inconsistently get default values
More to that... when you create field with UI then you can have no default value #1919834: Field instance got no default value when created in field UI
PS: +1 to major wtf
Comment #23
wim leersStill a problem.
I suspect this actually belongs in the
entity systemcomponent?Comment #24
berdirI think what @Wim refers to as DrupalWTF is adding a new field with a default value and then existing entities do not have it. Yeah, that is weird, but this is the wrong way to fix it. As I said above, this patch is wrong and has nothing to do with the issue title or summary.
Just because there is a default value doesn't mean that a user can not explicitly remove that and save it as empty. That is 100% valid and we must not break that.
The only case in which we could consider it is for required fields.
Also keep in mind that if we would do this, this would only fix it for entities when loading them. It does nothing to fix it in the database, so this might in fact be more confusing than the current behavior. If you'd load/edit/view your entity then the correct value would be there, but when you try to do an entity query against it for example it would not work.
Downgrading and set to normal until something can prove to me why this is a bug and can come up with code that does not break valid use cases. The only thing I can I think of right now is provide a UI to re-save all entities in a batch and set an explicit default value there.
Comment #26
jibranThis feels like a duplicate of the #2346019: Handle initial values when creating a new field storage definition after looking at the comments but IS is describing a different problem. We need an IS update.
Comment #28
andypostI think that needs
- documentation for methods to explain difference that default values are not applied when using
get()method- follow-up to discuss addition some method to get field default values, see #16
- follow-up to "to re-save all entities in a batch and set an explicit default value" when new field added with default value but there's some entities already #24 but #26 said that issue fixed partially
The main problem that lots of people using
get()method on entities when there's alredy some data in database at it takes hours to understand why that works in not expected wayComment #29
berdirDiscussed with @andypost in IRC.
I was able to clarify the misunderstanding that get() and __get() behave differently in regards to fields and default values. They do not. The fallback logic in __get() that initializes things to NULL is *only* about non-fields and is part of the BC layer that we can not remove until 9.x and I think we have issues for that.
Closing.
We still have issues like #1919834: Field instance got no default value when created in field UI about the specific problem of new fields not having a default value on existing entities.
Comment #30
andypost@Berdir is there issue to remove this BC layer in 9.x?