我需要在Drupal 7中为Webform创建多个预构建选项列表,以对不同的PostgreSQL表进行不同的查询。实际上,我的模块仅适用于一个选项列表,但是我不知道如何创建另一个选项列表。这是我在模块文件中的代码:
function webform_options_webform_select_options_info() {
$items = array();
$items['node_titles'] = array(
'title' => t('Example Option'),
'options callback' => '_get_node_titles',
);
return $items;
}
function _get_node_titles() {
db_set_active('external_db');
$options = db_query('SELECT DISTINCT name, name FROM table ORDER BY name')->fetchAllKeyed();
db_set_active('default');
return $options;
}
已解决
我找到了解决方案,它非常简单,这里是工作代码:
function webform_options_webform_select_options_info() {
$items = array();
$items['node_titles'] = array(
'title' => t('Example Option'),
'options callback' => '_get_node_titles',
);
$items['node_titles_2'] = array(
'title' => t('Example Option 2'),
'options callback' => '_get_node_titles_2',
);
return $items;
}
function _get_node_titles() {
db_set_active('external_db');
$options = db_query('SELECT DISTINCT name, name FROM table ORDER BY name')->fetchAllKeyed();
db_set_active('default');
return $options;
}
function _get_node_titles_2() {
db_set_active('external_db');
$options = db_query('SELECT DISTINCT name2, name2 FROM table2 ORDER BY name2')->fetchAllKeyed();
db_set_active('default');
return $options;
}