Allow the use of Twig templating in the Subject, Email and Message fields, so that dynamic content and logic can be used in these fields.

Patch Updates

  • Enables full Twig templating support on Subject, Email and Message fields, including {{ variables }}, {# comments #} and {% logic %}, with all related content accessible through {{ entity }}.
  • Changes the Subject and Email field types from textfield to text areas for improved usability with entering Twig code. The Subject field shows as a single line but is expandable for entering template code, and the Email field is now 3 lines and expandable.
  • The Email field accepts both comma and newline delimiters as separators.
  • Updates the README.txt file with notes about Twig support and provides examples of how it can be used.
  • Token support is available with the Twig Tweak module through the {{ drupal_token('token:name') }} Twig function.

Example Template Usage

Subject

No Twig used

New Content Needs Review

Subject

Using Twig variables

{{ entity.bundle|title }} from {{ entity.Owner.name.0.value }} Needs Review

Subject

Using Twig variables, comments, and conditionals and Twig Tweak's drupal_token() function

  {% set author = entity.Owner.name.0.value %}
  {{ entity.bundle|title }} from {{ author }} Needs Review
  {# Get their attention if it's from someone important #}
  {% if author == "admin" %}
    !!! DO IT NOW !!!
  {% endif %}
  ({{ drupal_token('site:name') }})

Adhoc Email addresses

Add an email in an single-value entity reference field and a standard email box

{{ entity.field_department.entity.field_manager_email.0.value }}, dropbox@example.com

Adhoc Email addresses

Add a standard email box and all emails in an multi-value entity reference field

  dropbox@example.com
  {% for referenced_entity in entity.field_content_owners %}
    {{ referenced_entity.entity.field_email.0.value }}
  {% endfor %}

Message

Can support all the same Twig options, but will run template output through the selected Text Format's (Basic HTML, Full HTML, etc) Input Filters before it will be mailed

Please update this content from {{ entity.Owner.name.0.value }}.

Screenshot of UI Changes

Screenshot of UI changes

Comments

diamondsea created an issue. See original summary.

diamondsea’s picture

StatusFileSize
new7.89 KB
diamondsea’s picture

Title: Add Twig Support in Subject, Email, Message » Add Twig support in Subject, Email, and Message fields
diamondsea’s picture

StatusFileSize
new7.22 KB
diamondsea’s picture

Status: Active » Needs review

Fixed bad patch. Ready for review.

diamondsea’s picture

Issue summary: View changes

Improving summary formatting.

diamondsea’s picture

USEFUL TWIG VARIABLES
---------------------

The most commonly used Twig variables for notifications are:

Current User Email:                       {{ user.email }}
Current User Username (the login name):   {{ user.username }}
Current User DisplayName:                 {{ user.displayname }}
Current User ID (uid):                    {{ user.id }}

Author Email:                             {{ entity.Owner.mail.0.value }}
Author Username (the login name):         {{ entity.Owner.username }}
Author DisplayName:                       {{ entity.Owner.displayname }}
Author id (uid):                          {{ entity.Owner.id }}

Entity Title:                             {{ entity.title.value }}
Entity Bundle (Content type system name): {{ entity.bundle }}
Entity Bundle Label (Content type name):  {{ entity.type.entity.label }}
Entity ID:                                {{ entity.nid.value }}
Entity Revision:                          {{ entity.vid.value }}
Entity UUID:                              {{ entity.uuid.value }}
jhedstrom’s picture

This is great! Could you add some twig stuff to the existing tests (TokenNotificationsTest probably makes the most sense) so we ensure it doesn't break in the future?

mmcintosh’s picture

StatusFileSize
new123.57 KB
new45.57 KB

Taking it out for a spin and reviewing while I am at it. (Drupal 8.6)

Loaded the module as well as Twig Tweak and applied patch #4. Tested the feature examples for subject, adhoc email, and message as well as a couple of the listed useful twig variables.

See example outputs:

screeenshot1

Patched test 1

screenshot2

Patched test 2

A nice feature will come in handy and thanks for the examples and documentation.

diamondsea’s picture

Issue summary: View changes
Status: Needs review » Closed (outdated)
dww’s picture

Status: Closed (outdated) » Needs work
Issue tags: +Needs tests

Since #2959026: Support Twig, TO, CC, BCC, FROM, REPLY-TO, SUBJECT, Message, Abort, Debugging and Tips is too huge to get done, re-opening this so we have a possibility of actually getting the Twig support in. This would be very slick and useful! I have a need for conditional text in some of the notification messages, and this would make it really easy. The only other alternative is to have to try to alter stuff via hook_content_moderation_notification_mail_data_alter() or hook_mail_alter(), which will be much more difficult/messy.

Tagging for 'Needs tests' per #8.

dww’s picture

StatusFileSize
new7.18 KB
new3.88 KB

Needed a fairly significant re-roll in the intervening years. ;)

  1. The 'emails' field is already a textarea in 8.x-3.x branch.
  2. Left the 'subject' field as a textfield, since that definitely cannot include newlines per ancient email RFCs and specs (I can dig up a citation if needed).
  3. Wrapped the README.txt changes to 80 chars.
  4. Removed a drupal_set_message() that would break D9 compatibility and seemed to only be debugging code left in path #4.
  5. Use short array syntax [] not array()
  6. Resolved conflicts with #3088150: Content Moderation Notifications: Email text field quietly truncates longer than 128 characters

Still leaving as NW for the tests (I'll work on that next), but at least this applies cleanly again and current tests pass.

interdiff is all confused since the original doesn't apply. So here's a raw diff of the 2 patch files, FWIW...

dww’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new10.19 KB
new2.95 KB

Now with tests. Maybe not 100% thorough, but at least making sure we can use Twig in emails, subject and body fields of a notification. Happily, interspersing both tokens and Twig works just fine (in the new test, body contains both).

Any other requests for this before it's RTBC?

Thanks!
-Derek

dww’s picture

Issue summary: View changes
Related issues: +#3134866: Extend maximum subject length
StatusFileSize
new10.77 KB
new1.07 KB

Okay, re-reading the whole summary and history here, I see why 'subject' was changed to a textarea. I guess that makes sense. ;) I'm a little worried about undoing the work from #3134866: Extend maximum subject length, but maybe that's for the best...

dww’s picture

Issue summary: View changes
StatusFileSize
new10.75 KB
new2.49 KB

Whoops, now with 100% less new code style violations. ;) Thanks, bot!

spokje’s picture

StatusFileSize
new10.75 KB

Like this, want this!

The only problem I've encountered after applying the patch is that the tokens used in the test body, so [content_moderation_notifications:from-state], [content_moderation_notifications:workflow] and [content_moderation_notifications:to-state] work in what seems any but the Full HTML format.

Weirdly the token browser doesn't show them in any format, but entering them (manually) anyway works (except for the mentioned Full HTML format)

Could be a wonky site setup on my site, adding a quick hack of your test with the body in full_html to check it all works on TestBot

spokje’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new10.75 KB

Re-uploaded patch from #16 so it's the latest, avoid confusion with my unneeded #17 patch (which works, so my full_html worries are gone) and so it gets retested 2-daily when it's declared RTBC...

...which is exactly now. I haven't touch anything on #16, just reuploaded it for the above reasons, so I think I'm entitled to put it on RTBC.

(To go fully overboard on unnecessary worries, I also manually added a lot of new-lines in the subject to check #12.2. Also works fine)

capysara’s picture

I'm using the patch in #16 on Drupal 9.3.0 and CM Notifications 8.x-3.3. Should these changes support tokens in the Adhoc email field, or am I misunderstanding how this works? When I use either of the following (I have another email field that I want to use, but I'm using author mail just for testing purposes):
{{ drupal_token('node:author:mail') }}
or
[node:author:mail]

I get an error when the email gets sent:
Drupal\Core\Entity\EntityStorageException: Email "[node:author:mail]" does not comply with addr-spec of RFC 2822. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 811 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

The mail token displays as expected in the body, it just throws an error in the Adhoc email.

Note (for anyone else looking for the syntax examples): I was able to get the values I wanted in the Adhoc email. I have a field on a content moderated node that's a multi-value entity reference to users. I wanted to send an email to the selected users on a specific transition. This is what worked in the Adhoc email addresses field:

{% for referenced_entity in entity.field_my_field %}
    {{ referenced_entity.entity.mail.0.value }}
{% endfor %}

  • jhedstrom committed 2ab0b87 on 8.x-3.x authored by Spokje
    Issue #2953489 by dww, diamondsea, Spokje, mmcintosh: Add Twig support...
jhedstrom’s picture

Status: Reviewed & tested by the community » Fixed

This looks great. Thanks all!

dww’s picture

Yay, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.