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
line 464 become
return $env->getRuntime(EscaperRuntime::class)->escape($return, $strategy, $charset, $autoescape);
instead of
return twig_escape_filter($env, $return, $strategy, $charset, $autoescape);
insert this line after line 8:
use Twig\Runtime\EscaperRuntime;
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
| Comment | File | Size | Author |
|---|---|---|---|
| #31 | fix-twig-escape-filter-usage-3457168-31.patch | 729 bytes | julienjoye |
Issue fork drupal-3457168
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3457168-since-twigtwig-3.9
changes, plain diff MR !8546
Comments
Comment #2
raphaelbertrand commentedComment #3
raphaelbertrand commentedComment #4
gauravvvv commentedComment #6
gauravvvv commentedComment #7
cilefen commentedDo we know which commit in 10.3.x broke this?
Comment #8
smustgrave commentedMR has failure and will need test coverage.
Comment #9
raphaelbertrand commentedI tried locally by replacing line 464 with :
return (string) $env->getRuntime(EscaperRuntime::class)->escape($arg, $strategy, $charset, $autoescape);
instead of return $env->getRuntime(EscaperRuntime::class)->escape($env, $return, $strategy, $charset, $autoescape);
and insert use Twig\Runtime\EscaperRuntime; at the begining of file
Comment #11
raphaelbertrand commentedmistake, as the return type is string|null:
i propose this correction :
return $env->getRuntime(EscaperRuntime::class)->escape($arg, $strategy, $charset, $autoescape);
instead of return twig_escape_filter($env, $return, $strategy, $charset, $autoescape);
and insert use Twig\Runtime\EscaperRuntime; at the begining of file
for future, i think that as escape filter is overriden only for html strategy, it will be better to use setEscaper() method instead of overriding filter, but it need to use twig 3.10 minimum and it need more changes than this quick patch
Comment #12
raphaelbertrand commented@cilefen the change that broke this is internal at twig as the twig_escape_filter() is now declared as internal in twig, deprecated, and no more usable directly. As 10.3 use twig 3.9 or 3.10, it bring these changes into drupal.
Comment #14
raphaelbertrand commentedComment #15
raphaelbertrand commentedComment #16
smustgrave commentedbelieve still will need test coverage
Comment #17
raphaelbertrand commentedok no problem. can someone do it ?
Comment #18
raphaelbertrand commentedsmustgrave want test coverage but still nobody to help to do it ?
i don't have time to do it actually and i am still with drupal 10.3.
Comment #19
rajab natshahTested MR8546 manually, Working, Thank you :)
This is a refactor change, current automated testing should pass.
Comment #20
longwaveCan we add a simple test that does this?
Comment #21
bbralaTo be honest I was just checking a related issue on how this class is covered and it seems pretty good. Also aren't we technically doing the same only through a different path?
I'd say we need no extra coverage here.
Comment #22
bbralaThat timing was perfect.
I've added the tests requested to test the fallback of the filter.
Comment #23
smustgrave commentedRan test-only feature https://git.drupalcode.org/issue/drupal-3457168/-/jobs/2883906 which shows the coverage
Looking at the code change nothing stands out.
LGTM
Comment #27
catchThis looks good. Just a 1-1 swap. Committed/pushed to 11.x and cherry-picked back through to 10.3.x, thanks!
Comment #31
julienjoye commentedHey.
It seems that this fix introduces fatal errors on my Drupal instance (with `10.3.6` update), on every places where I use an `html_attr` escape filter.
Here is the error I get:
Solution:
Use
instead of
in `app/core/lib/Drupal/Core/Template/TwigExtension.php`.
Comment #34
raphaelbertrand commented@julienjoye you are right, by mistake the fix have left behind the preprocess of the beginning of the function.
Your code seem to be the good one, but i can't reopen this issue as i am not a maintainer.
Comment #35
raphaelbertrand commented@julienjoye according to https://www.drupal.org/docs/develop/issues/fields-and-other-parts-of-an-...
it seem you need to open a new issue to provide your fix as this one is in status "closed (fixed)"
Comment #36
julienjoye commented@raphaelbertrand Yes no pb. I will do it. Thanks ;)