drupal - add select/option list to a form

32,185

Solution 1

I'm doing something similar in a custom form, and found it much easier to use taxonomy_get_tree, with the vocabulary code as the function's argument. See below:

//get the list of locations from taxonomy to use in the dropdown
$dropdown_source = taxonomy_get_tree(2);
$dropdown_array = array('0' => '--none--');
foreach ($dropdown_source as $item) {
$key = $item->tid;
$value = $item->name;
$dropdown_array[$key] = $value;
}

//location filter dropdown
$form['filterset']['locationfilter'] = array(
  '#weight' => '1',
  '#key_type' => 'associative',
  '#multiple_toggle' => '1',
  '#type' => 'select',
  '#options' => $dropdown_array,
  '#title' => 'Filter by location',
);

unset($dropdown_array);

Solution 2

I've written this helper function for my module ( drupal 7 ):

/**
 * helper function to get taxonomy term options for select widget
 * @arguments string $machine_name: taxonomy machine name
 * @return array of select options for form
 */
function MYMODULE_get_tax_term_options($machine_name){
    $options = array( '0' => '');

    $vid = taxonomy_vocabulary_machine_name_load($machine_name)->vid;

    $options_source = taxonomy_get_tree($vid);

    foreach($options_source as $item ) {
        $key = $item->tid;
        $value = $item->name;
        $options[$key] = $value;
    }

    return $options;
}

Then you can call this function on your #options in your $form:

$form['field_name'] = array(    
  '#options' => MYMODULE_get_tax_term_options('taxonomy_machine_name'),
);

Solution 3

this is the drupal way - _taxonomy_term_select()

Solution 4

I think you can use the function: taxonomy_form

Here you have the doumentation: taxonomy_form

Solution 5

Here's how to do it in Drupal 7

// Populate FAPI select box from vocabulary term values.
// In this case term_reference field is field_category
$form = array();
$form['category_default'] = array(
  '#type' => 'select',
  '#title' => t('Default category'),
  '#options' => taxonomy_allowed_values(field_info_field('field_category')),
  '#description' => t('The selected category will be shown by default on listing pages.')
);
return $form;
Share:
32,185
harry_T
Author by

harry_T

Updated on July 09, 2022

Comments

  • harry_T
    harry_T almost 2 years

    I am a bit confused. I have created a simple form with a one text box and a submit button. Now I want to add a select/option dropdown box of taxonomy terms, using the taxonomy_get_vocabularies() function.

     $vocabularies = taxonomy_get_vocabularies('my_type'); 
    

    My question is how do I get vocabulary list onto form "the Drupal way". The way Drupal defines form seem quite rigid. Also how could I make this conditionl, say on existence of relevant taxonomy terms.

    function my_form_name($form_state) {
    
    // A Short question.
      $form['title'] = array(
        '#type' => 'textfield',
        '#title' => t('Question'),
        '#default_value' => $node->title,
        '#required' => TRUE,
        '#weight' => 1,
        '#description' => t('A text box goes here '),   
      );
    
      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('submit'),
        '#weight' => 7,
      );
    
      return $form;