Get single level of taxonomy terms as array for form #option properties

Public

Returns a single level of terms in an array suitable for #options elements in form arrays. Great for ajax forms where the user needs to drill down from a parent term to a more granular term.

</> CopyGet raw version
php
  1. /**
  2.  * Gets a single level of terms as an array suitable for use in form arrays.
  3.  *
  4.  * @param int $vid
  5.  * The Vocabulary ID
  6.  * @param int $tid
  7.  * Optional Term ID. If present, will return a single level of that
  8.  * term's children.
  9.  *
  10.  * @return array
  11.  * An array of taxonomy terms keyed by TID with the term name as the value.
  12.  * Suitable for use in #options arrays in form elements.
  13.  */
  14. function MODULE_get_term_option_array($vid, $tid = NULL) {
  15. $terms = ($tid) ? taxonomy_get_tree($vid, $tid, 1, TRUE) : taxonomy_get_tree($vid, 0, 1, TRUE);
  16. foreach ($terms as $t) {
  17. $options_array[$t->tid] = $t->tid;
  18. }
  19. return $options_array;
  20. }