From 647fa8c36d4c17ba04d8284d3d465a5216683760 Mon Sep 17 00:00:00 2001 From: Matheo Daninos Date: Wed, 13 Dec 2023 11:01:04 +0100 Subject: [PATCH] Fix twig deprecation: twig_escape_filter --- src/LiveComponent/src/Util/LiveAttributesCollection.php | 8 +++++++- src/StimulusBundle/src/Dto/StimulusAttributes.php | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/LiveComponent/src/Util/LiveAttributesCollection.php b/src/LiveComponent/src/Util/LiveAttributesCollection.php index 7c99a19f8e7..2c6108d7de4 100644 --- a/src/LiveComponent/src/Util/LiveAttributesCollection.php +++ b/src/LiveComponent/src/Util/LiveAttributesCollection.php @@ -12,6 +12,7 @@ namespace Symfony\UX\LiveComponent\Util; use Twig\Environment; +use Twig\Extension\EscaperExtension; /** * An array of attributes that can eventually be returned as an escaped array. @@ -99,6 +100,11 @@ public function setBrowserEventsToDispatch(array $browserEventsToDispatch): void private function escapeAttribute(string $value): string { - return twig_escape_filter($this->twig, $value, 'html_attr'); + if (method_exists(EscaperExtension::class, 'escape')) { + return EscaperExtension::escape($this->twig, $value, 'html_attr'); + } + + // since twig/twig 3.9.0: Using the internal "twig_escape_filter" function is deprecated. + return (string) twig_escape_filter($this->twig, $value, 'html_attr'); } } diff --git a/src/StimulusBundle/src/Dto/StimulusAttributes.php b/src/StimulusBundle/src/Dto/StimulusAttributes.php index fe0b76929ae..71291b7d28a 100644 --- a/src/StimulusBundle/src/Dto/StimulusAttributes.php +++ b/src/StimulusBundle/src/Dto/StimulusAttributes.php @@ -14,6 +14,7 @@ namespace Symfony\UX\StimulusBundle\Dto; use Twig\Environment; +use Twig\Extension\EscaperExtension; /** * Helper to build Stimulus-related HTML attributes. @@ -214,6 +215,11 @@ private function getFormattedValue(mixed $value): string private function escapeAsHtmlAttr(mixed $value): string { + if (method_exists(EscaperExtension::class, 'escape')) { + return EscaperExtension::escape($this->env, $value, 'html_attr'); + } + + // since twig/twig 3.9.0: Using the internal "twig_escape_filter" function is deprecated. return (string) twig_escape_filter($this->env, $value, 'html_attr'); }