2014-09-18 2 views
0

Je souhaite créer une taxonomie hiérarchique à l'aide de mon module personnalisé. Je reçois toute la liste de vocabulaire en utilisant taxonomy_get_vocabularies().Comment créer une taxonomie hiérarchique par programmation drupal 7

Mon problème est que lorsque je sélectionne un vocabulaire ou un parent parent doit être la liste dans une autre liste de sélection/liste déroulante.

Mon code: -

enter code here 

<?php 
function student_addform($form, &$form_state) { 

    $vocabulary = taxonomy_get_vocabularies(); 
    $checklist_vocab_array = array(); 
    foreach ($vocabulary as $item) { 
    $vocab_vid = $item->vid; 
    $vocab_name = $item->name; 
    $checklist_vocab_array[$vocab_vid] = $vocab_name; 
    } 

    $form['vocabulary_list'] = array(
    '#type'    => 'select', 
    '#title'   => t('List of current Classes.'), 
    '#position'   => 'left' , 
    '#options'   => $checklist_vocab_array , 
    '#required' => TRUE, 
    '#description'  => t('List of classes displayed as dropdown'), 
    '#ajax' => array(
     'callback' => '_student_records_callback_fields', 
     'wrapper' => 'check-box-replace', 
     'effect' => 'fade', 
     'event' => 'change' , 
    ), 
); 

    $form['child-list'] = array(
    '#type' => 'select', 
    '#title' => t('Select fields of'), 
    'options' => $term_list, 
    '#prefix' => '<div id="check-box-replace">', 
    '#suffix' => '</div>', 
); 

    return $form; 
} 

function _student_records_callback_fields($form, $form_state) { 
    $term_list = array(); 
    $vocabulary_id = $form['vocabulary_list']['#value']; 
    $terms = taxonomy_get_tree($vocabulary_id); // Use the correct vocabulary id. 
    $count = count($terms); 
    for ($term = 0 ; $term < $count ; $term++) { 
    $term_list[$terms[$term]->vid] = $terms[$term]->name; 
    } 

    return $term_list; 
} 
?> 

Merci,

Répondre

0

Vous devez toujours créer des éléments de formulaire dans la fonction de forme, et non pas dans le rappel.
Lorsque le rappel est déclenché, la fonction de formulaire est appelée à nouveau vous permettant de le modifier. Le rappel renvoie généralement uniquement l'élément de formulaire.
Donc ...

enter code here 

<?php 
function student_addform($form, &$form_state) { 

    $vocabulary = taxonomy_get_vocabularies(); 
    $checklist_vocab_array = array(); 
    foreach ($vocabulary as $item) { 
    $vocab_vid = $item->vid; 
    $vocab_name = $item->name; 
    $checklist_vocab_array[$vocab_vid] = $vocab_name; 
    } 

    $form['vocabulary_list'] = array(
    '#type'    => 'select', 
    '#title'   => t('List of current Classes.'), 
    '#position'   => 'left' , 
    '#options'   => $checklist_vocab_array , 
    '#required' => TRUE, 
    '#description'  => t('List of classes displayed as dropdown'), 
    '#ajax' => array(
     'callback' => '_student_records_callback_fields', 
     'wrapper' => 'check-box-replace', 
     'effect' => 'fade', 
     'event' => 'change' , 
    ), 
); 

    // Check if a vocabulary is selected, if it is get the children and populate the second dropdown. 

    //something like this. 

    // $term_list = array(); 
    // if(vocabulary is selected){ 
    // $term_list = term children 
    // } 

    // you should be able to check if a vocabulary is selected by checking the 
    // $form_state['values'] or $form_state['input'] (I forget which) 

    $form['child-list'] = array(
    '#type' => 'select', 
    '#title' => t('Select fields of'), 
    'options' => $term_list, 
    '#prefix' => '<div id="check-box-replace">', 
    '#suffix' => '</div>', 
); 

    return $form; 
} 

function _student_records_callback_fields($form, $form_state) { 
    return $form['child-list']; 
} 
Questions connexes