Webform enables developers to create predefined lists that can be used to populate select lists. Instead of applying a copy-and-paste methodology for frequently used lists on a website, we can create an extension module that makes such option lists available to all Webform select components.
Apart from the time saving this represents to administrators creating Webforms, we will only need to make changes in one place to affect all Webform components making use of a custom list.
This is a developer activity and the following task description assumes some knowledge of PHP and Drupal programming.
Let's get the ball rolling by creating a folder to house our extension module. In the sites/all/modules
folder of our website, we create a new subfolder called webform_custom_lists
.
Drupal modules require a minimum of two files in their module folders: an info file that declares the module to the Drupal system and a module file that does the actual work.
webform_custom_lists.info
in our newly created sites/all/modules/webform_custom_lists
folder:name = Webform Custom Lists description = Custom option lists for Webform select components. core = 7.x package = Webform dependencies[] = webform
webform_custom_lists.module
in our sites/all/modules/webform_custom_lists
folder:<?php /** * Implements hook_webform_select_options_info(). * * Define callbacks that can be used as custom select list options. */ function webform_custom_lists_webform_select_options_info() { $items = array(); $items['titles'] = array( 'title' => t('Honorific titles'), 'options callback' => 'webform_options_titles', ); return $items; } /** * Option list containing honorific titles. */ function webform_options_titles() { $titles = array( 'Mr' => 'Mr', 'Mrs' => 'Mrs', 'Ms' => 'Ms', 'Prof' => 'Prof', 'Dr' => 'Dr', 'Hon' => 'Hon', 'Rev' => 'Rev', ); return $titles; }
Load a pre-built option list
drop-down.Our webform_custom_lists.module
file contains two functions. The first function (webform_custom_lists_webform_select_options_info
) implements a hook which lets Webform know that we have added a new list of options by populating an associative array of items. The onscreen title of our list is specified, as well as the name of the function which will return the list of options with their respective keys.
The second function (webform_custom_options_titles
) returns the list of option keys with the corresponding option values. In this case, we have set up a simple associative array with option keys pointing to corresponding values. If necessary we could include any amount of programming required to generate the list, as long as our function returns an associative array as described.
Any additional lists we wish to add will require an entry to the $items
array along with the corresponding options array callback function.
More information on creating custom options lists can be found documented in the webform.api.php
file in our sites/all/modules/webform
folder.
The predefined list of honorific titles we created will be available to all Webforms we build on our site.
A potential drawback of using such pre-built select option lists is that we can lose the functionality of both the Options Element and Select (or other) modules. Since these custom option lists are effectively hardcoded (from Webform's point of view), we cannot rearrange the option sequence, nor make use of the other dynamic functionality exposed by these modules. Note that all the fields in the OPTION SETTINGS fieldset are disabled when we navigate again to the component Edit page.