Since twig/twig 3.9: error with "twig_escape_filter" function usage in /core/lib/Drupal/Core/Template/TwigExtension.php

Created on 26 June 2024, 6 months ago

Problem/Motivation

with custom theme template when using escape filter with other strategy than html, since drupal 8.3 and twig update it produce fatal error:

Call to undefined function Drupal\Core\Template\twig_escape_filter() dans Drupal\Core\Template\TwigExtension->escapeFilter() (ligne 464 de ***/core/lib/Drupal/Core/Template/TwigExtension.php).

it is related to changes in twig escape function visibility: twig/doc/filters/escape.rst
https://github.com/twigphp/Twig/blob/04b1ad3cedfb81f48b6a7ec8d77a2116c604c7d6/doc/filters/escape.rst

Steps to reproduce

update to drupal 8.3 whith composer, it update twig to 3.10.3
use escape filter with another strategy than html, for example: escape('js') or escape('html_attr')
The filter is wrapped to drupal implementation, where fallback use the twig function locally without context. As the implementation of this function change, it is not defined as this so it don't work

Proposed resolution

for now, try using $env->getRuntime(EscaperRuntime::class)->escape() instead of twig_escape_filter() on line 464 of /core/lib/Drupal/Core/Template/TwigExtension.php
as suggested in
https://github.com/twigphp/Twig/blob/3.x/doc/deprecated.rst

for future, as escape filter is overriden only for html strategy, use setEscaper() method instead of overriding filter.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Active

Version

9.3

Component
Theme 

Last updated 4 days ago

Created by

🇫🇷France raphaelbertrand Lauris

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024