Drupal 7: Aggregate Totals for Webform Grid Component

David G - DrupalI have a project I have mentioned here frequently in the past. The application accepts student referrals from off-campus community members. In the webform to be completed by the public the client includes a Grid Component. The client wants to count the number of total values selected per column and append it to grid output. I set out to complete this task and ultimately, like the solution I chose.

The existing webform component appears as such:

Example webform which contains a Grip component to allow people to rate a student.

Example webform which contains a Grip component to allow people to rate a student.

It shows submissions typically as any drupal Webform install would:

HTML output of webform submission.

HTML output of webform submission.


The client came in and showed me a printout of the submission PDF, which was created using the great webform2pdf module, and penned in the example output they wanted to show the totals or tally per column of the Grid component. This is because  they are currently accepting ~700 applications per quarter and tabulating this data by hand! If Drupal can do it for them, this will save them alot of time.

Researching Solutions:

I looked at various possible solutions before commencing work for about 1 hour:

In my opinion all these solutions all failed to be:

  1. Simple to maintain.
    What happens using these modules if a new question or column is added? Would logic using these automatically support webform changes?
  2. Simple to implement and test.
    As much as possible I want to test this functionality before it’s live. For the webform, if the webform changes, If the webform has not been submitted, etc.

Final Solution:

The final product for the webform now looks like this for an HTML page or a rendered PDF.

Final example PDF output, note the Totals: line.

Final example PDF output, note the Totals: line.

So how did I create this output. I simply alter the rendering of the webform submission. To me this is the simplest approach to this problem:

 * Implements hook_webform_submission_render_alter().
 * This adds a aggregate counts value of the number of responses within the
 * grid as a final row of the HTML table. In reality, I couldn't alter the webform
 * grid theme output function to accomplish this and I did not want to override
 * the theme registry to accomplish this task. So I  insert a new table below
 * the existing table.
 * Notes:
 * This output is purely visible, no data is saved in the database.
 * This output is for submissions only and not the Webform the visitors see.
 * This output is carefully constructed to not break the page if the webform
 * component key changes, or if the number of items in the grid changes at
 * a later date.
function summer_session_webform_webform_submission_render_alter(&$renderable) {
  if (array_key_exists('quality_ratings', $renderable)) {
    $column_totals = array_keys($renderable['quality_ratings']['#grid_options']);
    foreach ($column_totals as $column_id => &$n) {
      // this adds a css class to the td.
      $n = array('data' => 0, 'class' => 'checkbox');
    foreach ($renderable['quality_ratings'] as $key => $grid_children) {
      if (is_int($key)) {
        $rating_value = $grid_children['#value'];
    $table_header = array('Totals:') + array_fill(0, count($column_totals)+1, '');
    $grid_summary_table = theme('table', array(
      'header' => $table_header,
      // the 'a' key below forces the nbsp to the front of the array, without
      // overriding the 0th key.
      'rows'   => array(array('a' => ' ') + $column_totals),
      'id'     => 'rmp_grid_totals',
      'class'  => 'mytable',
    $renderable['quality_ratings']['#suffix'] = $grid_summary_table;

Please note that my solution does not store any information in the database.

But I made every effort to:

  • Not compute anything if the Grid is empty.
  • By using hook hook_webform_submission_render_alter() I assure this custom output does not try to display when a visitor is simply completing the form. This output should be shown only after-the-fact to university staff.
  • Support changes to the Grid component in either the number of Rows or Columns.
  • Easily tested this by hitting the live webform, the HTML submission page and the PDF download link page!

I think this is a nice example of using the Drupal and contributed module API to complete a task. And, also to research existing modules before you go down the path of creating custom code for your project (which is sometimes necessary). Happy Coding!

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , | Posted under Drupal | RSS 2.0

Author Spotlight

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

Your email address will not be published. Required fields are marked *