diff --git a/code/Control/UserDefinedFormController.php b/code/Control/UserDefinedFormController.php index eb277cd94..9979e0d7f 100644 --- a/code/Control/UserDefinedFormController.php +++ b/code/Control/UserDefinedFormController.php @@ -256,6 +256,9 @@ public function process($data, $form) } } + // set visibility flag according to display rules + $submittedField->Displayed = $field->isDisplayed($data); + if (!empty($data[$field->Name])) { if (in_array(EditableFileField::class, $field->getClassAncestry())) { if (!empty($_FILES[$field->Name]['name'])) { @@ -305,6 +308,8 @@ public function process($data, $form) $submittedFields->push($submittedField); } + $visibleSubmittedFields = $submittedFields->filter('Displayed', true); + $emailData = [ 'Sender' => Security::getCurrentUser(), 'HideFormData' => false, @@ -350,6 +355,10 @@ public function process($data, $form) // This string substitution works for both HTML and plain text emails. // $recipient->getEmailBodyContent() will retrieve the relevant version of the email $emailData['Body'] = SSViewer::execute_string($recipient->getEmailBodyContent(), $mergeFields); + // only include visible fields if recipient visibility flag is set + if ((bool) $recipient->HideInvisibleFields) { + $emailData['Fields'] = $visibleSubmittedFields; + } // Push the template data to the Email's data foreach ($emailData as $key => $value) { diff --git a/code/Model/Recipient/EmailRecipient.php b/code/Model/Recipient/EmailRecipient.php index 6a056d898..a132617eb 100644 --- a/code/Model/Recipient/EmailRecipient.php +++ b/code/Model/Recipient/EmailRecipient.php @@ -80,6 +80,7 @@ class EmailRecipient extends DataObject 'EmailTemplate' => 'Varchar', 'SendPlain' => 'Boolean', 'HideFormData' => 'Boolean', + 'HideInvisibleFields' => 'Boolean', 'CustomRulesCondition' => 'Enum("And,Or")' ]; @@ -303,6 +304,10 @@ public function getCMSFields() 'HideFormData', _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.HIDEFORMDATA', 'Hide form data from email?') ), + CheckboxField::create( + 'HideInvisibleFields', + _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.HIDEINVISIBLEFIELDS', 'Hide invisible fields from email?') + ), CheckboxField::create( 'SendPlain', _t( diff --git a/code/Model/Submission/SubmittedFormField.php b/code/Model/Submission/SubmittedFormField.php index 60059a5e1..828d93b2f 100755 --- a/code/Model/Submission/SubmittedFormField.php +++ b/code/Model/Submission/SubmittedFormField.php @@ -19,7 +19,8 @@ class SubmittedFormField extends DataObject private static $db = [ 'Name' => 'Varchar', 'Value' => 'Text', - 'Title' => 'Varchar(255)' + 'Title' => 'Varchar(255)', + 'Displayed' => 'Boolean', ]; private static $has_one = [ diff --git a/docs/en/userguide/form-submissions.md b/docs/en/userguide/form-submissions.md index 794dae79b..b74bcfca1 100644 --- a/docs/en/userguide/form-submissions.md +++ b/docs/en/userguide/form-submissions.md @@ -50,6 +50,10 @@ In this field you can add a custom message to add to the email You can check this if you do not wish for the email recipient to see the form submission's data in the email. +#### Hide invisible fields from email? + +You can check this if you want to hide the fields from the email that were invisible to the user according to the display rules set up for the form fields. + #### Send email as plain text? You can check this if you want to remove all of the HTML from the email, this means the email diff --git a/lang/en.yml b/lang/en.yml index 43ba9dfff..6be699c2d 100644 --- a/lang/en.yml +++ b/lang/en.yml @@ -277,6 +277,7 @@ en: EmailFromContent: 'The from address allows you to set who the email comes from. On most servers this will need to be set to an email address on the same domain name as your site. For example on yoursite.com the from address may need to be something@yoursite.com. You can however, set any email address you wish as the reply to address.' FROMADDRESS: 'Send email from' HIDEFORMDATA: 'Hide form data from email?' + HIDEINVISIBLEFIELDS: 'Hide invisible fields from email?' ORSELECTAFIELDTOUSEASFROM: '.. or select a field to use as reply to address' ORSELECTAFIELDTOUSEASTO: '.. or select a field to use as the to address' PLURALNAME: 'Base Pages' diff --git a/templates/email/SubmittedFormEmailPlain.ss b/templates/email/SubmittedFormEmailPlain.ss index 730842d57..bbc15ec66 100644 --- a/templates/email/SubmittedFormEmailPlain.ss +++ b/templates/email/SubmittedFormEmailPlain.ss @@ -2,9 +2,9 @@ $Body.RAW <% if not $HideFormData %> - * - <% loop $Fields %> - * <% if $Title %>$Title<% else %>$Name<% end_if %> - * $FormattedValue - <% end_loop %> + * + <% loop $Fields %> + * <% if $Title %>$Title<% else %>$Name<% end_if %> + * $FormattedValue + <% end_loop %> <% end_if %>