7

私は少し混乱しています。1 つのテキスト ボックスと送信ボタンを備えた単純なフォームを作成しました。次に、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;
4

7 に答える 7

12

私はカスタム フォームで同様のことを行っていますが、関数の引数として語彙コードを使用して、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);
于 2010-03-21T15:22:59.667 に答える
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'),
);
于 2012-04-17T12:35:38.927 に答える
1

迅速なお返事をありがとう!こんな感じに仕上がったと思います。

$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;
        }
    } 
于 2010-03-12T02:00:05.643 に答える
1

これが drupal の方法です - _taxonomy_term_select()

于 2011-04-06T19:35:24.790 に答える
1

関数を使用できると思います: taxonomy_form

ここに文書があります: taxonomy_form

于 2012-03-07T14:06:53.290 に答える