verbb / formie

The most user-friendly forms plugin for Craft CMS.
Other
94 stars 70 forks source link

Not possible to override "multi-line-text.html" in email templates #1982

Closed handplant closed 1 month ago

handplant commented 1 month ago

Describe the bug

I want to override a few fields in an email notification. I followed the documentation and successfully overridden the "file-upload" field. I did the same for the 'multi-line-text' field, but unfortunately, the template is not being used. It always shows random text.

Bildschirmfoto 2024-07-19 um 10 04 56 Bildschirmfoto 2024-07-19 um 10 10 17

Steps to reproduce

I followed the documentation and created custom email templates. https://verbb.io/craft-plugins/formie/docs/template-guides/email-templates

Form settings

Craft CMS version

Craft Pro 4.10.6

Plugin version

2.1.21

Multi-site?

Yes

Additional context

No response

engram-design commented 1 month ago

So a few things here, firstly the email templates for fields will only be used for bulk variables like "All Form Fields". If you're selecting specific fields to output their content, that's different, and will use the value of the field.

That is to say, if you have something like this:

image

It's going to show the notification as:

image

Note that the former shows things in a slightly more formatted way, using the email template for the field. You've got total control over that. The latter just shows the raw value of the field. Some fields actually still do use the email template when outputting just the field, because they are too complex to output as a single string (Repeater, Table, Element-fields, etc). Just wanted to clarify that first.

Secondly, the email preview is going to be a little different to a real email, and it's going to generate a fake value for the field.

It looks like from your screenshot that you're outputting just singular fields on their own?

handplant commented 1 month ago

I want to format a specific email for a service that extracts data from emails programmatically. For this, I need to format multiline text fields as follows: {{ value|replace({'\r\n': '°', '\r': '°', '\n': '°'}) }}.

I have started to override the fields. The file-upload.html template works, but the multi-line-text.html template does not.

file-upload.html

{% set elements = value.all() %}
{% if elements | length %}
{{ elements[0].filename }}
{% endif %}

multi-line-text.html

{{ value|replace({'\r\n': '°', '\r': '°', '\n': '°'}) }}

Notification config: Bildschirmfoto 2024-07-22 um 11 26 41

Received Email Bildschirmfoto 2024-07-22 um 11 28 04


Unfortunately, I don't really understand your response. It is definitely the case that overriding file-upload works, but multi-line-text does not.

What options do I have?

engram-design commented 1 month ago

As I mentioned, some fields like File Upload fields use their email template for the value in email notifications when referencing a single field like you're doing.

Other fields like Multi-Line Text call getValueAsString and bypass the email template altogether.

That's why when you override the File Upload email template, it's working, but not for other fields like Multi-Line Text. It's only when you are using the consolidated variable tags like "All Form Fields" that always use the email template for fields. If you test using the "All Form Fields" variable, you'll see that the Multi-Line Text field uses your custom template.

It's done this way mostly to handle the two ways in which fields are output, on their own and part of their consolidated collection. Where you want just the value with no formatting for the former, and where you want formatting for the latter (for example, most email templates wrap content in a <p> and show the label). But there are exceptions to this, like the File Upload field, whose value doesn't really translate to a single value and is rendered as a <ul>.

Changing this now would be a breaking change, but we'll improve this in Formie 3.

For right now I'm going to have to introduce a config setting for you to enforce the value of fields to use their email-based HTML. Add a configuration setting to your /config/formie.php file to get the behaviour that you'd expect by setting useEmailTemplateForFieldVariables to true.

Updated for the next release. To get this early, run composer require verbb/formie:"dev-craft-4 as 2.1.21".

handplant commented 1 month ago

Ok, thanks. I think I understand it now. :)

engram-design commented 1 month ago

Fixed in 2.1.23