【问题标题】:drupal - add select/option list to a formdrupal - 将选择/选项列表添加到表单
【发布时间】:2010-03-11 23:29:34
【问题描述】:

我有点困惑。我创建了一个带有一个文本框和一个提交按钮的简单表单。现在我想使用 taxonomy_get_vocabularies() 函数添加分类术语的选择/选项下拉框。

 $vocabularies = taxonomy_get_vocabularies('my_type'); 

我的问题是如何将词汇表添加到“Drupal 方式”的表格中。 Drupal 定义形式的方式似乎相当僵化。另外,如果存在相关的分类术语,我怎么能做出这个条件。

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;

【问题讨论】:

    标签: drupal forms taxonomy


    【解决方案1】:

    我在自定义表单中做类似的事情,发现使用 taxonomy_get_tree 更容易,将词汇代码作为函数的参数。见下文:

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

    【讨论】:

      【解决方案2】:

      我已经为我的模块(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;
      }
      

      然后您可以在 $form 中的 #options 上调用此函数:

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

      【讨论】:

        【解决方案3】:

        感谢您的及时回复!我想我是这样解决的。

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

        【讨论】:

        • 你应该使用 cmets 来回复帖子,而不是自己发额外的帖子。
        • 对不起,我认为我的评论对于“评论”格式有点冗长。顺便说一句,如果有人有更好的解决方案,请告诉我们。 taxonomy_get_vocabularies() 的示例也很有用。
        【解决方案4】:

        这是 drupal 方式 - _taxonomy_term_select()

        【讨论】:

          【解决方案5】:

          我认为你可以使用函数:taxonomy_form

          这里有记录:taxonomy_form

          【讨论】:

            【解决方案6】:

            这是在 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;
            

            【讨论】:

              【解决方案7】:

              在分类模块的 taxonomy.admin.inc 文件中研究如何做到这一点

              /**
               * 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) {
                ...
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2019-04-19
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多