phpdrupaldrupal-fapidrupal-alter

Default values for Content Taxonomy fields in Drupal with Hierarchical Select widget


I'm trying to set the default value for a Content Taxonomy field in a hook_form_alter, but can't pin down the necessary format. I've tried this and many variations:

foreach (element_children($form) as $child) {
// Set $default_value.

  if ($form[$child]['tids']) {
// This, for Content Taxonomy fields, isn't working:
    $form[$child]['tids']['#default_value'] = array('value' => $default_value);
    dsm($form[$child]['tids']['#default_value']);
  }
  else {
// This, for other fields, is working:
    $form[$child][0]['#default_value']['value'] = $default_value;
  }
}

Can anyone tell me what I'm missing?

Edit: In response to Henrik Opel (thanks for getting involved), here is the print out of the relevant field of the form with my changes to the default fields commented out, showing the '#default_value' field I'm trying to influence.

It also shows that the option widget I'm using is Hierarchical Select (could this be a factor?). In the dsm() in the code above, the changes to the default value are recognised, but they don't get processed later on.

field_name_of_content_taxonomy_field (Array, 3 elements)
  #tree (Boolean) TRUE
  #weight (String, 1 characters ) 5
  tids (Array, 7 elements)
    #title (String, 10 characters ) Vocabulary_name
    #type (String, 19 characters ) hierarchical_select
    #weight (String, 1 characters ) 5
    #config (Array, 15 elements)
      // 15 elements here
    #required (String, 1 characters ) 0
    #description (String, 0 characters )
    #default_value (Array, 0 elements)

Solution

  • Turns out the answer was in the documentation: http://drupal.org/node/319190

    I've renamed the question to make it clear where the problem was. This narrows the question to a rather specific use-case, but both Content Taxonomy and Hierarchical Select are useful and recommended modules for dealing with large taxonomies.