I'm trying in Webform (version 4.9) to create a pre-build option list with data from an external database. A pre-build option list is an available list you get when creating a "select options" field in Webform (like list of countries, state, ...).
I didn't find an existing module that allows me to add such pre-build list to Webform.
So, first I added my external database information to the setting.php
file.
To test it, I build a simple php page containing this:
db_set_active('external_db');
$query = db_query("SELECT id, name FROM TABLE")->fetchAllKeyed();
db_set_active();
Print_r($query);
And, it's working (= I got a correct result of my request).
So then I added this to my .module
file:
function webform_pre_build_list_db_webform_select_options_info() {
$items = array();
$items['modeles_db'] = array(
'title' => t('Modeles_db'),
'options callback' => 'webform_pre_build_list_db'
);
return $items;
}
function webform_pre_build_list_db() {
db_set_active('external_db');
return db_query('SELECT id, name FROM table')->fetchAllKeyed();
db_set_active('default');
}
And, it's not working (= my list is empty).
If I try with a request to the Drupal DB, then, it's working. For example:
function webform_pre_build_list_db() {
return db_query('SELECT cid, recipients FROM contact')->fetchAllKeyed();
}
Is working well, I got recipients form drupal table "contact" in my list...
What am I missing?
Any help will be appreciated!
db_set_active('default');
afterreturn
statement, next - read about db_set_active probably you need to clear cache