Logicify / mautic-advanced-templates-bundle

Plugin extends default email template capabilities with TWIG block so you can use advanced scripting techniques like conditions, loops etc
https://logicify.com/?utm_source=github&utm_campaign=mautic-templates&utm_medium=opensource
MIT License
86 stars 57 forks source link

Localized dynamic content upon template parsing #41

Open sertys3 opened 2 months ago

sertys3 commented 2 months ago

Greetings, thank you for the wonderful work so far. I am setting Mautic as a marketing suite for an ecomm operating in 15+ countries and template localization is a big feature for me. With your plugin I was able to bake into my grapejs templates references to dynamic content for the message generation(mostly footers and impressum). Although it became apparent that these includes are 'strict' without attempt to get their translated entities for leads(contacts) which have their preferred locale set. After a few hours into code inspection I was able to modify the plugin as to fetch the translated entities for dynamic content and it works. Yet in a very hacky way using PHP globals to pass the lead from EmailSubscriber to Twig_Loader_DynamicContent. Basically this in TemplateProcessor.php :

private function processTwigBlock($lead, $tokens = null)
    {
        $this->lead = $lead;
        $GLOBALS['lead'] = $lead;

And then in Twig_Loader_DynamicContent:

private function findTemplate($resourceAlias)
    {
        $model = $this->modelFactory->getModel('dynamicContent');
        $result = $model->getEntities(
            [
                'filter' => [
                    'where' => [
                        [
                            'col' => 'e.name',
                            'expr' => 'eq',
                            'val' => $resourceAlias,
                        ],
                        [
                            'col'  => 'e.isPublished',
                            'expr' => 'eq',
                            'val'  => 1,
                        ]
                    ]
                ],
                'ignore_paginator' => true,
            ]);

        if (count($result) === 0) {
            return null;
        }

        /**** The result array key is the dynamic content ID - So use array_keys and get the first (and only) found key  ****/

        $keys = array_keys($result);  

        $originalEntity = $result[$keys[0]];

        $translatedEntity = $model->getTranslatedEntity($originalEntity,$GLOBALS['lead']);

        $this->logger->debug("Templates: The resolved base entity is :".print_r($translatedEntity[1]->getId(),true));
        $this->logger->debug("Templates: The current lead prefered locale is:".$GLOBALS['lead']['id'] . " and locale is :".$GLOBALS['lead']['preferred_locale'] );

        return $translatedEntity[1]; 
    }

Would you care to hint a more elegant way of passing the Lead down the Twig chain? And would you plan merging such a feature into master for the community?

Regards, Daniel