The purpose of this exercise is to demonstrate the basics of creating our very own custom module to change the way Webform does things. Later on, when we get to creating our own custom components and so forth, we will build on knowledge gained in this relatively simple project.
What we'll be doing here is changing the default Results tab view from the Submissions overview to the full submission Table view. As we noticed in the preceding exercises, the Submissions overview is not very helpful when the majority of submissions received are from anonymous users. Our intention is to save our data administrators a click or two by having Webform show the full submission data listing when they click on the Results tab.
While we're busy, we will also be re-arranging the sub-tabs on the result page so that the more frequently used items are towards the right and we'll be changing the Table sub-tab to read Database instead.
Non-coders may happily skip the remainder of this section if they so wish.
Those of us who will be following along here should note that this is not a definitive guide to creating Drupal modules. It is a simplistic and introductory venture into the world of custom coding for Drupal 7 modules using a standard API (Application Programming Interface) hook, hook_menu_alter
. Some knowledge of the PHP programming language syntax is assumed.
All we need to create our custom module is a text editor and some method of copying files to the server hosting our Drupal website.
Drupal modules each reside in their very own folder and the folder is given the same name as the module.
webform_custom
to house our custom module files. webform_custom.info
.name = Webform Custom description = Customize Webform Results page. core = 7.x package = Webform dependencies[] = webform
Downloading the example code
You can download the example code fles for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the fles e-mailed directly to you.
core
stipulates that this module may only be enabled on a Drupal 7 website. webform_custom.module
.<?php // Make changes to the Results menu sub-tabs. function webform_custom_menu_alter(&$items) { // Set Results Table sub-tab as new default sub-tab. $items['node/%webform_menu/webform-results']['page callback'] = 'webform_results_table'; $items['node/%webform_menu/webform-results']['page arguments'] = array(1, '50'), $items['node/%webform_menu/webform-results/table']['type'] = MENU_DEFAULT_LOCAL_TASK; $items['node/%webform_menu/webform-results/submissions']['type'] = MENU_LOCAL_TASK; // Rename Results Table sub-tab to Database. $items['node/%webform_menu/webform-results/table']['title'] = 'Database'; // Change the order of the sub-tabs. $items['node/%webform_menu/webform-results/table']['weight'] = 4; $items['node/%webform_menu/webform-results/download']['weight'] = 5; $items['node/%webform_menu/webform-results/analysis']['weight'] = 6; $items['node/%webform_menu/webform-results/submissions']['weight'] = 7; }
$items
. All we're doing here is changing a few of the menu items that apply to the Webform Results page.A standard within Drupal is that items are sequenced according to their weight—a lower weight floats towards the top and a heavier weight floats towards the bottom. In the last four lines, then, we are changing the display sequence of the sub-tabs on the Results page.
/sites/all/modules
.The Drupal system scans through the /sites/all/modules
folders looking for the .info
files with corresponding .module
files. These are then listed on the Modules page. Thanks to the pluggable modular design, Drupal allows us to make changes to various behavioral and display aspects via the hook system.
We have used hook_menu_alter
to effect a change in the way that the Webform Results page behaves.
Where the Results page menu used to look like similar to the following screenshot:
It now looks similar to the following screenshot:
The concepts we have covered in this simple example will be reinforced when we encounter the Webform API, which exposes a set of hooks all of its own that we may use to manipulate Webform behaviors.