Last updated August 1, 2012. Created by quicksketch on September 26, 2011.
Edited by tobiasb, todo_drupal, heylookalive, Sk8erPeter. Log in to edit this page.

An example for your particular use-case would look like the following:

- Make a new directory in sites/all/modules called "mywebform_extra".
- Make a new text file in sites/all/modules/mywebform_extra called mywebform_extra.info. Put this inside of it:

name = Webform Extra
description = Customizations for the Webform module.
core = 6.x
package = Webform
dependencies[] = webform

- Make a new text file in sites/all/modules/mywebform_extra called mywebform_extra.module. Put this inside of it:

<?php
/**
* Implementation of hook_form_alter().
*/
function mywebform_extra_form_alter(&$form, &$form_state, $form_id) {
 
// Add validation for a particular Webform node:
 
if ($form_id == 'webform_client_form_44') {
   
// Simply add the additional validate handler.
   
$form['#validate'][] = 'mywebform_extra_validate_44';
   
// Add the submit handler after the existing Webform submit handler,
    // but before the second Webform handler. Pop off the first one and add
    // ours second.
   
$first = array_shift($form['#submit']);
   
array_unshift($form['#submit'], $first, 'mywebform_extra_submit_44');
  }
}
/**
* Validation handler for Webform ID #44.
*/
function mywebform_extra_validate_44(&$form, &$form_state) {
  global
$user;
  if (!isset(
$user->roles[4])) {
   
form_set_error('', t('Your user role is not allowed to submit this Webform.'));
  }
}
/**
* Submit handler for Webform ID #44.
*/
function mywebform_extra_submit_44(&$form, &$form_state) {
  global
$user;
 
// Changes can be made to the Webform node settings by modifying this variable:
 
$form['#node']->webform;
 
// Insert things into other database tables or modify properties.
}
?>

- Note that "webform_client_form_44" means modify the Webform form for the node with NID "44". Adjust this to match whichever webform node's form you're modifying.
- Turn on the new Webform Extra module you've just created.

Besides this tutorial, there is also an excellent reference at http://www.drupalcoder.com/blog/additional-processing-in-drupals-webform...

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

I am trying to use the following code to insert data into a component, but with no luck. I have made sure that mywebform_extra_submit_16 is actually called, but $form['submitted'][6] = 'Joe'; doesn't seem to fill the component with the desired value.

Anyone who have any idea what I am doing wrong?

<?php
function mywebform_extra_submit_16($form, &$form_state) {
  global
$user;
 
// Changes can be made to the Webform node settings by modifying this variable:
 
$form['#node']->webform;
 
// Insert things into other database tables or modify properties.
 
$form['submitted'][6] = 'Joe';
}
?>

Change the $form to &$form in the function declaration.

<?php
function mywebform_extra_submit_16(&$form, &$form_state) {
}
?>

Ick bin ein Berliner

Is this the official way of inserting post processing code. There have been post on Webform PHP which depreciated. Are there replacement module for Webform PHP. Thanks

Huy Lam

Is this the official way of inserting post processing code?

Yes, this handbook page is the official way to handle validation/processing. Webform PHP uses this exact same approach internally, the only difference is that it allows end-users to enter code through a UI and then save it into the database. Saving and loading code from the database however is difficult to debug, easy to break, and dangerously easy to expose your site to malicious users.

For Webform 3.x you should create a small module and take advantage of hooks. For example altering submitted data is really this easy:

/**
* Implements hook_webform_submission_presave()
*
* Edit some webform form values before saving to database.
*
* @see http://api.drupalize.me/api/drupal/function/hook_webform_submission_presave/6
*/
function mymodule_webform_submission_presave($node, &$submission) {
  switch ($node->nid) {
    case 44: //node id
      $cid = 10; //component id
      $submission->data[$cid]['value'][0] = "new value";
      break;
  }
}

See all available hooks in http://api.drupalize.me/api/drupal/group/webform_hooks

--
Perttu Helle

Can I use this to add an action to the form, something like:
action="https://www.somewhere?encoding=UTF-8" method="POST" onSubmit="return validate(this);"
??

Would I just add that where you say:"// Insert things into other database tables or modify properties."?

Or can I just put an action into a hidden input tag, using the "Markup" webform input type?

Did you ever get this resolved?