I want to add base64 encode decode filter in twig. But it's showing Unknown "base64_encode" filter. in Twig\ExpressionParser->getFilterNodeClass()
I have tried with below filter but nothing works.
{% set youtube_id = content.field_youtube_embed_code|render|striptags|trim %}
{{ youtube_id|base64_encode }}
<p>{{ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi dapibus.'|base64_encode }}</p>
This filter is a part of Symfony2 but unfortunately not a part of Drupal Twig package. If you want to use it the simpliest way is to create your own module with something like this (it's just an untested mockup, please review before implementing)
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
class TwigExtensions extends AbstractExtension {
public function getFilters() {
$filters = [];
$filters [] = new \Twig\TwigFilter('base64_encode', function (string $encode) {
return base64_encode($encode);
});
return $filters;
}
}
alternatively Twig Tweak
module PHP filter might do what you want.