Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #1
sokrplare CreditAttribution: sokrplare commentedClosing 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.Comment #2
druderman CreditAttribution: druderman commentedI 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 .
Comment #3
Wim LeersIf 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)
.Comment #5
TravisJohnston CreditAttribution: TravisJohnston commentedInstead 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
using the syntax shown in the API.txt file
and a single ID set
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: