I'm trying to define some fields for the user, I can create and delete them but I can't figure out how to define the default_value_function to calculate the default value for the fields said.

The documentation (http://api.drupal.org/api/drupal/modules--field--field.module/group/field/7) doesn't propose any example.

default_value_function (string) The name of the function, if any, that will provide a default value.

My field instance definition:

      'field_test' => array(
      'field_name' => 'field_test',
      'label'       => $t('Test field'),
      'widget'      => array(
        'type'    => 'text_textfield',
      'display' => array(
        'test_field_list' => array(
          'label' => 'hidden',
          'type' => 'field_test',
      'default_value_function' => '_mymodule_get_attribute()',

The function:

  function _mymodule_get_attribute() {
    return 'foo';

Maybe I'm missing something, any idea? Also, if I want to pass a variable to the _mymodule_get_attribute function, what's the correct syntax to use?



I'll +1 this... very frustrating. I tried the approach listed here, also, to no avail.

I just downloaded Drupal 7.8, did a search for files that contain "default_value_function". Bingo, there is an example in modules/field/tests/field.test. Compare the Drupal 7.8 usage with yours. You'll see what needs to be changed in your code.

I recommend doing the same thing. Do a search on the Drupal source. Always are great resource :)

When in doubt, have the Leprechaun take care of it!

Download the entire Drupal 7.8 base for the solution?

I was stuck on this too. Here's what I found that helped me.

1. 'default_value_function' needs to appear as a key in the return array from hook_field_info.
2. If there are any instances that have ALREADY been created, they require a database update using field_update_instance; something you can do in a hook_update_n, for instance.

The kicker was that I couldn't figure out how to get my already created instances to read the data from hook_field_info, as clearing caches did not work.

Again the records of those existing instance settings in the database need to be updated.

Hope this saves someone time.

This works in Drupal 7.26
'default_value' => array(array('value' => "1")),

$form['body'] = array(
'#type' =>
'#title' => t('Body'),
'#default_value' => $node->body,
'#required' => TRUE,

From https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7#default_value