Integrate Rules with Webform

Webform is a great module to build simple to complex forms for users to fill out, but it has no built-in way to trigger events for Rules. Unfortunately the maintainer is not going to add this kind of functionality anytime in the near future.

Drupal wouldn't be Drupal if you couldn't get around this issue by writing a small module.

The following code triggers an event upon sending the webform, which can be used by Rules. This is useful if you would like to send an email or display a message when the webform is submitted or use any other kind of available action in the system. The current user as well as the webform node and the form data are delivered as a parameter to the executed rule.

Contents of webform_rules.module: <?php /** * Implementation of hook_form_alter(). */ function webform_rules_form_alter(&$form, $form_state, $form_id) { if (strpos($form_id, 'webform_client_form_') !== FALSE) { // Add custom submit handler. $form['#submit'][] = 'webform_rules_client_form_submit'; } } /** * Custom submit handler for webform submissions. */ function webform_rules_client_form_submit($form, &$form_state) { global $user; $node = isset($form['#node']) ? $form['#node'] : new stdClass(); $data = isset($form['#post']['submitted']) ? $form['#post']['submitted'] : array(); rules_invoke_event('webform_rules_submit', $user, $node, $data); } ?> Contents of webform_rules.rules.inc: <?php /** * Implementation of hook_rules_event_info(). * @ingroup rules */ function webform_rules_rules_event_info() { return array( 'webform_rules_submit' => array( 'label' => t('A webform has been submitted'), 'module' => 'Webform', 'arguments' => array( 'user' => array('type' => 'user', 'label' => t('User, who submitted the webform.')), 'node' => array('type' => 'node', 'label' => t('The webform node.')), 'data' => array('type' => 'data', 'label' => t('The submitted webform data.')), ), ), ); } ?>

After installing the module you will see a new entry in the list of available events when creating a new rule and you'll be able to use the webform data.

Download and test the module

If you found any errors while using the module or have support questions or think a new feature would be cool to be integrated use the issue queue of Webform Rules. This will reach a wider audience and increases the chance of an reaction ;).

Stefan Borchert
  • CEO

Stefan maintains several Drupal contributed modules, has been working on Drupal core since the early days, and is author of Drupal 8 Configuration Management (Packt Publishing).