我有点困惑。我创建了一个带有一个文本框和一个提交按钮的简单表单。现在我想使用 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;
最佳答案
我在自定义表单中做类似的事情,发现使用 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);
关于drupal - 将选择/选项列表添加到表单中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2429426/