Hướng dẫn alter the Webform submission trước khi lưu database

In this article we are going to look at a cool Webform api function that allows us to interfere with the submissions being saved to the database. Interfere sounds like a negative thing but because the Webform module is so awesome, we can perform all sorts of operations on the submitted data before saving it.

Among the many API functions the Webform module comes with, a cool one is hook_webform_submission_presave() and that's what we will look at in this article. This function gets called whenever a user submits a form but before the submission gets saved. And we have available as parameters the $nodeobject and the $submissionobject that contains the data and that can be altered (passed by reference).

Use case

Why would you need it? To illustrate how you can use it, I am going to show you what I needed, followed by how I implemented it with hook_webform_submission_presave().

My user accounts had some fields on it (first and last name) and all users on the site were invited to submit once a particular webform. And since I already have their first and last names in the $userobject, I wasn't going to ask them to provide them again in the webform. And yes, I needed the webform submission to contain the names.

Implementation

The first thing I did was create 2 hidden fields on the webform for the first and last names. Users filling in the info wouldn't even realize they are there. Next, I implemented the hook:

/**
* Implements hook_webform_submission_presave().
*/

function my_module_webform_submission_presave($node, &$submission) {
  // Get the submission user object.
  $user = user_load($submission->uid);

  // Check if $user does have the first name filled.
  if (!empty($user->field_first_name)) {
    $first_name = $user->field_first_name['und'][0]['value'];
    // Add the $user first name to the submission data.
    $submission->data[4]['value'][0] = $first_name;
  }

  // Same as above but for the last name.
  if (!empty($user->field_last_name)) {
    $last_name = $user->field_last_name['und'][0]['value'];
    $submission->data[5]['value'][0] = $last_name;
  }

}

As you can see from my comments, the first thing I do is retrieve the $userobject of the submission author. Next, I check if the relevant fields are set (to make sure no errors are thrown if there is a user without any names), and assign them to the dataarray in the $submissionobject. However, as you can see, I hardcoded the webform component id when doing this assignment:

$submission->data[4]['value'][0] = $first_name;
$submission->data[5]['value'][0] = $last_name;

This means I looked them up before and am sure they will not change (i.e. get deleted and/or recreated). It's probably not the bestest of solutions but if you are certain of the webform you created, you can do it like this. Another approach would be to iterate through all the components in the dataarray and check for the machine name of the field and do the assignment based on that. So you have plenty of options.

Additionally, you'll notice that I do not use the $nodeobject at all which means that this hook will do its job on all webforms. This is because I only have one webform on the site (and I'm certain I'll only have one). But that's why the $nodeobject is there, so you can check against and condition your logic to certain nodes that meet various criteria. The choices are many.

Conclusion

In this article we've looked at using this awesome hook provided by the Webform module: hook_webform_submission_presave(). You can do a lot of stuff with it. We've seen only how to add aditional information to the submission but you can also alter the passing submission values. So keep in mind there's this nifty function you can use when you're building your webforms.