5

We have a module which sends emails to customers. To get the data in the email, we're passing it to an html template which then calls a phtml template to actually render the data, passing the data along with it.

The flow is as follows:

  1. In our email sending method (in the controller) we're passing a model (a data object) to the template as variable:
// ...more code...
$this->transportBuilder
    ->setTemplateIdentifier('quotation_email')
    ->setTemplateVars(['quotationRequest' => $quotationRequest])
// ...more code...
  1. From there, we have an html template which then adds header and footer blocks and calls the actual phtml template via a layout handle:
<!--@subject {{trans "Your Quote Request"}}  @-->
{{template config_path="design/email/header_template"}}

{{layout handle="quotation_email" quotationRequest=$quotationRequest area="frontend"}}

{{block type="cms/block" block_id="agb_email_english"}}

{{template config_path="design/email/footer_template"}}
  1. Finally, in the phtml email template we're retrieving the data object with $quotation = $block->getQuotationRequest(); to use it in the template.

Up until 2.4.3 this worked without any issues. Since upgrading to 2.4.4 the template does not work anymore - the quotationRequest is null in the phtml template.

In the migration guide for custom email templates it says

As of Magento 2.3.4, custom email templates are only allowed to use scalar values for variable data. Direct calls to methods are no longer allowed.

This explains why the variable is null and does not contain the object when trying to retrieve it in the phtml template.

Now the question is, how do I properly pass the object from my controller to the phtml template?

1 Answer 1

1

As it turns out, objects are sanitized and removed but array with objects in them are not. That means when I pass the data like this:

$this->transportBuilder
    ->setTemplateIdentifier('quotation_email')
    ->setTemplateVars(['quotationRequest' => ['request' => $quotationRequest]])

I can access the object via $block->getQuotationRequest()['request'] in the phtml template.

While this works, it does not feel like it should. Does anyone know how to do this properly?

Edit: The docs have a section about this: https://devdocs.magento.com/guides/v2.4/frontend-dev-guide/templates/template-email-migration.html#nested-arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.