1

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!

7
  • first of all you are using db_set_active('default'); after return statement, next - read about db_set_active probably you need to clear cache Commented Aug 4, 2015 at 11:16
  • What's a "Webform prebuild list"? Also, would it be an option to use an (existing) contributed module instead of writing your own module? If you want to answer these questions, then please do so via an EDIT of your question. Commented Aug 4, 2015 at 11:22
  • @Pierre.Vriens, I have edited my post.
    – Fumble
    Commented Aug 4, 2015 at 11:41
  • @Neok, Yes to go back to default Drupal DataBase, but I change to "external_db" before return. I already tried clearing cache. Thanks!
    – Fumble
    Commented Aug 4, 2015 at 11:47
  • OK, now the question is way more clear. I hope my extra edits are OK (also). If not please rework as you like ... Commented Aug 4, 2015 at 11:55

1 Answer 1

1

I changed this:

function webform_pre_build_list_db() {
db_set_active('external_db');
return db_query('SELECT id, name FROM table')->fetchAllKeyed();
db_set_active('default');
}

to this

function webform_pre_build_list_db() {
  db_set_active('external_db');
  $options = db_query('SELECT id, name FROM table')->fetchAllKeyed();
  db_set_active('default');
  return $options;
}

And it's working!

(Explanation: return immediately ends the execution of a function, in this case before db_set_active() could be called.)

Thanks to Nevets

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.