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/Motivation
If you create a new field with a default value on an existing content type, the value is not set for previously created nodes, even when the nodes are edited.
Original report by NancyDru
This was originally reported against a field provider (#1827444: Default value not saved), but appears to be a core Fields issue. I added a new field to an existing content type. It has a default value. It would be nice if existing nodes were updated, but I didn't really expect it. However, editing existing content still does not save the default value. This is not good.
Comments
Comment #1
pjcdawkins CreditAttribution: pjcdawkins commentedEntity forms start off with default values for new entities. But when you're editing an existing entity, then form elements for fields & properties are populated with the existing values for that entity.
It appears to be documented well. When you edit a field, the "Default value" section has this description:
So this is surely "works as designed" unless I've misunderstood the OP.
Comment #2
NancyDruIt may work as designed, but, IMHO, the design is flawed. If a node is edited and has no value for a field with a default value, then that default should be used, new node or not. This is the way most Forms API developers create forms.
Comment #3
swentel CreditAttribution: swentel commentedAlso tempted to call it by design, or else rather a feature request than a bug. Needs to move to 8.x first
Comment #4
swentel CreditAttribution: swentel commentedOh wait, that is definitely a bug - or at least relatively easy to fix.
(edit, although, still, maybe not a bug, I don't know, depends on expectations I guess)
Comment #5
NancyDruLook at most Forms implementations, including many in core:
Maybe it's a feature request; I don't care what it's called. It is a major annoyance the way it is now.
Comment #6
pjcdawkins CreditAttribution: pjcdawkins commentedWell, the behaviour is clearly not a mistake, given the default-value-widget description I mentioned - "The default value for this field, used when creating new content". That sentence (in
field_ui.admin.inc
) appears to have been around since August 2009 according to git blame and it might even have been inherited from CCK.Perhaps a D7 Field maintainer could explain why defaults only ever applied to creating new content - maybe contact @yched.
Comment #10
drugan CreditAttribution: drugan as a volunteer commentedThe example how it could be resolved is here:
#2816859: Allow the 'step' to be configured as a NumberWidget setting
It works on the number field exactly as suggested @NancyDru on #2.
Comment #16
pameeela CreditAttribution: pameeela commentedComment #17
pameeela CreditAttribution: pameeela commentedComment #18
catchMoving this back to works as designed, if we implemented it as suggested, then even if you zeroed out a default value when you saved, it would be set back to the default next time you edited.