Problem/Motivation
Some third-party integrations support the processing of data via attachments to an email inbox. For example, SAP ERMS has built-in parsing of XML email attachments in the below format.
<?xml version="1.0" encoding="UTF-8"?>
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<VERSION>1.0</VERSION>
<SENDER>emailaddress</SENDER>
<WEBFORM_ID>ContactUsForm</WEBFORM_ID>
<SOURCE>
<NAME>Lastname</NAME>
<VALUE>Lastname Value</VALUE>
</o2PARAVALU>
<o2PARAVALU>
<NAME>Firstname</NAME>
<VALUE>Firstname value</VALUE>
</o2PARAVALU>
<o2PARAVALU>
<NAME>Emailaddress</NAME>
<VALUE>Emailaddress value</VALUE>
</o2PARAVALU>
</SOURCE>
</asx:values>
</asx:abap>
Proposed resolution
Add an 'Attachment' element which allows site builders to dynamically create files which can be attached to emails and downloaded via the UI.
Remaining tasks
- Refactor WebformManagedFileBase
- Create WebformAttachment form element
- Create WebformAttachment webform element
- Add a route for generating and returning a dynamic file. /webform/{webform}/submissions/{webform_submission}/attachment/{element_key}
- Update EmailWebformHandler to handle the WebformAttachment element
- Write tests
- Create change record
Notes
- Downloading of generated files is only available to submissions stored in the database.
- Webform and element access controls will be applied to download route.
- Submission purging should be used for temp submission storage and file generation.
WebformAttachment element properties
- #name (string)
- #content (long text)
- #twig (boolean)
Display formats
- Link
- URL
- Name
User interface changes
New WebformAttachment element
API changes
EmailWebformHandler needs to be updated.
Data model changes
N/A
| Comment | File | Size | Author |
|---|---|---|---|
| #57 | Contact Webform Demo Site.png | 276.7 KB | jrockowitz |
| #54 | Screen Shot 2019-01-22 at 4.11.19 PM.png | 37.22 KB | ksavoie |
| #53 | 3013498-53.patch | 107.96 KB | jrockowitz |
| #49 | 3013498-48.patch | 107.33 KB | jrockowitz |
| #47 | 3013498-47.patch | 110.19 KB | jrockowitz |
Comments
Comment #2
jrockowitz commentedComment #3
jrockowitz commentedComment #4
jrockowitz commentedComment #10
jrockowitz commentedThe attached patch is really not ready for review because it has no test coverage. I just want to see if it is going to cause any regressions.
Comment #13
jrockowitz commentedComment #17
jrockowitz commentedComment #18
jrockowitz commentedHere is the draft change record and the documentation page.
Comment #21
jrockowitz commentedThis patch refactors the code to remove the unneeded WebformAttachmentManager.
Comment #22
jrockowitz commentedThe attached webform is a working demo of the SAP ERMS XML email attachments.
Comment #23
jrockowitz commentedTo make it easier for themers to alter the generated attachments we should add some Twig template wrappers.
The Twig templates would be just for Twig and Token attachments.
webform-attachment-token.html.twig
webform-attachment-twig.html.twig
These templates would also support some basic template suggestions.
webform-attachment-twig--WEBFORM_ID.html.twig
webform-attachment-twig--WEBFORM_ID--ELEMENT_KEY.html.twig
Comment #25
jrockowitz commentedConsidering how powerful and flexible Twig is, I don't think we need to add template wrappers. Attached is the email example for which now include the below Twig.
Comment #26
jrockowitz commentedThe attached patch allows the file name to be sanitized.
Comment #30
jrockowitz commentedComment #32
ksavoie commentedThis is getting really close the file naming looks to be working.
However I am occasionally getting the following error on submission. Not exactly sure the conditions that cause it.
Also is there any way to enhance the phone field validation. Currently is allows anything as a phone number.
Comment #33
jrockowitz commentedTo prevent the error, you need to check the 'Sanitize file name" setting for the attachment element.
.
Please post general support questions to https://drupal.stackexchange.com/questions/tagged/webforms
Comment #34
ksavoie commentedThe sanitation flag lets the form submit but it strips the date I have configured at the beginning.
I discovered the issue with submitting is with the drop downs containing '/'.
Additionally, if you submit a form that had a file name that would have erred if not for the sanitation flag. Then you disable the flag, the 'result' page errors until you re-enable the flag.
Comment #35
jrockowitz commentedOkay, I will figure out how to prevent the fatal error. I think we might want to turn on sanitize on by default.
Comment #36
ksavoie commentedThat's what I was thinking.
Is it a requirement as part of sanitation the lower casing of all the characters?
Comment #37
jrockowitz commentedThe attached patch fixes the / in file name issue .
The sanitize code is from http://stackoverflow.com/questions/2021624/string-sanitizer-for-filename and Drupal's standard code practice which is to lower case file names.
Comment #39
jrockowitz commentedComment #42
ksavoie commentedIs patch 39 OK to test?
Comment #43
jrockowitz commentedYes please try the patch from 39
Comment #44
ksavoie commentedThe sanitation error looks to be mitigated, however sanitizing still strips the date.
Comment #45
jrockowitz commentedWhat exactly is being removed from the date?
One solution would be to add support for a ':sanitize' token prefix and you can decide which tokens are sanitized.
Comment #46
ksavoie commentedThe literal values of the two fields that append the file name
"Rental Property Owner/Manager"
"Authorization to Allow Management Company to act on behalf of owner"
File Name configuration:
Filename result with sanitize not checked
Filename result with sanitize checked
Comment #47
jrockowitz commentedThe attached patch fixes the slash issue with sanitizing enabled.
Comment #49
jrockowitz commentedRe-rolling patch to include #3026422: Improve webform token :suffix handling
Comment #50
ksavoie commentedEmail Subject field length needs to be increased. Similar issue as with file name. Once I start adding in potential token values, the field truncates.
This is what I'm trying to currently use as a email subject.
Website_ContactUsForm_[webform_submission:completed:custom:Ymdhis]_[webform_submission:values:category:raw]-[webform_submission:values:sub_budget_bill:raw][webform_submission:values:sub_bill_amount:raw][webform_submission:values:sub_refund:raw][webform_submission:values:sub_payment:raw][webform_submission:values:sub_web_admin:raw][webform_submission:values:sub_miscellaneous:raw]Comment #51
ksavoie commentedClicking edit on a field set as required doesn't perpetuate the required status into the edit box. Will disable if saved without re-selecting.
Comment #53
jrockowitz commentedThe attached patch fixes #50 but I am not sure where #51 is happening. Can you please include a screenshot?
Comment #54
ksavoie commentedIn this case (see attached image), if I click edit on this select (that is currently set as required), the edit panel doesn't have 'required' selected. If I click save without making any change, it will no longer be required.
Comment #55
jrockowitz commentedYou need to click the 'Save' button to update the 'Required' states via the form builder.
Comment #56
ksavoie commentedDoesn't make a difference. If I save in main screen or save in edit panel, as soon as I edit the field it is always deselected.
Comment #57
jrockowitz commentedHere is a screenshot of the 'required' and 'Save elements', I mentioned in #55.
Comment #58
ksavoie commentedYes, this is exactly what has been done. The state is saved, I can go away from that form and reopen it and the required state is selected, however as soon as I click edit, the edit panel does not have required selected at the bottom under 'Form Validation'.
Comment #59
jrockowitz commentedYou are right the Required checkbox is no longer working for the element edit form.
What is strange is all versions of Webform are broken. This might be a Drupal core issue.
Comment #60
jrockowitz commentedOkay, the regression is #3024568: Conditional field requirement, customize requrement text and only the latest dev release of the webform module is affected.
Comment #61
jrockowitz commentedI committed the updated patch from #3024568: Conditional field requirement, customize requrement text. Please download the latest dev release to review.
BTW, make sure to clear your browser's cache.
Comment #62
ksavoie commentedBoth fixes appear to be working, however custom required text no longer displays. The required fields will focus but no text. (remember this is a case where two fields are conditionally required, one or the other).
Comment #63
jrockowitz commented@ksavoie Please make sure to clear your browser cache. The custom required text will only appear if an element is conditionally required.
Comment #65
jrockowitz commentedI committed the patch. Please download the latest dev release to review. If there are any new issues please create a new ticket.
Comment #66
ksavoie commentedWhat is the format of the token to write the attachment into the body of a custom message format.
Tried
[webform_submission:values:sap_contact_us_results_xml]
But it just wrote it verbatim into the body.
Comment #67
jrockowitz commentedThe attachment token only renders the link to the attachment's file. Please open a new ticket and we can probably add support for a
[webform_submission:values:sap_contact_us_results_xml:content]token.Comment #68
ksavoie commentedA link would have been fine.
Should [webform_submission:values:sap_contact_us_results_xml] have written the link?
Comment #69
jrockowitz commentedIt depends, please create a new ticket and we can figure out what is happening with attachment element token.
Comment #71
idebr commentedI was about to file a new issue where the 'Resend' option generated an error for a custom file that is being attached with a custom EmailWebformHandler when I stumbled upon this issue. After applying the patch in #53 the form is working again beautifully!
Many thanks @jrockowitz for your great work!