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

| Comment | File | Size | Author |
|---|---|---|---|
| #18 | 2953489-16.patch | 10.75 KB | spokje |
Comments
Comment #2
diamondseaComment #3
diamondseaComment #4
diamondseaComment #5
diamondseaFixed bad patch. Ready for review.
Comment #6
diamondseaImproving summary formatting.
Comment #7
diamondseaUSEFUL TWIG VARIABLES
---------------------
The most commonly used Twig variables for notifications are:
Comment #8
jhedstromThis is great! Could you add some twig stuff to the existing tests (
TokenNotificationsTestprobably makes the most sense) so we ensure it doesn't break in the future?Comment #9
mmcintosh commentedTaking 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:
Patched test 1
Patched test 2
A nice feature will come in handy and thanks for the examples and documentation.
Comment #10
diamondseaClosing this ticket in favor of the expanded functionality in #2959026: Support Twig, TO, CC, BCC, FROM, REPLY-TO, SUBJECT, Message, Abort, Debugging and Tips
Comment #11
dwwSince #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.
Comment #12
dwwNeeded a fairly significant re-roll in the intervening years. ;)
textareain 8.x-3.x branch.textfield, since that definitely cannot include newlines per ancient email RFCs and specs (I can dig up a citation if needed).drupal_set_message()that would break D9 compatibility and seemed to only be debugging code left in path #4.[]notarray()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...
Comment #13
dwwNow with tests. Maybe not 100% thorough, but at least making sure we can use Twig in
emails,subjectandbodyfields of a notification. Happily, interspersing both tokens and Twig works just fine (in the new test,bodycontains both).Any other requests for this before it's RTBC?
Thanks!
-Derek
Comment #14
dwwCalling this a child of #2959026: Support Twig, TO, CC, BCC, FROM, REPLY-TO, SUBJECT, Message, Abort, Debugging and Tips.
Comment #15
dwwOkay, 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...
Comment #16
dwwWhoops, now with 100% less new code style violations. ;) Thanks, bot!
Comment #17
spokjeLike 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_htmlto check it all works on TestBotComment #18
spokjeRe-uploaded patch from #16 so it's the latest, avoid confusion with my unneeded #17 patch (which works, so my
full_htmlworries 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)
Comment #19
capysara commentedI'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:
Comment #21
jhedstromThis looks great. Thanks all!
Comment #22
dwwYay, thanks!