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.
I think there's a bug in the field module. I couldn't use the default value function without the following patch.
diff --git a/modules/field/field.form.inc b/modules/field/field.form.inc
index 4b92501..dd7b30d 100644
--- a/modules/field/field.form.inc
+++ b/modules/field/field.form.inc
@@ -22,7 +22,7 @@ function field_default_form($entity_type, $entity, $field, $instance, $langcode,
$addition[$field_name] = array();
// Populate widgets with default values when creating a new entity.
- if (empty($items) && empty($id)) {
+ if (empty($items) || empty($id)) {
$items = field_get_default_value($entity_type, $entity, $field, $instance, $langcode);
}
Comment | File | Size | Author |
---|---|---|---|
#5 | drupal-1342984-5.patch | 1.35 KB | tim.plunkett |
#5 | drupal-1342984-5-test-only.patch | 636 bytes | tim.plunkett |
#4 | fix-default-value-function-callback-1342984-4.patch | 1.3 KB | jox |
#1 | fix-default-value-function-callback-1342984-1.patch | 716 bytes | jox |
Comments
Comment #1
jox CreditAttribution: jox commentedWhat do you mean exactly? Are you talking about the
'default_value_function'
callback, which is actually used infield_get_default_value()
?/modules/field/field.module:
If so, could you describe how and where you defined the callback?
Because I am not able to use that callback without applying the attached patch and defining the callback like this:
Comment #2
jox CreditAttribution: jox commentedComment #4
jox CreditAttribution: jox commentedThe test needs the same adjustment.
Comment #5
tim.plunkettMoving to 8 and uploading both the test, and the patch and test.
The patch will have to be rerolled for D7 because of the removal of function_exists for D8 callbacks.
Comment #7
tim.plunkettSetting back.
Comment #8
jox CreditAttribution: jox commentedThanks for moving to 8 and testing properly!
Comment #9
tim.plunkettThere's a missing change for field_ui_field_edit_form(), and the test coverage doesn't really test anything.
Comment #10
klonos...blocks #1239946: Embedded field collection items with a default value result in new items on save
Comment #11
klonosbumping.
Comment #12
yched CreditAttribution: yched commented'default_value_function' is a property that lives directly under $instance, it's not expected under $instance['settings'] (nothing is "expected" in $instance['settings'], since what's in there is entirely up to the field type).
It's just that $instance['default_value_function'] can only be set through programmatic field_create_instance() / field_update_instance(), Field UI only lets you edit $instance['default_value']
Comment #13
tim.plunkettYeah not sure how I got that one wrong, oh well. Back to the drawing board.
Comment #14
klonosbumping again.
Comment #15
tim.plunkettUnless you're planning on working on this, please please stop "bumping" it. I'm not sure what your intention is by doing that.
Comment #16
klonosSorry Tim, didn't mean to upset anyone. As I said back in #10 this one blocks (or was blocking - it recently got fixed) #1239946: Embedded field collection items with a default value result in new items on save. So, my bump only meant to do what bump posts normally do: bring an issue buried under 100s of issues back to people's radar.
Comment #17
fizk CreditAttribution: fizk commented@klonos, is this issue resolved by the commit in #1239946: Embedded field collection items with a default value result in new items on save ?
Comment #18
klonosI doubt it. That issue was blocked on this one at some point, but since it got fixed I guess people found a way to work around whatever blocker.
Comment #27
quietone CreditAttribution: quietone as a volunteer commentedI tested this on Drupal 9.3.x and Drupal 7.80-dev with an integer field. I created the field on the basic page content type and set a default value. In both cases the default value worked as expected, the default value was saved when the node was saved. I also set a minimum value and that worked just fine in both cases. With the exception that there is a regression in D9, covered in #2907520: Field UI doesn't validate a field's default value on create.
Therefore, closing as cannot reproduce. If you are experiencing this problem reopen the issue, by setting the status to 'Active', and provide complete steps to reproduce the issue (starting from "Install Drupal core").
Thanks!