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

StatusFileSize
new716 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:

<?php
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:

<?php
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() {
   ...
}
?>

Status:Active» Needs review

Status:Needs review» Needs work

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

Status:Needs work» Needs review
StatusFileSize
new1.3 KB
PASSED: [[SimpleTest]]: [MySQL] 37,261 pass(es).
[ View ]

The test needs the same adjustment.

Version:7.x-dev» 8.x-dev
Issue tags:+needs backport to D7
StatusFileSize
new636 bytes
FAILED: [[SimpleTest]]: [MySQL] 34,366 pass(es), 1 fail(s), and 1 exception(es).
[ View ]
new1.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.

Status:Needs work» Needs review

Setting back.

Thanks for moving to 8 and testing properly!

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.

bumping.

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

Assigned:tim.plunkett» Unassigned

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

Assigned:tim.plunkett» Unassigned

bumping again.

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

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.

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.