Community Documentation

How to pre-populate the dropbox

Last updated August 8, 2011. Created by Wim Leers on October 9, 2008.
Log in to edit this page.

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:

<?php
$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

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:

<?php
$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),
  );
?>

This is the site I crash often: http://www.drupalfever.com

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?

Reggie W

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 5.x, Drupal 6.x, Drupal 7.x
Audience
Developers and coders

Site Building Guide

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.