Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localized dynamic content upon template parsing #41

Open
sertys3 opened this issue Jun 9, 2024 · 0 comments
Open

Localized dynamic content upon template parsing #41

sertys3 opened this issue Jun 9, 2024 · 0 comments

Comments

@sertys3
Copy link

sertys3 commented Jun 9, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant