Not able to pass custom variables to email-wrap.html.twig

Created on 21 February 2024, 10 months ago
Updated 17 June 2024, 6 months ago

Problem/Motivation

I'm trying to override the email-wrap.html.twig template for my project. I'm passing some custom variables to the twig template through my custom Email Builder plugin something like $email->setVariable('custom_variable', 'Demo Text');.
But the variable cannot be accessed in the twig using {{ custom_variable }}.

Noticed that the variables are not been handled in template_preprocess_email_wrap(). In template_preprocess_email the custom variables are handled using $variables = array_merge($variables, $email->getVariables());.

Is it done on purpose? Is there any plan to handle it? If not, how we should pass the custom variables to email-wrap.html.twig.

As of now i'm overriding the preprocess function in my custom module.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

💬 Support request
Status

Active

Version

1.0

Component

Code

Created by

🇮🇳India arisen Goa

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @arisen
  • Hello
    Did you try this ? It works for me.

    function your_module_preprocess_email_wrap(array &$variables) {
      $config = \Drupal::config('system.site');
      $variables['site_name'] = $config->get('name');
    }
    
  • 🇮🇳India arisen Goa

    Hi @seda12 yes that works fine if we write the preprocess in our custom module.
    But the question is to make it work without having to do the preprocessing in custom module. So expecting a confirmation from the project maintainer about the correct approach.

  • 🇮🇳India himanshu_jhaloya Indore

    Try this

    function mymodule_preprocess_email_wrap(&$variables) {
    $email = $variables['email'];
    $variables = array_merge($variables, $email->getVariables());
    }

  • 🇬🇧United Kingdom adamps

    This was done like this deliberately.

    The idea is that the variables should be replaced in the "normal" template. The wrap template is aimed to be generic, not related to the contents of the email.

    Please can you explain why you can't use it that way?

  • 🇨🇦Canada phjou Vancouver 🇨🇦 🇪🇺

    I encountered the same issue, but I found a way out, I could access all my variables that have been passed to the email.

    /**
     * Prepares variables for email wrap template.
     */
    function my_module_preprocess_email_wrap(array &$variables) {
      $email = $variables['email'];
      $message = $email->getParam('legacy_message');
    
      $params =  $message['params']['extra'];
    }
    
Production build 0.71.5 2024