Drupal Webform : Add a dynamic select option list
Ever wanted to have a dynamic option list in your Webform select widget ( checkbox, radiobutton, dropdown ) ?
Well, I wanted to load a dynamic list of dates in a Webform and display them as checkboxes. I faced this problem when I was developing a training website where each training class would have different training dates.
So let's start and build our dynamic select option list.
In order to achieve this you need to do the following steps:
1. Add a select widget to your Webform
2. Create a custom Webform module ( webform_custom ) with the following files.
- webform_custom.info – as the name suggest contains the info of the module
name = Webform Custom description = Provides custom functionality for the Webform core = 6.x project = "webform_custom" package = Other
3. Active you custom module from Administer → Site building → Modules
4. The following hook adds another item for “Load a pre-built option list” menu in Webform's select form component. This hook will go into the module file:
function webform_custom_webform_select_options_info()
{
$items = array();
if (function_exists('_webform_get_dates'))
{
$items['training_dates'] = array(
'title' => t('Training class dates'),
'options callback' => '_webform_get_dates',
);
}
return $items;
}
The result of the above function will add the following item in the select form component drop-down:
As you have noted already we still do not have the data yet. No worries!!
The following code fetches the data from the database and returns a list of all dates for a particular node/training class but you can return whatever you want as long as its an array:
function _webform_get_dates()
{
$dates = array();
$node_id = arg(1);
$select = db_query(db_rewrite_sql("
SELECT t.nid, t.field_training_date_value as tdate
FROM {content_type_training_class} c, {node} n
WHERE c.nid='$node_id'
and c.nid=n.nid
ORDER BY field_training_date_value"));
while ($date = db_fetch_object($select)) {
$td = $date->tdate;
$dates[$td] = $td;
}
return $dates;
}
5. Now go back to the Webform select form component and choose the 'Training class dates' option from the “Load a pre-built option list” dropdown and save the field.
You can do a lot of customization with these hooks, which may not be possible to do via Drupal's admin interface. See the full list of Webform hooks here.
That's it folks!!
Filed under: PHP






Which bits of code go in which files?
I think you could make a module to reference views to provide options to webform’s list widgets.
Theres a module similar, but actually it is on alpha version for drupal 6 and it provides a new widget, not pre-built options for list widget.
@Adilson, yes the module is ‘Reference’. I used this module recently in Drupal 7. I created a view and made it a ‘reference’ type. Then added a reference field in my content-type and assigned this View to this field.
great post. thx a lot.
[...] Webform is a great module in Drupal which help collecting user data. Recently i am working on a new website which needs a webform and one of the field is a selection list contains the node titles of a specific content type. I found a blog post about this dynamic select options feature in Drupal 6 by creating a custom module. xebee – Drupal Webform : Add a dynamic select option list [...]