OroCRM Forums

Covering OroCRM topics, including community updates and company announcements.

Forums Forums OroCRM OroCRM – Programming Questions Adding a custom function to twig to use in e-mail templates

This topic contains 4 replies, has 2 voices, and was last updated by  mathijs 5 years, 7 months ago.

Starting from March 1, 2020 the forum has been switched to the read-only mode. Please head to StackOverflow for support.

  • Creator
    Topic
  • #37378

    mathijs
    Participant

    I’ve created an extension with my custom function in it.

    I’ve added it to services.yml:

    When I look if it is found by ORO/Symfony by doing “php bin/console debug:twig”, I can see my custom function appear in the list. When I try to implement it in an email template using the email template editor in OROcrm, I get the following error:

    After a while I figured I should check out what the compiler did, and noticed the getOroEmail_EmailRendererService.php file(s) did not contain my extension.

    I’ve tried manually adding them to the generated file just to see what happens, and resaving the template, which went without errors this time. Yet, when I try to send it using my notificationrules, nothing gets sent. To make sure it wasn’t just a mail server error, I updated the template with nothing but “hello”, which did arrive in my inbox this time.

    So right now I’m probably facing 2 issues, one is that Oro/Symfony doesn’t add the extension to getOroEmail_EmailRendererService.php, and the other is that even if I add it manually just for testing purposes, it still won’t send the email.

    I’ve been stuck on this issue for a few days now and it’s really doing my head in. Anyone got the solution to this problem?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #37403

    Andrey Yatsenko
    Moderator

    Emails have independent twig renderer with the sandbox to disable all not required twig functions there.

    Here is an example, how to add twig extensions to email renderer:
    1) You need a compiler pass, that will add an extension to the email renderer and exclusion to email renderer security policy service:
    https://github.com/oroinc/orocommerce/blob/master/src/Oro/Bundle/ProductBundle/DependencyInjection/CompilerPass/TwigSandboxConfigurationPass.php
    2) also this compiler pass should be registered to the container at the bundle class
    https://github.com/oroinc/orocommerce/blob/master/src/Oro/Bundle/ProductBundle/OroProductBundle.php#L41

    #37419

    mathijs
    Participant

    Thanks Andrey for pointing me in the right direction. I’ve looked at the snippets you linked and recreated my own versions of them and I see both the function and the extension being injected in their respective compiled files, I can also save the template with my function in it. Yet, when I try to send an e-mail using the template containing my function, it won’t send.

    I’ve tried adding the function with and without parameters, I tried having the function return nothing more than ‘hello’ but whatever I attempt, it still seems like the function is blocked somewhere. I don’t see any errors while running oro:message-queue:consume -v either.

    Here are my versions of the TwigSandboxConfigurationPass class and the bundle class:

    • This reply was modified 5 years, 7 months ago by  mathijs.
    #37421

    Andrey Yatsenko
    Moderator

    Please check the signature of the SecurityPolicy:__construct method.

    You are adding functions, not filters as in my example.

    Arguments list index starts from zero,

    To extend the list of allowed functions you need to replace argument with index 4

    #37423

    mathijs
    Participant

    If we ever run into eachother in this lifetime, remind me to buy you a drink Andrey. Thanks for the help!

Viewing 4 replies - 1 through 4 (of 4 total)

The forum ‘OroCRM – Programming Questions’ is closed to new topics and replies.

Back to top