Counting Webform submissions with views

One of the most widely installed and utilized contributed modules in the Drupalverse (that is, the Drupal universe) is a module called Views. The Views module is also, in essence, a query builder, but it operates in a far larger context than Webform Report. Webform Report is a query builder within the context of a particular Webform's submissions. Views is a query builder that can work within or above the context of almost anything on our website.

The most common use of Views is to dig into various areas of our Drupal database, pull out the required content data, manipulate and filter the data, and finally present it in some way on our site, for example in the form of a listing on a page or a block. Apart from an impressive set of functionality via the user interface, Views also offers a powerful API for developer use.

Due to the technicalities around the way Webform stores submitted data (as discussed in Chapter 3, Working with Submissions), Webform submitted data has historically not been an accessible area for the Views module. To the relief of many fans of Views and Webform, that has recently begun to change. At the time of writing, the integration dealing with submitted Webform data is not fully matured for general production use (although there are several sites employing it), so we will look at an example of what is available in the current Webform release.

We are going to create a view that will list the names of all published Webforms on our site, along with the number of submissions that have been made to each Webform. This is useful when we wish to have an overview of our currently active Webforms.

Getting ready

For its complex under-the-hood functionality, the Views module is dependent on the presence of the Chaos tool suite (ctools), so we will need to first install and enable that.

Let's navigate to the Chaos tool suite (ctools) homepage at http://drupal.org/project/ctools and download the latest 7.x version. The compressed folder needs to be extracted in sites/all/modules so that we have sites/all/modules/ctools.

Now we navigate to our site's Modules page to enable Chaos Tools by checking the box next to its name and clicking on Save configuration.

Having resolved the dependency issue, we can navigate to the Views module homepage at http://drupal.org/project/views and download the latest 7.x version. The compressed folder needs to be extracted in sites/all/modules so that we have sites/all/modules/views. Now we navigate to our site's Modules page to enable Views by checking the box next to its name and clicking on Save configuration.

To complete the installation we also need to enable the Views UI (that is, Views User Interface) module on the Modules page (there is no separate download required for Views UI, it is a submodule of Views and is therefore already downloaded) and click on Save configuration.

How to do it...

  1. Navigate to Structure in the menu toolbar and click on Views.
  2. Click on Add new view.
  3. Enter Submission counter for View name.
  4. Select Webform submissions for Show.
  5. Select Table in Display format.
  6. Check the box at Use a pager.
    How to do it...
  7. Click on the Continue & edit button.
    How to do it...
  8. In the left-most column of settings we click on the Add button next to the FIELDS heading.
  9. In the overlay pop-up, scroll down about a third of the way and check the box at Content: Title and click on the Apply (all displays) button.
    How to do it...
  10. Click on the Apply (all displays) button in the next overlay pop-up.
  11. Click on Add next to the FILTER CRITERIA heading below the FIELDS section.
    How to do it...
  12. In the overlay pop-up check the box at Content: Published and click on Apply (all displays).
  13. In the following overlay pop-up we select Yes for Published and click on Apply (all displays).
    How to do it...
  14. Towards the right of the screen we click on Advanced.
    How to do it...
  15. In the list of Advanced settings now displayed, we click on No at Use aggregation.
    How to do it...
  16. Check the box at Aggregate and click on Apply (all displays).
  17. Returning to the left-most column of settings, we click upon Aggregation settings at Webform submissions: Sid (Sid) in the FIELDS section.
    How to do it...
  18. Let's select the value Count DISTINCT in the Aggregation type selection list and click on Apply.
    How to do it...
  19. Under the heading FORMAT, let's click on Settings (next to Format: Table).
  20. Check the boxes under SORTABLE for both the Sid and Title fields and click on the Apply (all displays) button.
    How to do it...
  21. Scrolling to the top of the page, we click on the Save button to store the view we have defined.
    How to do it...
  22. To see our view in action, we click on the view page button to the right of the screen.
    How to do it...

The view that we have set up provides us with a table containing all active Webforms on our site. Along with the names of the forms, we also see the number of submissions that have been made to each form. The active form names are rendered as links that will take us to the respective Webforms when clicked.

The listing is sortable by clicking the column headings. When we have more than ten active forms on our site, the view will also contain a pager (that is, links for next and previous pages) to assist with navigating through the list.

How it works...

In a nutshell, we have defined a database query through the Views user interface. When the view was created we stipulated that the data driving the view would come from the Webform submissions table in the Drupal database. We requested that the view return values of the content title and the number of unique (distinct) submissions against each title, but only for Webforms currently in a published state.

There's more...

We have barely scratched the surface of what can be achieved with Views. A full and detailed coverage of the module and its various configurable options would fill enough books to cause even the sturdiest bookshelf to break into a sweat. However, we have covered the basics of how this query builder works and had a small taste of how displays may be customized.

In the near future we shall be able to use the Views module to construct customized submitted data reports similar to what is achievable with Webform Report, so that the value of Views is not limited to only non-Webform content on our websites.

Obtaining help

The developers of Views have gone above and beyond the call of duty to thoroughly document the module. We may install the Advanced help module (http://drupal.org/project/advanced_help) to access the documentation from the menu toolbar of our sites for easy reference while we experiment with the Views options. Of course, we may also refer to the drupal.org website for additional documentation and the IRC channels for assistance.

..................Content has been hidden....................

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