Support rendering an email using a Twig template

Created on 1 July 2020, over 4 years ago
Updated 1 February 2024, 10 months ago

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?

Feature request
Status

Needs review

Version

1.0

Component

Code

Created by

🇨🇿Czech Republic Honza Pobořil

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Would be great if we could theme these via twig!

  • 🇩🇪Germany Anybody Porta Westfalica

    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 Add CKEditor support to the email body textarea Needs review , 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.

  • 🇹🇷Turkey rgnyldz

    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.

  • 🇹🇷Turkey rgnyldz

    Any update on this ?

  • Status changed to Needs review 11 months ago
  • 🇨🇭Switzerland tcrawford

    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

  • 🇺🇦Ukraine marchuk.vitaliy Rivne, UA

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

  • 🇺🇦Ukraine marchuk.vitaliy Rivne, UA

    Tested MR together with the issue Add CKEditor support to the email body textarea Needs review and it works great.

Production build 0.71.5 2024