I am allowing anonymous user create submissions, and creating a user programmatically based on these submissions. How do I associate this submission with the user I've just created?

UPDATE: I thought I could use db_update for this but I'd be interested to know if there is a more functional way.


If you inspect your webform_submissions table you'll see the relevant column. So to do it after the fact you could do:

UPDATE webform_submissions SET uid = 123 WHERE sid = 456;

(just an example, use db_update and proper placeholers for this)

If you do it in the context of hook_webform_submission_presave then you should just be able to update the submission which is being passed by reference:

function mymodule_webform_submission_presave($node, &$submission) {
  global $user;
  $submission->uid = $user->uid;
  • I had used _insert then db_update, but _presave looks easier in this context. I'll try that when I get home and let you know. Thanks.
    – Kartagis
    Jul 14 '15 at 14:46

I would recommend checking out the available hooks provided by the Webform module: http://cgit.drupalcode.org/webform/tree/webform.api.php

It looks like hook_webform_submission_insert() will be your best bet, with that you should be able to not only programmatically create the user at that point, but then update the submission with the newly created user id.

function my_module_webform_submission_insert($node, $submission) {
  // Create new user.
  // Retrieve new user id.
  // Update webform submission with new user id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.