Current best practice on default values

Events happening in the community are now at Drupal community events on www.drupal.org.
Torenware's picture

I'm trying to figure out a problem in the location_cck module, which is having problems with default values (#416230: Location CCK field does not set default location). Part of the difficulty in fixing this: the lack of consistent documentation on how defaults should be handled.

It looks like the current handbook may be wrong, since some modules have recently changed their implementations. Details below.

Right now, the handbook mentions a hook_default_value:

<?php
/**
* sets default values for the field.
<em>
</em> To get this hook actually called, make sure to set
*   'default value' => CONTENT_CALLBACK_CUSTOM
* on hook_widget_info.
<em>/
function hook_default_value(&$form, &$form_state, $field, $delta) {

  $items = array();
  $field_name = $field['field_name'];

  switch ($field['multiple']) {
    case 0:
      $max = 1;
      break;
    case 1:
      $max =  isset($form_state['item_count'][$field_name]) ? $form_state['item_count'][$field_name] : 1;
      break;
    default:
      $max = $field['multiple'];
      break;
  }

  for ($delta = 0; $delta < $max; $delta++) {
    $items[$delta] = array(
      /</em> YOUR STUFF HERE */
    
);
  }
  return
$items;
}
?>

I've been looking through modules like date.module and imagefield.module to see how they handle default values, and AFAICT, these modules implemented this hook in the past, but do not do so in their current versions. Comments in imagefield.module suggest that the developer was unable to get this hook to work.

Is it busted? Or is there a better way to implement default values?

Can someone point me to some sample code, or to a good implementation of default value handling?

Content Construction Kit (CCK)

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: