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);
   }
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jox’s picture

What do you mean exactly? Are you talking about the 'default_value_function' callback, which is actually used in field_get_default_value()?

/modules/field/field.module:

function field_get_default_value($entity_type, $entity, $field, $instance, $langcode = NULL) {
  $items = array();
  if (!empty($instance['default_value_function'])) {
    $function = $instance['default_value_function'];
    if (function_exists($function)) {
      $items = $function($entity_type, $entity, $field, $instance, $langcode);
    }
  }
  ...

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:

function mymodule_field_info() {
  return array(
    'myfield' => array(
      ...
      'instance_settings' => array(
        'default_value_function' => 'mymodule_my_default_value_function',
      ),
      ...
    )
  );
}

function mymodule_my_default_value_function() {
   ...
}
jox’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, fix-default-value-function-callback-1342984-1.patch, failed testing.

jox’s picture

Status: Needs work » Needs review
FileSize
1.3 KB

The test needs the same adjustment.

tim.plunkett’s picture

Version: 7.x-dev » 8.x-dev
Issue tags: +Needs backport to D7
FileSize
636 bytes
1.35 KB

Moving 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.

Status: Needs review » Needs work

The last submitted patch, drupal-1342984-5-test-only.patch, failed testing.

tim.plunkett’s picture

Status: Needs work » Needs review

Setting back.

jox’s picture

Thanks for moving to 8 and testing properly!

tim.plunkett’s picture

Assigned: Unassigned » tim.plunkett
Status: Needs review » Needs work

There's a missing change for field_ui_field_edit_form(), and the test coverage doesn't really test anything.

klonos’s picture

klonos’s picture

bumping.

yched’s picture

Assigned: Unassigned » tim.plunkett

'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']

tim.plunkett’s picture

Assigned: tim.plunkett » Unassigned

Yeah not sure how I got that one wrong, oh well. Back to the drawing board.

klonos’s picture

Assigned: tim.plunkett » Unassigned

bumping again.

tim.plunkett’s picture

Unless you're planning on working on this, please please stop "bumping" it. I'm not sure what your intention is by doing that.

klonos’s picture

Sorry 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.

fizk’s picture

klonos’s picture

I 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.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

quietone’s picture

Issue summary: View changes
Status: Needs work » Closed (cannot reproduce)
Issue tags: +Bug Smash Initiative

I 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!