1

I am trying to alter a form with a taxonomy term select drop down field; I want to change the default term selected when the form loads. I'm altering the form array via hook_form_alter; however, my changes do not seem to be taking effect. I'm looking for an answer similar to this but for Drupal 7.

I'm expecting :

        $form['field_mytax_reference']['#default_value'] = array($taxTermId => $taxTermName);

to do the job but the form remains with the default specified when the field was created.

Thanks in advance.

3

It'll be something more along the lines of:

$form['field_mytax_reference'][LANGUAGE_NONE][0]['#default_value'] = $taxTermId;

If the field is a multi-select you would wrap the default value in an array, but there's no need to set a key for it:

$form['field_mytax_reference'][LANGUAGE_NONE][0]['#default_value'] = array($taxTermId);

Have a look at Why is hook_form_alter() so messy? for a bit more info.

3

This worked:

$form['field_mytax_reference']['und']['#default_value'] = array($term_tid);

Clive's answer was a helpful point in the right direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.