function taxonomy_form

You are here

4.6 taxonomy.module taxonomy_form($vid, $value = 0, $help = NULL, $name = 'taxonomy')
4.7 taxonomy.module taxonomy_form($vid, $value = 0, $help = NULL, $name = 'taxonomy')
5 taxonomy.module taxonomy_form($vid, $value = 0, $help = NULL, $name = 'taxonomy')
6 taxonomy.module taxonomy_form($vid, $value = 0, $help = NULL, $name = 'taxonomy')

Generate a form element for selecting terms from a vocabulary.

Parameters

$vid: The vocabulary ID to generate a form element for.

$value: The existing value of the term(s) in this vocabulary to use by default.

$help: Optional help text to use for the form element. If specified, this value MUST be properly sanitized and filtered (e.g. with filter_xss_admin() or check_plain() if it is user-supplied) to prevent XSS vulnerabilities. If omitted, the help text stored with the vocaulary (if any) will be used.

Return value

An array describing a form element to select terms for a vocabulary.

See also

_taxonomy_term_select()

filter_xss_admin()

1 call to taxonomy_form()
taxonomy_form_alter in modules/taxonomy/taxonomy.module
Implementation of hook_form_alter(). Generate a form for selecting terms to associate with a node. We check for taxonomy_override_selector before loading the full vocabulary, so contrib modules can intercept before hook_form_alter and provide scalable…

File

modules/taxonomy/taxonomy.module, line 434
Enables the organization of content into categories.

Code

function taxonomy_form($vid, $value = 0, $help = NULL, $name = 'taxonomy') {
  $vocabulary = taxonomy_vocabulary_load($vid);
  $help = ($help) ? $help : filter_xss_admin($vocabulary->help);

  if (!$vocabulary->multiple) {
    $blank = ($vocabulary->required) ? t('- Please choose -') : t('- None selected -');
  }
  else {
    $blank = ($vocabulary->required) ? 0 : t('- None -');
  }

  return _taxonomy_term_select(check_plain($vocabulary->name), $name, $value, $vid, $help, intval($vocabulary->multiple), $blank);
}

Comments

<?php
function generate_html(){
 
  return
drupal_get_form('example_form');
}

function

example_form() {
 
 
$form = array();
 
 
// Returns the taxonomy list with vocabulary id = 10
 
$form['taxonomy_dropdown'] = taxonomy_form(10);
 
 
 
// If you don't want to make multiple selections,
  // set the following parameter to false.
 
$form['taxonomy_dropdown']['#multiple'] = false;
 
 
// If you chose to disable multiple selection, it makes more sense
  // to make this field behave more like a drop-down field. In this
  // case you should set the following parameter to 1
 
$form['taxonomy_dropdown']['#size'] = 1;
 
  return
$form;
}
?>

Does anyone know what to use instead of this function for D7? Or explain why it's not in D7 anymore? I've searched for awhile, but I can't find any documentation on this. Thanks!

Unfortunately, it looks like this function was simply blown out in D7, with no upgrade guide docs or information about it.

The only way to do this now is to build your own FAPI options array and form field... annoying, but it could be as simple as:

<?php
  $categories
= db_query("SELECT tid, name FROM {taxonomy_term_data} WHERE vid = :vid", array(
   
':vid' => [Vocabulary ID],
  ))->
fetchAllKeyed();

 

$form['category'] = array(
   
'#type' => 'select',
   
'#options' => $categories,
  );
?>

  $form = array();
  $terms = taxonomy_get_tree($vocabulary_id);
  $terms_select = array();
  foreach ($terms as $term) {
   $terms_select[$term->tid] = str_repeat('-', $term->depth) . $term->name;
  }
  $form['term_dropdown] = array(
    '#type' => 'select',
    '#title' => t('Select the term'),
    '#options' => $terms_select,
    '#required' => TRUE,
  );

peter.milans solution is best as it also includes indenting the terms with "-" based on depth.