First, thanks for this module! It was exactly what I needed when I needed it, amazing.
My webform is for adding a user and I'm also using the Webform Import module to add them in batches from a CSV file. I have a PHP code condition in my rule which checks to see if the username or e-mail address is already in use. I write messages and update the watchdog log when a I can't do the add, but I'd like to write a value back to the webform submission data as well to handle the case of the imported users.
I added a 'result' field to the webform (which will be a link to the user account page on success or an error message on failure), but I can't figure out how to update the submission properly.
I tried this:
$newname = [data:username-value-raw];
$account = user_load(array('name' => $newname));
if (!empty($account)) {
$msg = t('Failed to create user: username %name exists', array('%name' => $newname));
} else {
$newemail = $data[e_mail][value][0];
$account = user_load(array('mail' => $newemail));
if (!empty($account)) {
$msg = t('Failed to create user: e-mail %email in use', array('%email' => $newemail));
}
}
if ($msg) {
drupal_set_message($msg);
watchdog('cox admin', $msg);
$submission = webform_menu_submission_load($sid, $node->nid); // <------ Don't have submission id
$submission->data[result][value][0] = $msg;
webform_submission_update($node, $submission);
return 0;
}
return 1;
So, I added the sid variable to the event and it works swimmingly. I'm sure I'm not the only one who's going to be doing this sort of processing using rules so this might be a good thing to add. Or if someone has a better idea on how to handle this, I'd love any suggestions.
(I'm trying to add the patch but I'm getting a validation error, but it was very straight forward.)
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | 997246-savable_webform_data-4.patch | 5.59 KB | stborchert |
Comments
Comment #1
stborchertHi.
Could you paste the patch to drupalbin instead?
The main goal is to make the data type "webform_data" savable so you can modify any submitted data, right?
Comment #2
justmagicmaria commentedOK, I've thought about this some more. I think we need two things:
1. The submission id (or the submission object all loaded) as a variable so that I can do error checking in PHP in a condition and then save a failure message if the check fails.
2. An action for 'Update submission data' which would be more convenient for things like saving computed values.
I wish I had time to roll a patch for these but not today.
Comment #3
stborchertThe submission id is now available as token (
[data:sid]) and as value in$data.http://drupal.org/cvs?commit=465254
Second part still missing.
Comment #4
stborchertMaria: could you try the attached patch on the latest dev-version (6.x-1.x-dev) and test if this works (yes, of course I've tested it but for me its always working ;) )?
Small usage example:
Guess we have a webform with two components: "email", "reputation" and "workdays". "email" is a simple textfield, "reputation" is a grid and "workdays" is a list of checkboxes (prefilled by webform with days from monday to sunday).
Stefan
Comment #5
justmagicmaria commentedSadly, with this patch I get:
Fatal error: Cannot use object of type stdClass as array in /var/www/zeevee/sites/all/modules/webform_rules/webform_rules.module on line 104
I get this when following the link to the submission form, not after submitting the form.
This is line 104:
Perhaps this should be || instead of &&?
Edited to add that this was actually a problem with the unpatched -dev code and that changing the && to || did, indeed, fix the problem.
Also, both changes are confirmed to work! I used the [data:sid] in the condition PHP to save the error to the submission and I used the 'intelligent saving' of the data array in the action PHP code to do the same. It's a little unfortunate that I can't use intelligent saving in the condition PHP, but that's because of the way Rules works, I suppose, not the way Webform Rules is using it.
Comment #6
stborchertYes, sorry. It should have been
||instead of&&(see #1001438: Fatal error: Cannot use object of type stdClass as array). Fixed this in the latest dev.Great to hear. So its time to create a new minor release (6.x-1.2) and look how to get this into 7.x (there are some problems I couldn't solve yet).
Comment #7
stborchertCreate new release: 6.x-1.2.
Comment #8
stborchertBtw. what type of components did you added to your webform?
I've tried the default ones and couldn't get this error.
Comment #9
justmagicmaria commentedJust a simple textfield.
Comment #11
wmelon commentedI'm trying to do a similar thing, but I don't see how one would go about doing this. I'd like to set the value of a field after form submission. Any tips?
Comment #12
stopbox commentedDitto!
EDIT: FYI, I used a workaround for my particular requirement using Custom Tokens module to load the data into a hidden field on the Webform via a Custom Token.