Aman Goyal
Aman Goyal

Reputation: 46

How to set the first option to null in a drupal drop down list?

A drop list is generated from a database, and the first option is always selected. How can I set the first option blank and display the message "Select option" in the first option? This is my source code:

$vocabulary = taxonomy_vocabulary_machine_name_load('asset_type');
$tree = taxonomy_get_tree($vocabulary->vid);
foreach ($tree as $item) {
    $options[$item->name] = str_repeat('-', $item->depth) . $item->name;
}
$form['search_fieldset']['doc_type']=array( '#type'=>'select', '#title'=>
    t('Document Type'),'#options' => $options, );

Upvotes: 0

Views: 76

Answers (2)

Aman Goyal
Aman Goyal

Reputation: 46

"#empty_option"=>t('- Select -'),

Upvotes: 0

Manuel Alejandro
Manuel Alejandro

Reputation: 598

Try this:

$vocabulary = taxonomy_vocabulary_machine_name_load('asset_type');
$tree = taxonomy_get_tree($vocabulary->vid);
foreach ($tree as $item) {
    $options[$item->name] = str_repeat('-', $item->depth) . $item->name;
}

array_unshift($options, array("-" => "Select option"));

$form['search_fieldset']['doc_type']=array( '#type'=>'select', '#title'=>
    t('Document Type'),'#options' => $options, );

Notice the array_unshift function which insert the desired option at the beginning.

Upvotes: 1

Related Questions