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

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.


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.


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.

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Khanh Hoang

Khanh Hoang - Kenn

Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.

Quảng cáo việc làm


Thích hợp các bạn nữ mảng thợ may làm việc tại nước NGA

Đơn hàng Tuyển dụng 100 Thợ may đi Nga(đợt 1 tháng 3.2021, đợt 2 tháng 5.2021). Lương thực lãnh 800 USD, bao ăn ở, vé máy bay và visa, phí xuất cảnh(1800 USD)trả khi đi làm có lương. Bạn có thể liên hệ CÔNG TY qua Phone/Zalo: (+84) 944 225 212. Công ty sẽ tư vấn cho bạn.

Xem chi tiết: >>> https://bit.ly/3o9NOfR

Tìm kiếm bất động sản





Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết


Những cái tên Facebook "xì tin" của dân mạng Việt

Thế giới Facebook muôn hình muôn vẻ đang ngày càng bành trướng rộng rãi hơn. Đồng thời, mạng xã hội lớn nhất hành tinh dần trở thành "ngôi nhà" thứ 2 cho giới trẻ.

Nguy hại khôn lường khi sử dụng bao cao su

Nguy hại khôn lường khi sử dụng bao cao su

Theo viện nghiên cứu Dị ứng và Miễn dịch của Mỹ, hiện nay, có rất nhiều trường hợp ở cả nam và nữ bị dị ứng với các loại bao cao su được làm từ mủ lấy từ nhựa cây cao su hay các hóa chất có trong bao cao su

How to use open source (and how not to)

Cách sử dụng open source: Drupal, Joomla, Wordpress, Magento

The open source path can be a delightful and cost-effective way to go for a web-based project. However, if you don't understand the primary dos and don'ts of open source, a "free" open source website can quickly become a costly and difficult bear to manage.