Reputation: 46
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
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