I am currently working on a custom registration form module. While there are a lot of posts about this, none of them really provide a clear picture of how to populate a select list from taxonomy terms programmatically.

The machine name of the taxonomy terms is 'cities'. Here is the code so far:

  $form['cities'] = array(
    '#type' => 'select',
    '#title' => t('Click on your city'),
    '#multiple' => true,
    '#options' => ???,
    '#description' => t('Click on one or more cities.'),
  );

Comments

shyamsukhamit’s picture

just insert a field in the form select the type as term refernce and then select the taxonomy term as city.
the field is populate...

creativepragmatic’s picture

I do not have extensive module development experience so I am not sure how to express this in PHP code. Is this what you mean?

  $form['cities'] = array(
    '#type' => 'term reference',
    '#taxonomy' => 'cities',
    '#title' => t('Click your city'),
    '#multiple' => true,
    '#required' => TRUE,
  );
shyamsukhamit’s picture

me too don't have module development experience , but as the term reference are multiple I think there should be some kind of loop to fetch all the values..

sorry If I am wrong..

creativepragmatic’s picture

This is what I ended up with:

  $cities = db_query("SELECT tid, name FROM {taxonomy_term_data} WHERE vid = 1")->fetchAllKeyed();
  
  $form['cities'] = array(
    '#type' => 'select',
    '#title' => t('Click on your city'),
    '#multiple' => true,
    '#options' => $cities,
    '#description' => t('Click on one or more cities.'),
  );

I hoped to find a function that would take the machine name of the taxonomy term and return results that match the query. I checked the taxonomy functions but didn't find anything that would do this. Is there a more elegant way of expressing this?

henrikakselsen’s picture

It doesn't help with the name/id thing, but there is a function called taxonomy_get_tree that is helpful with this kind of things so you don't have to query the db yourself.

Henrik Akselsen | frontkom | Twitter: @FrontHenrik

creativepragmatic’s picture

That's why I couldn't find a function in Drupal Core. I was being too specific in my search. taxonomy_get_tree is a more generic function than what I was searching for. Thank you for taking the time to explain that Henrik.

davidwhthomas’s picture

Here's how to do it in Drupal 7, for those looking:

// 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;
tgshannon’s picture

I was just looking for a solution and found the answer in your recent post. THANKS!

er.pushpinderrana’s picture

So useful code in most of cases.

Thank you for sharing this nice solution.

Pushpinder Rana #pushpinderdrupal
Acquia Certified Drupal Expert

manuel_mra’s picture

Thanks.
It's easy to do it ... with help ...

Manuel Ruiz Alvarez
Lima - Perú
South America

DrCord’s picture

Thanks for this snippet, it is exactly what the doctor ordered, clean and to the point.

manuel_mra’s picture

By this way it was easy to solve filling a select list in an API form.
Thank you.

Manuel Ruiz Alvarez
Lima - Perú
South America

emanaton’s picture

In case anyone else was looking for the Drupal 6 version of this answer:

<?php
$vid = variable_get('my_stored_vid', FALSE);
$form['category_default'] = taxonomy_form($vid);
unset($form['category_default']['#weight']);
$form['category_default']['#title'] = t('Default category');
$form['category_default']['#default_value'] = variable_get('category_default', 0);
$form['category_default']['#description'] = t('The selected category will be shown by default on listing pages.');
?>
aguchoca’s picture

I already have my taxonomy vocabulary but it does return an empty array ?

any advice ?

Thanks

chippyjacob’s picture

This is working for me...Thank you

manuel_mra’s picture

Manuel Ruiz Alvarez
Lima - Perú
South America

jlongbottom’s picture

$terms = taxonomy_get_tree($vid);

building elegant solutions efficiently

pramodk’s picture

  $results = db_query("SELECT tid, name FROM {taxonomy_term_data} WHERE vid = 1")->fetchAll();

$options = array();

foreach($results  as $key=>$value){
$options[$key] = $vlaue;
}

$form['cities'] = array(
    '#type' => 'select',
    '#title' => t('Click on your city'),
    '#multiple' => true,
    '#options' => $options,
    '#description' => t('Click on one or more cities.'),
  );
chippyjacob’s picture

This does not works for me..Anyway thank you

berramou’s picture

- try this function to get vocabulary and pass it to options :

/**
 * @return array of all vocabulary of city taxonomy
 *
 */
function _get_cities_helper_function(){
  $options = array();
  $vid = taxonomy_vocabulary_machine_name_load('cities')->vid;
  $options_source = taxonomy_get_tree($vid);
  foreach($options_source as $item ) {
    $key = $item->tid;
    $value = $item->name;
    $options[$key] = $value;
  }
  return $options;
}
karthid’s picture

$options = db_query("SELECT tid, name FROM {taxonomy_term_data} WHERE vid = 1")->fetchAllKeyed(0,1);

$form['cities'] = array(
    '#type' => 'select',
    '#title' => t('Click on your city'),
    '#multiple' => true,
    '#options' => $options,
    '#description' => t('Click on one or more cities.'),
  );