1

I added tomodomo/twig-pluralize-extension to my project composer, loaded with a custom module like that :

if (Craft::$app->request->getIsSiteRequest()) {
    // Add in our Twig extension
    $pluralize = new \Tomodomo\Twig\Pluralize;
    Craft::$app->view->registerTwigExtension($pluralize);
}

I use this to pluralize some string in my templates and email templates. When I check the url to my email template, everything work fine but the email didn’t send out because of "unknown function pluralize".

I have another custom module (to handle something else in my emails) and this part work fine.

Do I need to register/declare TwigExtensions with another method to get them work in emails ?

Note:

  1. The only error related to my emails in the logs is about pluralize function missing.

  2. Everything work fine if I don’t use |pluralize

2 Answers 2

1

All you should need to do is remove this condition check:

if (Craft::$app->request->getIsSiteRequest()) {

You are only registering the twig extension for front end (site) requests, but emails can be triggered both on a cart complete front end (site) requests AND a back-end (CP) order status changes.

1
  • May I use a double condition check ? Like so : if (Craft::$app->request->getIsSiteRequest() || Craft::$app->getRequest()->getIsCpRequest()) { The email using |pluralize is sent from my other custom module, not from a Order Status Change. Feb 4, 2019 at 13:49
1

Replace my if condition by

if ( Craft::$app->request->getIsSiteRequest() || Craft::$app->getRequest()->getIsCpRequest() ) {

Thanks to Luke Holder for pushing in the right direction!

Your Answer

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

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