2010-03-11 40 views
6

Estoy un poco confundido. He creado un formulario simple con un cuadro de texto y un botón de enviar. Ahora quiero agregar un cuadro desplegable de selección/opción de términos de taxonomía, utilizando la función taxonomy_get_vocabularies().drupal - agregar lista de selección/opción a un formulario

$vocabularies = taxonomy_get_vocabularies('my_type'); 

Mi pregunta es ¿cómo obtengo la lista de vocabulario en la forma "la forma de Drupal". La forma en que Drupal define la forma parece bastante rígida. También cómo podría hacer esta condición, por ejemplo, sobre la existencia de términos de taxonomía relevantes.

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; 

Respuesta

11

estoy haciendo algo similar en una forma personalizada, y se encontró que sea mucho más fácil de usar taxonomy_get_tree, con el código de vocabulario como argumento de la función. Vea a continuación:

//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); 
0

Investigar cómo hacerlo en el archivo taxonomy.admin.inc del módulo de taxonomía

/** 
* Form builder to list and manage vocabularies. 
* 
* @ingroup forms 
* @see taxonomy_overview_vocabularies_submit() 
* @see theme_taxonomy_overview_vocabularies() 
*/ 
function taxonomy_overview_vocabularies() { 
    $vocabularies = taxonomy_get_vocabularies(); 
    $form = array('#tree' => TRUE); 
    foreach ($vocabularies as $vocabulary) { 
    ... 
0

gracias por su pronta respuesta! Creo que lo resolví así.

$form['limiter'] = array(
    '#type' => 'select', 
    '#title' => t('Choose a value'), 
    '#id' => 'limiter', 
    '#options' => get_faq_terms(), 
); 

function get_faq_terms() { 
    // get the vid value from vocabulary_node_types file 
    $result = db_query("SELECT * FROM vocabulary_node_types WHERE type = 'my_type' "); 
    $node = db_fetch_object($result) ; 
    $vid = $node->vid ; 

    // get corresponding term names from term_data file 
    $items = array(); 
    $terms = taxonomy_get_tree($vid); 
    foreach ($terms as $term) { 
     $count = taxonomy_term_count_nodes($term->tid); 
     if ($count) {  
      $items[$term->tid] = $term->name; 
     } 
    } 
+0

Debe utilizar los comentarios para responder a los mensajes, se debe provocar puestos adicionales tuyo. – jergason

+0

lo siento, pensé que mi comentario era un poco detallado para el formato de "comentario". Por cierto, si alguien tiene una mejor solución, háganos saber. También sería útil un ejemplo para taxonomy_get_vocabularies(). –

1

esta es la forma Drupal - _taxonomy_term_select()

+2

no para drupal 7 – FLY

1

Creo que se puede utilizar la función: taxonomy_form

Aquí tienes la doumentation: taxonomy_form

+0

<= drupal 6 solamente – DrCord

2

tengo escrito esta función auxiliar para mi módulo (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; 
} 

A continuación, puede llamar a esta función en sus #opciones en su formulario $:

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

He aquí cómo hacerlo en 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; 
Cuestiones relacionadas