How to alter the Webform submission information before being saved into the 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 $node
object and the $submission
object 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 $user
object, 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:
/**<br> * Implements hook_webform_submission_presave().<br> */<br><br>function my_module_webform_submission_presave($node, &$submission) {<br> // Get the submission user object.<br> $user = user_load($submission->uid);<br><br> // Check if $user does have the first name filled.<br> if (!empty($user->field_first_name)) {<br> $first_name = $user->field_first_name['und'][0]['value'];<br> // Add the $user first name to the submission data.<br> $submission->data[4]['value'][0] = $first_name;<br> }<br><br> // Same as above but for the last name.<br> if (!empty($user->field_last_name)) {<br> $last_name = $user->field_last_name['und'][0]['value'];<br> $submission->data[5]['value'][0] = $last_name;<br> }<br><br>}
As you can see from my comments, the first thing I do is retrieve the $user
object 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 data
array in the $submission
object. However, as you can see, I hardcoded the webform component id when doing this assignment:
$submission-&gt;data[4]['value'][0] = $first_name;<br>$submission-&gt;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 data
array 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 $node
object 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 $node
object 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.
var switchTo5x = true;stLight.options({"publisher":"dr-8de6c3c4-3462-9715-caaf-ce2c161a50c"});