【问题标题】:How to create hierarchical taxonomy programmatically drupal 7如何以编程方式创建分层分类drupal 7
【发布时间】:2014-09-18 11:18:47
【问题描述】:

我想使用我的自定义模块创建分层分类法。 我使用 taxonomy_get_vocabularies() 获取所有词汇表。

我的问题是当我选择任何词汇或与父母相关的孩子时,应该在另一个选择列表/下拉列表中列出。

我的代码:-

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;
}
?>

谢谢,

【问题讨论】:

    标签: drupal drupal-7 drupal-taxonomy


    【解决方案1】:

    您应该始终在表单函数中创建表单元素,而不是在回调中。
    当回调被触发时,表单函数被再次调用,允许你改变它。回调通常只返回表单元素。
    所以……

    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'];
    }
    

    【讨论】:

      猜你喜欢
      • 2014-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多