Hi,

I don't seem to be able to be able to set the default value of HS on my form, I have tried the following code:

$form["field_location"]["hierarchical_select"]["selects"]["0"]['#default_value']=$location;

and

$form["field_location"]['und'][0]['tid']['#default_value']=$location;

And none of them work. Could anyone offer his intake on this?

Thanks!

Comments

sokrplare’s picture

Status: Active » Closed (cannot reproduce)

Closing this out since it's been a while, but in case others stumble across take a peek at the API.txt file which shows a FAPI form element example for HS.

Hard to tell out of context, but I'm guessing you want something closer to: $form["field_location"]['#default_value']=$location;, but not certain.

druderman’s picture

Status: Closed (cannot reproduce) » Active

I can confirm that you cannot set the default using the proscribed method using FAPI if the HS field widget is set to "Save term lineage". If it's set to "Save only the deepest term", then it works.

function MYMODULE_form_alter(&$form, &$form_state, $form_id)
{
$form['field_things']['und']['#default_value'] = '3';
}

Is this a bug?

Ideally, I'd like to give my form, which has a Taxonomy HS field, a parameter so it will preselect and expand one term i.e /myform?tid=3 .

Wim Leers’s picture

Assigned: Unassigned » Wim Leers
Status: Active » Fixed

If term 3 is a root-level term, then it will work just fine when "Save term lineage" is enabled. If it is not a root-level term, but e.g. a term in the 3rd level, then … you of course need to provide the full lineage ;) For example: array(34, 746, 3).

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

TravisJohnston’s picture

Issue summary: View changes

Instead of opening another issue for the same reason, I'll just add to this one.

I am trying to set the default value after I grab information for the user filling out the form. I've tried a number of ways with no luck.

I've tired the following:

using an array

$form['registration_information'][$name][$subname]['field_massachusetts_districts_an']['und']['#default_value'] = array($parentTermID,$termID);

using the syntax shown in the API.txt file

$form['registration_information'][$name][$subname]['field_massachusetts_districts_an']['und']['#default_value'] = $parentTermID  ."|" .$termID;

and a single ID set

$form['registration_information'][$name][$subname]['field_massachusetts_districts_an']['und']['#default_value'] = $termID;

This is being down after a rebuild is sent due to the form being filled out with default values after the user makes a specific selection from a list above. I included this before hand to remove any old state data:

unset($form_state['input']['registration_information'][$name][$subname]['field_massachusetts_districts_an']);