Webform

by Gisle Hannemyr

Webform is a module for making forms and surveys.

Table of contents

Drupal project discussed in this chapter: Webform, Webform MySQL Views.

Introduction

Webform is a module for using the GUI to make web forms (including wizards and surveys).

It is not meant to be a replacement for the core fields feature – where the administrator creates a content type form with identical fields that is populated by the user and where each filled in form becomes a full node.

Webform is designed to be used where each new webform node gets its own set of custom fields. The use case for the module is when you want your users (often anonymous) to be able to submit data that you want emailed to a few email addresses or saved to a CSV file.

You can even use it with fields to add additional fields to webform nodes!

Webform typically is used when performing data collection that is a one-way communication, that is, many users submitting values to a very small set of administrators.

A webform allow conditionals to appear in a form. Conditionals may be used to hide or show certain components (or entire pages) based on the value of other components.

After a submission customizable emails can be sent to administrators and/or submitters. Results can be exported into Excel or other spreadsheet applications. Webform also provides some basic statistical review and has an extensive API for expanding its features.

There exists additional modules to make use of the data generated by webform submissions. In Drupal 7, for example, you may use the modules Data and Webform MySQL Views to use Views to display and extract submissions (see Drupal.org. post Display Webform submission data in Views and a Vimeo screen­cast: Display Webform submission data in Views).

Installing and enabling the module

noteBy default the Webform content type do not have a body field. To have a body field, set the the configura­tion variable “webform_install_add_body_field” to TRUE before installing the module. You may do this using drush, by using the $conf[] array in settings.php or by making a helper project.

Install Webform as you would normally install a contributed drupal module.

The simplest way to do this is to use drush to both install and ebable:

$ sudo drush en webform -y

Now, the Webform module is set up and ready to use.

Creating a web form

To review the fields that the administrator may use to create a webform, navigate to Configuration » Content authoring » Webform settings. Note that these are similar to, but not the same as those used to put fields into nodes. For instance, the webform “Select options” field allows direct creation of checkboxes, radio buttons, and select menus, while the fields in core feature relies on taxonomy entities that is external to the field.

A webform is a Drupal content type. To manage its settings, navigate to Structure » Content types » Webform » edit. The defaults for “Publishing options” and “Display settings” usually need to be changed.

To create a webform node, navigate to Content » Add content » Webform.

Using webforms

The results of submissions to a particular webform can be viewed under the tab “Results”. To delete all results, press the button “Clear”. This operation cannot be undone.

Advanced form processing

The the “Form settings” tab provide limited options for setting the redirect location for the confirmation page. To redirect the confirmation based on the value entered in a Webform component, use hook_form_alter. The following implementation is used to redirect to a different ePay form depending on the type of registration selected in the webform.

/**
 * Implements hook_form_alter.
 */
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ('webform_client_form_XX' == $form_id) {
    $form['#submit'][] = '_epay_redirect';
   }
}

function _epay_redirect(&$form, &$form_state) {
  $regtype = $form_state['values']['submitted'][1];
  switch ($regtype) {
    case 'fullconference':
      $form_state['redirect'] = 'https://epay.example.net/pay.html?regtype=…1';
      break;
    case 'daypass':
      $form_state['redirect'] = 'https://epay.example.net/pay.html?regtype=…2';
      break;
    default:
      drupal_set_message('Internal error, please contact webmasters.');
  }
}

Final word

[TBA]


Last update: 2018-07-06 [gh].