4

I have a select box that I want to populate with a particular taxonomy programmatically. If i was was to use a taxonomy vocabulary how would i populate a particular select box in my form. My form is generated using the from api and not using content types. If for example I was to have a vocabulary called countries. I have a select box which currently is configured like this

 $form['status']['currentstatus'] = array (
 '#values' => array(t('red'), ('green'))


 );


$form['status']['currentStatusList'] = array (
    '#title' => 'current status',
    '#type' => 'select',
    '#options' => $form['status']['currentstatus']['#values']    
);

Instead of just adding the values I would like to pass a vocabulary.

I have manged to get the vocabulary data but a zero is placed before the terms.

enter image description here

the code I figured out for the box is the same as yours. I even pass the id for the vocabulary. I will have a play with it thanks for your help

6

You should be able to use the taxonomy_get_tree function to get all the children of the term, and then just run a foreach() on that.

Example:

function mymodule_blah($vocabulary) {
  $terms = taxonomy_get_tree($vocabulary);

  foreach ($terms as $data) {
    $output[$data->tid] = $data->name;
  }

  return $output;
}

This should return an array keyed by Term ID, although you could put whatever you want where it says $data['tid'] to put a different key in.

Example output:

[1] => ['My First Term']
[2] => ['My Second Term']
...
[15] => ['My Fifteenth Term']

Then pass this to your '#options' on your select box.

EDIT:

Forgot to mention that the helper function will need the vocabulary ID passed to it - should have added in the clarification just for completeness' sake.

EDIT 2 (full implementation):

function mymodule_myform($form) {
//The rest of your form stuff is in here
$form['status']['currentStatusList'] = array (
    '#title' => 'current status',
    '#type' => 'select',
    '#options' => mymodule_selectbox_contents(PUT THE NUMERIC VOCABULARY ID HERE)    ,
);
//The rest of your form stuff is in here
}

function mymodule_selectbox_contents($vid) {
  $terms = taxonomy_get_tree($vid);

  foreach ($terms as $data) {
    $output[$data->tid] = $data->name;
  }

  return $output;
}
9
  • i think there is a problem with the syntax for the $output
    – Alexander
    Apr 2 '12 at 16:44
  • I've edited the code, please try again :)
    – Chapabu
    Apr 2 '12 at 18:22
  • $output[$data->tid] => $data->'name'; My editor says there is a problem with the syntax. Is there anything that I am missing. I am using drupal 7 if that helps
    – Alexander
    Apr 2 '12 at 18:52
  • D'oh, I forgot to remove the 's around name! Just remove them and all should be fine!
    – Chapabu
    Apr 2 '12 at 19:11
  • i remove the single quotes around name but still get an error. Am I missing other code i need
    – Alexander
    Apr 2 '12 at 20:11
1

See Chapabu's answer; if you want to make it more dynamic, you can use taxonomy_vocabulary_machine_name_load to load the vocabulary id.

Here's a addition to Chapabu's implementation:

  function mymodule_myform($form) {
    //The rest of your form stuff is in here
    $voc = taxonomy_vocabulary_machine_name_load(your_machine_name);
    $form['status']['currentStatusList'] = array (
        '#title' => 'current status',
        '#type' => 'select',
        '#options' => mymodule_selectbox_contents($voc->vid),
    );
    //The rest of your form stuff is in here
  }

    function mymodule_selectbox_contents($vid) {
      $terms = taxonomy_get_tree($vid);

      foreach ($terms as $data) {
        $output[$data->tid] = $data->name;
      }

      return $output;
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.