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, 7 days ago
Updated 30 June 2024, 3 days ago

Problem/Motivation

with custom theme template when using escape filter with other strategy than html, since drupal 10.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 10.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

Needs work

Version

11.0 🔥

Component
Theme 

Last updated about 4 hours ago

Created by

🇫🇷France raphaelbertrand Lauris

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.69.0 2024