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);
   }
Files: 
CommentFileSizeAuthor
#5 drupal-1342984-5.patch1.35 KBtim.plunkett
PASSED: [[SimpleTest]]: [MySQL] 34,368 pass(es). View
#5 drupal-1342984-5-test-only.patch636 bytestim.plunkett
FAILED: [[SimpleTest]]: [MySQL] 34,366 pass(es), 1 fail(s), and 1 exception(es). View
#4 fix-default-value-function-callback-1342984-4.patch1.3 KBjox
PASSED: [[SimpleTest]]: [MySQL] 37,261 pass(es). View
#1 fix-default-value-function-callback-1342984-1.patch716 bytesjox
FAILED: [[SimpleTest]]: [MySQL] 37,268 pass(es), 1 fail(s), and 1 exception(es). View

Comments

jox’s picture

FileSize
716 bytes
FAILED: [[SimpleTest]]: [MySQL] 37,268 pass(es), 1 fail(s), and 1 exception(es). View

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
PASSED: [[SimpleTest]]: [MySQL] 37,261 pass(es). View

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
FAILED: [[SimpleTest]]: [MySQL] 34,366 pass(es), 1 fail(s), and 1 exception(es). View
1.35 KB
PASSED: [[SimpleTest]]: [MySQL] 34,368 pass(es). View

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.