drupal - 将选择/选项列表添加到表单

har*_*y_T 7 forms drupal taxonomy

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

 $vocabularies = taxonomy_get_vocabularies('my_type'); 
Run Code Online (Sandbox Code Playgroud)

我的问题是如何将词汇表列入"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;
Run Code Online (Sandbox Code Playgroud)

Nic*_*lai 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);
Run Code Online (Sandbox Code Playgroud)