Drupal 7 Webform Cookbook

5 (1 reviews total)
By Vernon Denny
    Advance your knowledge in tech with a Packt subscription

  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Setting up Webform

About this book

The Drupal Content Management System puts everyone in charge of their own internet destiny. Webform takes this power a step further by ensuring that you are never more than just a few mouse-clicks away from fully functional information-gathering forms. From simple contact forms to advanced web applications, Webform provides a solid tool set for both the novice and the expert.

Drupal 7 Webform Cookbook gives you everything you need to achieve in minutes what previously cost hours in development and testing. Practical demonstrations of every facet give both the technical and the not-so-technical users an in-depth understanding of how things work.

Install. Use. Customize. Extend. A mammoth learning curve devoured in small bytes: all digestible, with no choking.

From designing coherent forms, to sending pretty HTML emails and managing data, explore the rich scope of possibilities that Webform enables. Also, easily extend and tune Webform with custom components and functionality.

With Drupal 7 Webform Cookbook you are placed firmly in the driving seat.

Publication date:
May 2012
Publisher
Packt
Pages
274
ISBN
9781849516488

 

Chapter 1. Setting up Webform

In this chapter, we will cover the following:

  • Downloading and installing Webform

  • Configuring Webform for our website

  • Setting user roles and permissions

Introduction

In this chapter, we are going to extend our Drupal installation by adding the powerful Webform module. Drupal is a very capable Content Management System (CMS) as it stands. However, it cannot inherently do everything you may want it to do. A rather clever solution to this problem is the Drupal module application programmer interface (API), enabling developers to create chunks of functionality as Drupal plugins that website administrators can use to customize their sites.

The Webform module facilitates two distinct, yet related, sets of functionality. First, it is a great tool for creating forms designed to capture required data from users, such as a customized Contact Us form for example. Secondly, Webform has a backend interface for website administrators to work with the submitted data. We can see then that Webform is designed for scenarios where many people (for example, users, subscribers, and so on) are submitting data handled by a small group of administrative users.

Submitted Webform data is not considered to be content. This is diametrically opposed to modules such as the Content Construction Kit (CCK) or the Fields modules, where the purpose is to help us build pages that accept data to be used as website content, that is, many people submit data for viewing by many people.

Apart from supporting all the standard HTML form input elements, such as textfields, radio buttons, and selection lists, Webform also offers some custom components, such as survey grids and date or time fields. All supported components include data validation options and various display options. For example, Webform has an e-mail address component which will automatically validate that submitted e-mail addresses are correctly formed, that is, they look like real e-mail addresses. We may specify mandatory fields (that is, the form user may not leave them blank), or even that only unique values may be submitted (that is, ensure that no other submission carries the same value in a specific field).

Additional to the form itself, Webform also enables us to set up a variety of automatic response e-mails to submissions. Whenever a submission is made on the form, we may configure Webform to send an e-mail to the submitting user confirming receipt of their submission, as well as sending a notification e-mail to an administrator to advise them that a submission has occurred on the form.

Administrators have access to view and edit submitted data, and resend e-mails, as well as the ability to download submitted data into spreadsheets. Webform even includes some basic statistical analysis of submitted data.

True to the spirit of Drupal, Webform is itself extendible via its own API. Several contributed modules exist that enable us to make our specific Webform installation bigger and better. When we cannot find a module that offers the functionality we seek, we may extend Webform by developing our own custom modules.

We will better understand the benefits that Webform offers us by getting stuck in and utilizing it. Before that can happen, we need to install Webform and get it properly set up to work on our website. After Webform has been properly installed, we'll take a look at some global configuration options before getting started with our first form.

 

Introduction


In this chapter, we are going to extend our Drupal installation by adding the powerful Webform module. Drupal is a very capable Content Management System (CMS) as it stands. However, it cannot inherently do everything you may want it to do. A rather clever solution to this problem is the Drupal module application programmer interface (API), enabling developers to create chunks of functionality as Drupal plugins that website administrators can use to customize their sites.

The Webform module facilitates two distinct, yet related, sets of functionality. First, it is a great tool for creating forms designed to capture required data from users, such as a customized Contact Us form for example. Secondly, Webform has a backend interface for website administrators to work with the submitted data. We can see then that Webform is designed for scenarios where many people (for example, users, subscribers, and so on) are submitting data handled by a small group of administrative users.

Submitted Webform data is not considered to be content. This is diametrically opposed to modules such as the Content Construction Kit (CCK) or the Fields modules, where the purpose is to help us build pages that accept data to be used as website content, that is, many people submit data for viewing by many people.

Apart from supporting all the standard HTML form input elements, such as textfields, radio buttons, and selection lists, Webform also offers some custom components, such as survey grids and date or time fields. All supported components include data validation options and various display options. For example, Webform has an e-mail address component which will automatically validate that submitted e-mail addresses are correctly formed, that is, they look like real e-mail addresses. We may specify mandatory fields (that is, the form user may not leave them blank), or even that only unique values may be submitted (that is, ensure that no other submission carries the same value in a specific field).

Additional to the form itself, Webform also enables us to set up a variety of automatic response e-mails to submissions. Whenever a submission is made on the form, we may configure Webform to send an e-mail to the submitting user confirming receipt of their submission, as well as sending a notification e-mail to an administrator to advise them that a submission has occurred on the form.

Administrators have access to view and edit submitted data, and resend e-mails, as well as the ability to download submitted data into spreadsheets. Webform even includes some basic statistical analysis of submitted data.

True to the spirit of Drupal, Webform is itself extendible via its own API. Several contributed modules exist that enable us to make our specific Webform installation bigger and better. When we cannot find a module that offers the functionality we seek, we may extend Webform by developing our own custom modules.

We will better understand the benefits that Webform offers us by getting stuck in and utilizing it. Before that can happen, we need to install Webform and get it properly set up to work on our website. After Webform has been properly installed, we'll take a look at some global configuration options before getting started with our first form.

 

Downloading and installing Webform


We are now going to install and enable the Webform module in our Drupal site.

Getting ready

To experience the richness of Webform an existing Drupal installation is required. This could be a local installation (that is, on your own PC or laptop) or a remote installation on a web server. Visit the web page http://drupal.org/start to obtain the latest version of Drupal. A detailed installation guide for Drupal can be found at the http://drupal.org/documentation/install web address (see, in particular, the section on Local Server Setup if you plan to test Drupal and Webform on your local computer).

This book is written for Drupal 7 and that would be the recommended version for you to install. The Webform module itself is maintained for both Drupal 6 and Drupal 7 in terms of functionality, but you will find that the location of some tabs, links, and buttons vary between the two versions. Screenshot images in the pages and chapters that follow will reflect the Drupal 7 interfaces only.

How to do it...

  1. 1. Let's download the latest Webform version from the projects home page at http://drupal.org/project/webform. Typical to most project home pages, the Webform page starts off with a brief overview of what kind of solutions the module presents and a screenshot of a sample Webform. You're welcome to read through all the information presented, perhaps even follow one or two of the links, but the bit we're interested in right now is the Downloads heading towards the bottom of the page.

  2. 2. Click on the .zip (Microsoft Windows, Linux, Mac OS) or .tar.gz (Linux, Mac OS) next to the version with 7 as its first digit (we're working on Drupal version 7).

  3. 3. Save the compressed file to the /sites/all/modules directory in our Drupal installation.

  4. 4. Once it has downloaded, we extract the compressed contents using whatever tool is available for our system (normally found by right-clicking on the compressed folder).

  5. 5. A Webform folder will now be created within the /sites/all/modules folder of our Drupal installation.

  6. 6. The curious may now enter the Webform directory, read the README.txt file and perhaps browse around the Webform system files and folders. We must be careful to not inadvertently change the contents of any of the files though!

  7. 7. Enabling plugin modules is an administrative action so we are going to need to log in to our site as administrator (the user we created when we installed Drupal) to continue the installation. Let's fire up our favorite web browser, navigate to our home page, and log in.

  8. 8. In the administrator menu bar, we need to click on Modules:

  9. 9. Scroll down to the very bottom of the Modules page until we get to Webform.

  10. 10. Click on the checkbox next to the Module name to select it:

  11. 11. Finally, click on the Save configuration button.

The page can take several seconds to re-load, but do not panic! The Webform module is busy creating database tables and identifying itself to the Drupal core system.

How it works...

An aspect of Drupal that makes it such a powerful tool for constructing websites is the fact that it is extensible. The underlying core system has been put together in a way that permits developers to add plugin functionality without needing to change the core Drupal program files. It really is just a case of plug it in and switch it on!

 

Configuring Webform for our website


Now that we have Webform installed and enabled, let's take a look at some of the global configuration settings that will apply as defaults on every piece of Webform content that you create on this site.

Webform provides a highly flexible and configurable content type. Certain options have been identified as being generally constant for all Webforms on a particular site and these have been isolated on the Webform settings page.

How to do it...

The global Configuration settings for Webform can be reached either by clicking the Configure link next to Webform on the Modules page, or by clicking on Webform settings under Content Authoring on the Configuration page (click Configuration in your administrative menu bar).

How it works...

The options and settings we select here will apply as default values on every new Webform piece of content we create. Many of these may be overridden in the configuration settings of specific forms if required.

There's more...

Understanding the configuration options will give us a good overview of Webform in broad strokes. As we progress through the book with the demonstration Webform we will discover the finer nuances in greater detail. We will not be changing any of these default settings at this point, but we will briefly discuss what they mean in order to get an idea of the scope of Webform. As what follows is intended as an introductory overview, the next few paragraphs will be easier to follow while referencing the Webform settings page.

Webform-enabled content types

To promote flexibility, the components of Webform may be associated with various types of content. A basic Drupal installation comes with two basic content types, Basic Page and Article. With this setting we can extend the existing content type by making Webform available within it. This permits us even to create a custom Drupal content type while leveraging the power of Webform.

Webform components

The purpose of Webform is to facilitate the creation of information-gathering web pages and to provide meaningful back-end support for administrators who need to work with that data. Over the years, Webform has developed a set of components which blend with ease of use for the end user, as well as provides both convenience and control for website administrators.

The components are so named because they are, in reality, more than just data fields. A component encapsulates the visual aspects of the field as it will be shown on the form and other output media, default validations to apply to the specific field type, specialized handling and formatting of submitted data for inclusion in spreadsheets for download, and so on.

Each component type represents a different kind of data. The validation of a date, for example, is naturally quite different from rules that would apply to an e-mail address. While many characteristics and settings are common to all components (such as field label for example), each component has their own specific settings and requirements for further handling.

Aside from unique validation requirements, some components also have specialized methods of rendering (that is, how they will look, or be drawn) on a web page. The date and time components, for example, are automatically rendered as appropriate drop-down lists, whereas the file component comes with a browse button.

Note

Any components that are unchecked on this page will not be available to you when you start building forms.

Default e-mail values

Each Webform can be configured to send one (or more) e-mails when a submission is received. It is customary to send a confirmation of a submission to the end user via e-mail, but you can also have Webform additionally send e-mails to one or more administrators. Specifying default values for the automatically generated e-mails here will save you the labor of typing these out on each and every Webform you create on the site:

Advanced options

There are forms you want to be filled out by any visitor to the page and other forms you would prefer to have only filled out by users registered on your site. Some forms may be filled out multiple times by the same person, others may only be filled out once. Webform provides some methods of handling access to forms and controlling the number of submissions from a particular source:

A frequent complaint on the Webform issue queue (http://drupal.org/project/issues/webform) is that "Webform is not sending out e-mails!" Quite often, the challenge lies with the operating system of the hosting server. Some Microsoft-based (IIS) servers cannot handle long-format e-mail address headers. If your site is hosted on such a server and you experience difficulties with e-mail, you will need to come to this page and try specifying a different e-mail address format. See http://phptutorial.info/?mail under the Notes heading for a description of the problem.

Part of the awesome functionality that Webform makes available is the facility to download all (or just some of) the data submitted on a form in pseudo-spreadsheet format, such as CSV (Comma Separated Values) or tab separated values files. You may set defaults here that will save yourself and other administrators the bother of specifying them on every occasion that data needs to be downloaded.

 

Setting user roles and permissions


Not only does Webform enable us to control who may submit data, it also provides a range of permissions regarding who may create or edit Webforms, who may access submitted data, and even who may change submitted data.

Getting ready

In the website administrator menu bar, click on People and then on the PERMISSIONS tab at the top-right of the screen, shown as follows:

Permissions are all about "who may do this action" or "who may access this content". It goes around what various users are permitted to do or not permitted to do.

Carefully read the paragraph at the top of the page regarding permissions, giving special attention to the closing sentence. Take this information to heart and let it guide your decision-making when assigning roles and responsibilities:

How to do it...

Scrolling down to the bottom of the page we find a heading for Webform with several permissions listed.

By default, no authenticated (that is, logged in) user may create or change Webforms, nor access submitted data, not even their own.

Reading the text under the various permission labels, we notice that anonymous users are completely banned from most activities.

For the purposes of later experimentation, let's permit authenticated users to access and alter their own submissions after the fact by granting them authority to Access own Webform submissions and Edit own webform submissions by checking the corresponding boxes in the Authenticated User column and then clicking on the Save Permissions button, as shown in the following screenshot:

Scrolling halfway back up the page so we are able to view the list of permissions under the Node heading, we find several more Webform permissions. The permissions listed here are focused on Webform as a Drupal content type, in contrast to the permissions we looked at previously which concerned themselves more with Webform data.

Looking at the options here, we can readily see that it is possible to create a user role that can create and delete its own Webforms, but cannot edit or delete Webforms created by a different role. With careful planning we can create very hierarchical structures in terms of user roles and their corresponding permissions.

How it works...

Programmed into Webform (and all other facets of Drupal) are several checks to determine whether a user is authorized to carry out a requested action or view requested content. If the user or role permissions are not set, the requested action is ignored and an error message output is displayed on the screen.

By checking the boxes for authenticated users as we did previously, we are allowing authenticated users to return to forms that they previously submitted and make alterations to the submitted data. They will not, however, be able to delete their submission because the Delete own webform submissions permission is not checked.

We must be very careful when assigning permissions. If we were to accidentally check the box Access all webform results for authenticated users, then every registered user of our site would be entitled to download the data entered by every person who has submitted information on any and every Webform across our site. That is probably not a very good idea!

There's more...

Drupal is, in general, a security conscious environment. Users of the site are, on first principle, denied authority on most activities and resources until fully authorized to them by the website administrator. The philosophy of 'Safety First' applies also to contributed modules such as Webform.

Why is this important? Well, just as we expect other websites to safeguard our personal information, so will our end users expect us to treat their submitted information with utmost circumspection. There is a large measure of implied trust on the part of anyone who submits information to a website. That trust may not be violated if we expect to retain our integrity and reputation.

Management of user roles and permissions can become quite a complicated task, depending on the size and scope of your web application. For more information on creating and managing user accounts and user roles, visit the following documentation web pages of drupal.org:

Roles: http://drupal.org/getting-started/6/admin/user/roles

Accounts and roles: http://drupal.org/node/22284

About the Author

  • Vernon Denny

    Born and bred in a small city called Klerksdorp in the north-west of South Africa, Vernon now lives in Johannesburg with his wife, daughter and supercilious cat. Despite standing two meters tall in his thinnest socks he has an apprehension of heights. After explaining the vagaries of the insurance industry to an IBM iSeries computer in RPG and PHP during the typical working day, he loves to help out on the Webform issue queue on the Drupal website. In between all of this he also keeps himself busy learning about the GNU/Linux operating system and the freedoms arising from the Free and Open Source Software movement. When a computer keyboard is out of reach, Vernon is either playing games with his daughter, hiking with the family, or irritating them with his attempts to learn guitar.

    Browse publications by this author

Latest Reviews

(1 reviews total)
Excellent
Book Title
Unlock this book and the full library for FREE
Start free trial