4

As per the Magento documentation, we can create custom directive like {{var}} {{if}} {{depends}} using this documentation https://devdocs.magento.com/guides/v2.4/frontend-dev-guide/templates/template-email-migration.html#abstraction-example.

We are using filters like nl2br, escape, raw So, Is it possible to extend the filters also? for eg. strtoupper to convert all things In upper case.

UPDATE

etc/di.xml

 <type name="Magento\Framework\Filter\DirectiveProcessor\Filter\FilterPool">
     <arguments>
         <argument name="filters" xsi:type="array">
             <item name="capitalize" xsi:type="object">Vendor\Module\Model\CapitalizeWord</item>
         </argument>
     </arguments>
 </type>

CapitalizeWord.php

 <?php
 namespace Vendor\Module\Model;
 use Magento\Framework\Filter\DirectiveProcessor\FilterInterface; 
 class CapitalizeWord implements FilterInterface
 {
     public function getName(): string
     {
         return 'capitalize';
     }
     /**
     * @inheritDoc
     */
     public function filterValue($value, array $params): string
     {
        return ucwords($value);

     }
 }
4

2 Answers 2

0

You write something similar to my comment

https://github.com/magento/magento2/blob/2.4/lib/internal/Magento/Framework/Filter/DirectiveProcessor/Filter/EscapeFilter.php

It would be StrtoupperFilter.php for example

use Vendor\Extension\Filter\DirectiveProcessor\Filter;
use Magento\Framework\Escaper;
use Magento\Framework\Filter\DirectiveProcessor\FilterInterface;

/**
 * StrtoupperInput
 */
class StrtoupperFilter implements FilterInterface {

Where

/**
 * @inheritDoc
 */
public function getName(): string
{
    return 'strtoupper';
}

Adapt the following to you needs

public function filterValue(string $value, array $params): string

And then add di XML. Structure would look a bit like this...

https://github.com/magento/magento2/blob/e42ed8dcf89ce65d7ba4af489fc6e6cd764e161b/app/code/Magento/Email/etc/di.xml#L85-L92

Vendor/Extension/etc/di.xml
    <type name="Magento\Framework\Filter\DirectiveProcessor\Filter\FilterPool">
        <arguments>
            <argument name="filters" xsi:type="array">
                <item name="strtoupper" xsi:type="object">Vendor\Extension\Filter\DirectiveProcessor\Filter\StrtoupperFilter</item>
            </argument>
        </arguments>
    </type>

The FilterPool mechanisnm will pick it up and add it to the pool

Then should be the case of

{{var param|strtoupper}}

in template

3
  • 1
    I have tried this but this filter pool not call when email template is render. May be because this file will call when filter apply for the template github.com/magento/magento2/blob/… Aug 3, 2020 at 16:06
  • Can you post your code? Aug 3, 2020 at 16:48
  • I have updated my question with Code. Aug 4, 2020 at 6:50
0

There does not appear to be any resolution - any resolution will be much appreciated.

1

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.