1

I use contact-form plugin together with contact-form-extensions to create a human-readable E-Mail.

I've gotten so far:

------ <br>
<p>
{{ submission.fromName }} <br>
{{ submission.message['firstName'] }} {{ submission.message['lastName'] }} <br>
{{ submission.message['shippingaddress'] }} <br>
{{ submission.message['zip'] }}{{ submission.message['city'] }}
</p>
------ <br>
<p>
{% for submission in craft.contactFormExtensions.submissions %}
   {% if loop.first %}
      Bestelldatum: {{ submission.dateCreated|date('d-m-Y H:i') }}<br>
   {% endif %}
{% endfor %}
E-Mail: <a href="mailto:{{ submission.fromEmail }}">{{ submission.fromEmail }}</a> <br>
Telefon: <a href="tel:{{ submission.message['tel'] }}">{{ submission.message['tel'] }}</a> <br>
Nachricht: {{ submission.message['body'] }}</p>
</p>
------ <br>
<p>Produkte:</p>

{% for submission in craft.contactFormExtensions.submissions %}
   {% if loop.first %}
      {{ submission.message }}
   {% endif %}
{% endfor %}

This works and results in this E-Mail:

enter image description here

Question 1: As you can see the last for loop is formatted somewhat like a JSON array. How can I stop that from happening?

Question 2: Since some of the fields are already printed as a separate tag, is it possible to exclude those fields from the last for loop? Because all custom fields are packed into message[custom] the normal twig tags don't seem to work.

1 Answer 1

2

What are you trying to output with the last loop in your template?

With craft.contactFormExtensions.submissions you're iterating through submissions, not fields of a single submission. In the e-mail template, you already have access to the current submission through the submission variable, you don't need to go through craft.contactFormExtensions.submissions. Looks like this is intended to show a list of submissions (for example, in an admin view of submissions in the frontend).

Question 1: As you can see the last for loop is formatted somewhat like a JSON array. How can I stop that from happening?

Since your submission consists of several fields, submission.message is an associative array with field values. You output that directly, triggering the __toString method of the value object. This just prints a JSON string, since it's the only reasonable thing to do in this case.

Question 2: Since some of the fields are already printed as a separate tag, is it possible to exclude those fields from the last for loop? Because all custom fields are packed into message[custom] the normal twig tags don't seem to work.

Why are you looping through the fields again if you already output all fields above? Looks like you're trying to loop through a specific field in the submission (Produkte), so you'll need something like submission.message.products (replace the last part with the name of the field in your contact form template).

If you want to display some hardcoded fields at the top and then display all other fields in a list, you can use the filter filter to exclude some fields from the loop:

{% set excludedFields = ['firstName', 'shippingAddress', 'zip'] %}
{% for field in submission.message|filter((_, field) => field not in excludedFields) %}
    {{ field }}
{% endfor %}
2
  • Fantastic. Besides the hardcoded fields I have a loop which creates non hardcoded select fields inside the contact form. So I cant just hardcode everything. I'll try it out.
    – KSPR
    Jan 24, 2023 at 11:00
  • @KSPR Yeah, in this case the last code example is probably what you're looking for. By the way, you can also invert the logic if you want to specify an include list instead of an exclude list for the fields to display. Just use in instead of not in in this case, then you only need to specify the fields you want to display. Whichever is easier.
    – MoritzLost
    Jan 24, 2023 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.