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
Comment #1
quicksketchComment #2
stephen.moz commentedOk, that was easy. Thanks for your patience and help clueing me in.
Comment #3
quicksketchComment #4
stephen.moz commentedOk, 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.
Comment #5
quicksketchI 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.
That's right, you'll need to use
module_load_include('inc', 'webform', 'includes/webform.submissions').Comment #6
stephen.moz commentedYes, 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:
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!
Comment #7
quicksketchIf 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().