- 🇺🇸United States loopy1492
I was having a ton of issues with this on a site and I think I finally tracked down the problem: it was using twig/twig: ^3.
Here is code for a twig FUNCTION that works with twig/twig 2
my_module/src/TwigExtension/MyModuleInputTwigExtension.php:
namespace Drupal\my_module\TwigExtension; /** * Custom Twig extension to format my field. */ class MyModuleInputTwigExtension extends \Twig_Extension { /** * {@inheritdoc} */ public function getFunctions() { return [ new \Twig_SimpleFunction('format_hours_msg', [$this, 'formatMsg'], ['is_safe' => ['html']]), ]; } /** * Formats a string for proper output. * * @param string $string * The input string. * * @return string * The formatted string. */ public function formatMsg($string) { // string formatting code for $string return $string; } }
my_module.services.yml:
services: my_module.function: class: Drupal\my_module\TwigExtension\MyModuleInputTwigExtension tags: - { name: twig.extension }
But when I tried to use a similar method with /Twig_SimpleFilter or even /Twig_SimpleFunction and whatnot on the other site, it threw:
Uncaught Error: Class "Twig_Extension" not found in /var/www/docroot/modules/custom/my_module/src/TwigExtension/MyModuleInputTwigExtension
When I removed the /TwigExtension/ from the routing file, it threw:
Service 'my_module.twig_extension' for consumer 'twig' does not implement Twig\Extension\ExtensionInterface.
Here is code for a twig FILTER that works with twig/twig 3:
my_module/src/TwigExtension/MyModuleInputTwigExtension.php:
namespace Drupal\my_module\TwigExtension; use Twig\Extension\AbstractExtension; use Twig\TwigFilter; use Twig\TwigFunction; class MyModuleTwigExtension extends AbstractExtension { public function getFilters() { return [ new TwigFilter('to_https', [$this, 'toHttpsFilter']), ]; } public function toHttpsFilter($url) { // Check if the URL starts with 'http:' or 'https:' and replace it with 'https:' $url = preg_replace('/^(http:|https:)?/', 'https:', $url); return $url; } }
I've also see the return line for getFilters() look like this:
'to_https' => new TwigFilter('to_https', [$this, 'toHttpsFilter']),
my_module.services.yml:
services: my_module.twig_extension: class: Drupal\my_module\TwigExtension\MyModuleTwigExtension tags: - { name: twig.extension }
- Status changed to Closed: outdated
3 months ago 9:31pm 16 September 2024 - 🇨🇦Canada joseph.olstad
Please see release notes:
https://www.drupal.org/project/twig_extensions/releases/8.x-2.1 →
- 🇮🇳India ajmaltash
Solution #20 provided by @loopy1492 has answer of the question. Basically issues could be more however the first issue is that you have to take correct class name of the Twig file. We can understand better with below code explanation:
Below code snippet from our custom Twig file "MyCustomUrlFromUri.php" to define a twig filterclass MyCustomUrlFromUri extends AbstractExtension { /** * {@inheritdoc} */ public function getFilters() { return [ new TwigFilter('url_from_uri', [$this, 'urlFromUri']), ]; }
And in our custom service file "my_custom.services.yml", you can see...
my_custom.urlfromuri_twig_extension: class: Drupal\my_custom\Twig\MyCustomUrlFromUri tags: - { name: twig.extension }
So far you have noticed that this "MyCustomUrlFromUri" same at three places, so you too keep it same what ever you are taking the name.
I hope it could help you.