Twig has much more possibilities, so in ym use-case I want to use it.

I see there is branch twig: https://github.com/batkor/commerce_order_email/blob/twig/src/EventSubscr...

Will this brange be merged?
What needs to be done for it?
Can I do it?

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Bobík created an issue. See original summary.

rszrama’s picture

Title: Support Twig in email template » Support rendering an email using a Twig template
j.’s picture

Would be great if we could theme these via twig!

anybody’s picture

I agree this would make sense, to allow more reliable and flexible definition of the mail templates, similar to commerce-order-receipt.html.twig.
I think it would even be the better alternative to #3309591: Add CKEditor support to the email body textarea, at least for developers / themers, propably not for non-coders.

So we could think about:
a) Having this as contrib or submodule
b) Having this configurable
c) Have an override order, so that the textarea / WYSIWYG can be used, but is rendered by a twig file, which gets all the variables as twig variables and is thereby able to fully replace the need to use the textarea / WYSIWYG.

I very much like C as it could be integrated and would use progressive enhancement and standard Drupal logics.

@Honza Pobořil to push this forward, you could write a MR implementing this.

rgnyldz’s picture

It would be nice to have this in-module but an alternative would be to use symfony mailer where we can already use templates for certain policies. And this module already suggests to use symfony mailer.

rgnyldz’s picture

Any update on this ?

khiminrm’s picture

khiminrm’s picture

Status: Active » Needs review
tcrawford’s picture

As previously mentioned by RgnYldz if using the symfony mailer it is possible already to use twig templates without the patch. Due to this, I am not sure this is really needed. The existing inline template approach also has advantages in some use cases. I can imagine though that if this patch is added it would make sense to consider making it configurable.

As an example of using symfony mailer with a specific hook_preprocess_HOOK() implementation for a specific mail type.

<?php
// mytheme.theme
/**
 * Implements hook_preprocess().
 */
function mytheme_preprocess_email__commerce__commerce_email_publication_fulfillment_mail(&$variables): void
{
  $email = $variables['email'];
  if (!$email instanceof Email) {
    return;
  }
  $entity = $email->getEntity();
  // Currently commerce_email does not set the entity on the email. We can get
  // this from the legacy_message param though.
  if ($entity === NULL) {
    $legacy_message = $email->getParam('legacy_message');
    // The context that is passed to the inline template.
    $context = $legacy_message['params']['body']['#context'] ?? NULL;
    if ($context) {
      $entity = $context[array_key_first($context)];
    }
  }
  if ($entity) {
    // Mail specific pre-processing.
    _mytheme_preprocess_publication_fulfillment_mail($variables, $entity);
  }
}

?>

You can then create a twig template for the specific mail, such as in my case: email--commerce--commerce-email-publication-fulfillment-mail.html.twig

vmarchuk made their first commit to this issue’s fork.

vmarchuk’s picture

Tested MR together with the issue and it works great.

  • vmarchuk committed 005d3536 on 8.x-1.x
    Issue #3156158: Support rendering an email using a Twig template.
    
vmarchuk’s picture

Status: Needs review » Fixed

Committed!

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.

anybody’s picture

This is small, but powerful! Thank you!!

Status: Fixed » Closed (fixed)

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