How to pre-populate the dropbox

Last updated on
30 April 2025

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:

Help improve this page

Page status: Not set

You can: