The API additions in webform 3.x are great. One question I have though, what is the
best-practice solution for modifying an existing webform submission without user intervention?

In my scenario, I would like to programmatically change a few values of an existing webform
submission in order to indicate that the webform submission has been reviewed by an
external process. In the existing case, I am accomplishing this by doing a SQL UPDATE on
the webform_submitted_data table. The problem with this approach, however is the solution
is brittle because it makes assumptions about the schema of the table that may change.

Comments

quicksketch’s picture

$submission = webform_submission_load($sid);
// change properties;
webform_submission_update($submission);
stephen.moz’s picture

Ok, that was easy. Thanks for your patience and help clueing me in.

quicksketch’s picture

Status: Active » Fixed
stephen.moz’s picture

Ok, there were a couple of issues:

1) using this approach is tightly-coupled with hook_presave, but the problem is sometimes the user may want to do an update *without* triggering the presave hook (for example, a presave hook that changes a date or a numeric value when the end-user is interacting with the form, but such change is not desirable when some other process is interacting with the form);

2) webform_submission_update() does not appear to work as expected when implemented in a module that does not include webform.submissions.inc, for example calling mymodule_webform_submission_update() does not pass the correct arguments unless invoked automatically by Drupal as a hook. Sometimes the user may want to invoke the function manually.

quicksketch’s picture

using this approach is tightly-coupled with hook_presave

I assume you mean with hook_webform_submission_presave()? hook_presave() is a NodeAPI hook. Regardless, presave() hooks are fired normally when saving any node or comment in Drupal, you cannot skip these hooks nor is it ever necessary with modules that are written correctly.

when implemented in a module that does not include webform.submissions.inc

That's right, you'll need to use module_load_include('inc', 'webform', 'includes/webform.submissions').

stephen.moz’s picture

Yes, I did mean hook_webform_submission_presave() ... thanks for helping me clarify that.

Admittedly, terms like "written correctly" and "tightly-coupled" may be causing us to talk past one another.

To try to illustrate, I offer this simplified use-case:

  • Step1: module_developer_alice creates the foochecker module that implements foochecker_webform_submission_presave()
  • Step2: foochecker_webform_submission_presave is in charge of running automatic-spelling-correction on all webform submitted values prior to saving (yes, there are probably better ways to spell-check, this is just an example)
  • Step3: foochecker_webform_submission_presave works perfectly, thanks to the excellent webform API that calls the appropriate function automatically
  • Step4: module_developer_bobby working on the same site creates the fooupdater function
  • Step5: fooupdater function attempts to use webform_submission_update to make some conditional unrelated updates to already-saved webform submissions
  • Step6: module_developer_bobby does not want his updates to be run through the automatic-spelling-correction implemented by module_developer_alice.

It appears, according to the current webform 3.x API, module_developer_bobby is left
with the dilemma: How do I use webform_submission_update without automatically
triggering the automatic-spelling-correction implemented by module_developer_alice?

This essentially was the goal in the original question, although the clarity in the original question
admittedly could be improved.

In any event, thanks for the feedback and help you provided; and of course, thanks for all the work on this module!

quicksketch’s picture

If you didn't want the submission to be changed upon update, then the presave function written by module_developer_alice should check if $submission->sid is set, this way it only takes effect on new submission and not ones that are being edited/updated by module_developer_bobby.

Or, if you want to modify things only if they're submitted through the UI and not through the API, then you should add additional submit/validation functions to the webform form itself through hook_form_alter().

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.