Defining custom option lists

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.

Getting ready

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.

How to do it...

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.

  1. First we will save the following code in a file called 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
    
  2. This file introduces our module to the Drupal system.
  3. To have Webform generate the custom list we will save the following code in a file called 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;
    }
    
  4. Now that we have the module code completed, we need to navigate to our site Modules administration page to activate the module and make it useful.
  5. Returning to our title component's Edit page we will find our custom preloaded options list in the Load a pre-built option list drop-down.
  6. Let's select it and click on the Save component button.
How to do it...

How it works...

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.

There's more...

The predefined list of honorific titles we created will be available to all Webforms we build on our site.

Options will not be editable if using pre-built list

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.

Options will not be editable if using pre-built list
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset