Skip to main content
Display form data

Displaying Webform submission data in Drupal Views

Although Webform module comes with limited integration to expose the submitted data in Views, it lacks the fine control to make View by Webform field as rows and columns. There is a workaround to achieve this though which I would like to briefly run through in this blog.

Webform MySQL Views together with Data and Schema modules with a patch to Webform MySQL Views from issue #889306: Allow the designation of a primary key for MySQL views makes this feasible.

Webform MySQL Views, as the name implies, allow us to create MySQL view from Drupal, leveraging the Data module which counts on Schema module.

Data module wraps a bundle of sub-modules, among them Data Search provides Views Integration and Data Admin UI for accessing its administrative pages.

Once the mentioned modules are enabled. You can see a sub-menu "MySQL Views" under Administration » Content » Webforms. Tick the Webform node whose fields are needed in Views. This form is only meant to create MySQL view.

Now the next step is to make the Data module adopt this view so it would be recognized by Views module. For the same open the admin page at Administration » Structure » Data tables (or /admin/structure/data/adopt). Select the view from "Orphaned Tables" list and hit "Adopt" button.

 


 

Now apply the patch from #889306: Allow the designation of a primary key for MySQL views. This can be added to any of your custom modules. You will have to change the {yourcustommodule}_views_data_alter(&$views) hook implementation to match your module's code. 

<?php
function {yourcustommodule}_views_data_alter(&$views) {
  // name of the database view
  $dbview_name = 'voting_results';
  $pseudo_pk = 'item_id';
  _mysql_views_designate_primarykey($dbview_name, $pseudo_pk, $views);
  // name of the database view
  $dbview_name = '_tmp_voting_AARGH';
  $pseudo_pk = 'item_id';
  _mysql_views_designate_primarykey($dbview_name, $pseudo_pk, $views);
}
?>

Typically $dbview_name would be webform_views_mywebform (depends on webform name) and $pseudo_pk would be sid. You will have to repeat this depends on no. of Webform nodes to be made available to use with Views.

Clear cache so that your new hook impelementation would be known to Drupal core and Data shall be able to let use our webform node view as base table for Views. 

Now create a new Drupal View, needless to say, still from Administration » Structure » Views » Add new view. Under show you should be able to see a new base table adopted by Data. 

Now you could see separate Fields, Filter and Sort criteria available per field in Webform. 

In case, you find that a field is not properly handled by View, you can change the handler(s) from Administration » Structure » Data tables » Data table. In my case, DateTime field was not properly handled by default, I had to change the handlers to fix the same.

Would like to see Webform comes with a native solution to achieve this in upcoming releases. Have a thought or question? Post it in the comment below to discuss.

Comments

Jonathan (not verified)

Wed, 08/27/2014 - 10:49

Thank you! Very helpful for a few of my projects.

How would you present the webform mysql views numeric data in chart format?

Thanks.

Luis Baquera (not verified)

Sat, 09/13/2014 - 02:54

Great tutorial!

I need my view to filter on a "private" (i.e., admin-set field). It doesn't appear that the private field is making it into the mysql view. I cannot seem to find a setting to include private fields; do you have any ideas on how I can achieve this?

Thanks very much,

-Luis

I am using

Webform 7.x-4.0-beta1
Webform MySQL Views 7.x-1.1
Data 7.x-1.0-alpha6+16-dev
Data Admin UI 7.x-1.0-alpha6+16-dev
Schema 7.x-1.2

I applied the given patch manually, but I do not see the new base table or any of the new functionality/fields available in views. Do you have any suggestions? I'd appreciate any insight.

Thanks!

tima (not verified)

Tue, 05/26/2015 - 10:16

"Now apply the patch from #889306: Allow the designation of a primary key for MySQL views. This can be added to any of your custom modules. You will have to change the {yourcustommodule}_views_data_alter(&$views) hook implementation to match your module's code."
i don't undrestand this setup , and when i well find this code:
<?php
function {yourcustommodule}_views_data_alter(&$views) {
// name of the database view
$dbview_name = 'voting_results';
$pseudo_pk = 'item_id';
_mysql_views_designate_primarykey($dbview_name, $pseudo_pk, $views);
// name of the database view
$dbview_name = '_tmp_voting_AARGH';
$pseudo_pk = 'item_id';
_mysql_views_designate_primarykey($dbview_name, $pseudo_pk, $views);
}
?>

Fernando Vesga (not verified)

Thu, 06/11/2015 - 08:53

Nice tutorial.
But if you don't want to install and patch all these modules, you can follow have a look to this threat https://www.drupal.org/node/1219954
It's a port from a drupal 6 module to show in a view the submission data.