Jump to content

Error rendering twig layout


Recommended Posts

 

Prestashop Version 8

PHP Version 8.1.0

I am attempting to extend an email layout following Prestashop documentation but having an issue rendering the template.

From my module, I register the hook and define a template.  

    public function hookActionListMailThemes(array $hookParams)
    {
        if (!isset($hookParams['mailThemes'])) {
            return;
        }

        /** @var ThemeCollectionInterface $themes */
        $themes = $hookParams['mailThemes'];
        $theme = $themes->getByName('modern');
        if (!$theme) {
            return;
        }

        // First parameter is the layout name, second one is the module name (empty value matches the core layouts)
        $orderConfLayout = $theme->getLayouts()->getLayout('payment', '');
        if (null === $orderConfLayout) {
            return;
        }

        //The layout collection extends from ArrayCollection so it has more feature than it seems..
        //It allows to REPLACE the existing layout easily
        $orderIndex = $theme->getLayouts()->indexOf($orderConfLayout);
        $path = __DIR__ . '/mails/layouts/payment.html.twig';

        $theme->getLayouts()->offsetSet($orderIndex, new Layout(
            $orderConfLayout->getName(),
             $path,
            ''
        ));
    }

 

Previewing the template from the back office displays the error:

Could not find layout file:
/Applications/MAMP/htdocs/prestashop8/modules/module-name/mails/layouts/payment.html.twig

The template file is located on the path so there is another issue.

The error occurs on a call to the render function from src/Adapter/MailTemplate/MailTemplateTwigRenderer.php (line 134)

    private function render(
        LayoutInterface $layout,
        LanguageInterface $language,
        $templateType
    ) {
        $layoutVariables = $this->variablesBuilder->buildVariables($layout, $language);
        $layoutVariables['templateType'] = $templateType;
        if (MailTemplateInterface::HTML_TYPE === $templateType) {
            $layoutPath = !empty($layout->getHtmlPath()) ? $layout->getHtmlPath() : $layout->getTxtPath();
        } else {
            $layoutPath = !empty($layout->getTxtPath()) ? $layout->getTxtPath() : $layout->getHtmlPath();
        }

        try {
            $renderedTemplate = $this->twig->render($layoutPath, $layoutVariables);
        } catch (LoaderError $e) {
            throw new FileNotFoundException(sprintf('Could not find layout file: %s', $layoutPath));
        }

        $templateTransformations = $this->getMailLayoutTransformations($layout, $templateType);
        /** @var TransformationInterface $transformation */
        foreach ($templateTransformations as $transformation) {
            $renderedTemplate = $transformation
                ->setLanguage($language)
                ->apply($renderedTemplate, $layoutVariables)
            ;
        }

        return $renderedTemplate;
    }

On further analysis, I noticed $layoutVariables contained the following:

 http://localhost:8888/prestashop8//mails/themes payment en-US html

What area of the application should be updated to correct the file path to /mails/themes for $layoutVariables["mailThemesUrl"]?

Edited by nokyc (see edit history)
Link to comment
Share on other sites

  • 4 weeks later...
  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...