views:

130

answers:

2

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)
+1  A: 

(NOTE: Assuming Drupal 6 here)

Looking at a local example, my Content Taxonomy fields don't have a 'tids' property, but I only use fields of type 'optionwidgets_buttons', so this might be different if you use other widget types. Could you post a dump of the Content Taxonomy field structure from the form you are trying to manipulate?

On another note, Content Taxonomy fields have some special interaction logic concerning their 'conversion' back to the standard taxonomy array format later on in the form/node processing, which might affect you. So is your problem that your changes to the default values do not show up at all on initial form display, or do they show, but don't get processed later on?


Edit (after question update): Sorry for late reply. I'm not sure, but looking at your field definition, the type shows as hierarchical_select, which would hint on a 'standalone' hierarchical select widget. When used in conjunction with content taxonomy, the code from the 'hs_content_taxonomy' submodule would make me expect a type of content_taxonomy_hs instead. This is all just guesswork, and I do not have the time to properly test this locally myself, but you might want to check if you're using the right module combination/configuration.

Another thing would be to check your modules weight settings in relation to the hierarchical select module(s) - also just a wild guess, but maybe your alteration needs to take place before/after hierarchical select does its magic, so you might want to test with adjusted weights on your module.

Henrik Opel
Thanks for the follow-up. It turns out the documentation had anticipated my confusion, and I've added an answer to point anyone else there.
lazysoundsystem
+2  A: 

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.

lazysoundsystem