twightml-entitiesshopware6

Shopware 6 Twig - HTML Entity decode


I try to decode html entities like & which are inside the products description and should be decoded for Feedexport.

As the entities are already inside of the description the |raw filter will not work. It seems that |html_entity_decode is not available in Shopware 6 Twig?

Are there any other options to decode entities like & in twig?


Solution

  • Other than replacing every possible html entity with the decoded value, no.

    You could create a small plugin that adds a twig filter for decoding html entities.

    <!-- <plugin root>/src/Resources/config/services.xml -->
    <services>
        <service id="SwagExample\Twig\HtmlEntityDecodeExtension" public="true">
            <tag name="twig.extension"/>
        </service>
    </services>
    
    <?php declare(strict_types=1);
    
    namespace SwagExample\Twig;
    
    use Twig\Extension\AbstractExtension;
    use Twig\TwigFilter;
    
    class HtmlEntityDecodeExtension extends AbstractExtension
    {
        public function getFilters(): array
        {
            return [
                new TwigFilter('html_entity_decode', $this->htmlEntityDecode(...), ['is_safe' => ['html']]),
            ];
        }
    
        public function htmlEntityDecode(string $text): string
        {
            return html_entity_decode($text);
        }
    }
    

    Then product.description|html_entity_decode etc should work.