2010-03-11 81 views
6

我有點困惑。我用一個文本框和一個提交按鈕創建了一個簡單的表單。現在我想使用taxonomy_get_vocabularies()函數添加分類術語的選擇/選項下拉框。drupal - 將選擇/選項列表添加到表格

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

回答

11

我做一個自定義窗體類似的東西,並發現它更容易使用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); 
0

研究如何做,在分類模塊的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) { 
    ... 
0

感謝您的及時回覆!我想我是這樣做的。

$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

您應該使用意見回覆的帖子,不進行其他職位你自己。 – jergason 2010-03-12 21:30:09

+0

對不起,我認爲我的評論對於「評論」格式有點冗長。順便說一句,如果任何人有更好的解決方案,請讓我們知道。另外 taxonomy_get_vocabularies()的一個例子會很有用。 – 2010-03-13 19:44:13

1

這是Drupal的方式 - _taxonomy_term_select()

+2

不適用於drupal 7 – FLY 2012-04-17 09:32:02

1

我想你可以使用的功能:taxonomy_form

在這裏,你有doumentation:taxonomy_form

+0

<= drupal 6 only – DrCord 2015-07-27 18:37:35

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

然後你就可以在你的$形式呼籲您#options此功能:

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

這裏是如何做到這一點在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; 
相關問題