0
votes

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


    class AppExtension extends \Twig_Extension
    {
        private $_container;

        public function __construct($container)
        {
            $this->_container = $container;
        }

        public function getFunctions()
        {
            return [
                new \Twig_SimpleFunction('autologin', [$this, 'autologin'], ['is_safe' => ['all']])
            ];
        }

        public function autologin($customer, $url)
        {
            return sprintf('');
        }

        public function getName()
        {
            return 'xxx_app_extension';
        }
    }

I've added it to services.yml:

    xxx_opportunity.twig.helper:
    class: 'OpportunityBundle\Twig\AppExtension'
    public: false
    arguments:
        - '@service_container'
    tags:
        - { name: twig.extension }

When I look if it is found by ORO 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:

The template for xxxx has syntax error: Unknown "autologin" function at line 18.

This has been doing my head in for way too long, anyone with a solution?


Update:

$instance = new \Oro\Bundle\EmailBundle\Provider\EmailRenderer(${($_ = isset($this->services['oro_email
$instance->addExtension(${($_ = isset($this->services['oro_ui.twig.html_tag']) ? $this->services['oro_u
$instance->addExtension(${($_ = isset($this->services['oro_config.twig.config_extension']) ? $this->ser
$instance->addExtension(${($_ = isset($this->services['oro_ui.twig.extension.formatter']) ? $this->serv
$instance->addExtension(${($_ = isset($this->services['oro_locale.twig.address']) ? $this->services['or
$instance->addExtension(${($_ = isset($this->services['oro_locale.twig.date_time']) ? $this->services['
$instance->addExtension(${($_ = isset($this->services['oro_entity.twig.extension.entity']) ? $this->ser
$instance->addExtension(${($_ = isset($this->services['twig.extension.intl']) ? $this->services['twig.e
$instance->addExtension(${($_ = isset($this->services['oro_locale.twig.date_time_organization']) ? $thi
$instance->addExtension(${($_ = isset($this->services['oro_calendar.twig.dateformat']) ? $this->service
$instance->addExtension(${($_ = isset($this->services['oro_calendar.twig.recurrence']) ? $this->service
$instance->addExtension(${($_ = isset($this->services['oro_currency.twig.currency']) ? $this->services[
$instance->addExtension(${($_ = isset($this->services['twig.extension.routing']) ? $this->services['twi

This is what the compiler ends up with for getOroEmail_EmailRendererService.php, it is lacking my extension as you can see. How can I fix this?

1
Is the namespace OpportunityBundle\Twig also present in the class AppExtension?DarkBee
Yes, there is nothing wrong with the class itself.Matt
Everything looks OK to me as well in your example codeDarkBee
JFYI emails templates doesn't have access to all twig functions and filters, only to narrow setSerhii Polishchuk
@SerhiiPolishchuk Howso? Surely symfony will inject his custom extension class or not?DarkBee

1 Answers

0
votes

Twig for rendering files and twig for rendering email templates are not the same instance. ORO CRM kind of extends Twig functionality for rendering email templates, look at Oro\Bundle\EmailBundle\Provider\EmailRendered. I was unable to find any tag related to this thing so you might need CompilerPass to add extension for EmailRenderer.