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.
In some cases, you'll want to pre-populate the dropbox. The answer is very simple. Use #default_value
.
Example
Here's an example for a taxonomy Hierarchical Select.
The vocabulary with vid 3 has the following terms:
- 6: Ford
- 7: Fiesta
The following piece of code:
$form['taxonomy'][$vid] = array(
'#type' => 'hierarchical_select',
'#title' => t('My taxonomy select'),
'#config' => array(
'module' => 'hs_taxonomy',
'params' => array(
'vid' => 3,
),
'save_lineage' => 1,
'enforce_deepest' => 0,
'resizable' => 0,
'level_labels' => array(
'status' => 0,
),
'dropbox' => array(
'status' => 1,
'title' => t('Cars'),
),
'editability' => array(
'status' => 0,
),
),
'#default_value' => array(6, 7),
);
would result in:
Attachment | Size |
---|---|
HS_pre-populated_dropbox.png | 6.86 KB |
Comments
Here is a little correction
Here is a little correction on the above code.
You need to declare the "#type" field option as "hierarchical_select" otherwise the code is not going to work.
Following is the code with the little correction:
Thanks, fixed!
Thanks, fixed!
Where do I add this code??
So, many places on this site, shows code to add to add functionality or fix a problem but, very few tell us dummies where the code should be added. So, excuse my ignorance but, where should I add this piece of code?
ReggieW
two questions
1) apparently the example shows how to prepopulate the Dropbox with fixes values (taxonomy items 6 and 7). But what if I edit a previosly saved node with other taxonomy? How can I prepoluate the node with previosly saved values?
2) As rwilson0429 was saying I don't know where write this piece of code.
I am interested to point 1.
I am interested to point 1. If you know the solution, please let me know