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:

AttachmentSize
HS_pre-populated_dropbox.png6.86 KB

Comments

drupalfever’s picture

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:

$form['taxonomy'][$vid] = array(
    '#type' => (module_exists('hierarchical_select')) ? 'hierarchical_select' : '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),
  );
Wim Leers’s picture

Thanks, fixed!

rwilson0429’s picture

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

goeny’s picture

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.

Alessandro77’s picture

I am interested to point 1. If you know the solution, please let me know