From 0b7c25a52c38b47681dd78f00377135272b08c6e Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 16 Dec 2024 20:49:00 +0100 Subject: [PATCH] Use the Twig guard tag --- src/Twig/EasyAdminTwigExtension.php | 22 ++++++++----------- templates/components/Icon.html.twig | 5 +++-- templates/crud/form_theme.html.twig | 18 ++++++++++++--- .../crud/includes/_delete_form.html.twig | 4 +++- templates/includes/_css_assets.html.twig | 4 +++- .../includes/_encore_link_tags.html.twig | 4 +++- .../includes/_encore_script_tags.html.twig | 4 +++- templates/includes/_importmap.html.twig | 4 +++- templates/includes/_js_assets.html.twig | 4 +++- 9 files changed, 45 insertions(+), 24 deletions(-) diff --git a/src/Twig/EasyAdminTwigExtension.php b/src/Twig/EasyAdminTwigExtension.php index 63df38b517..d1612b5446 100644 --- a/src/Twig/EasyAdminTwigExtension.php +++ b/src/Twig/EasyAdminTwigExtension.php @@ -14,6 +14,7 @@ use Symfony\Contracts\Translation\TranslatableInterface; use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\UX\Icons\Twig\UXIconRuntime; +use Twig\DeprecatedCallableInfo; use Twig\Environment; use Twig\Error\RuntimeError; use Twig\Extension\AbstractExtension; @@ -45,13 +46,13 @@ public function getFunctions(): array { return [ new TwigFunction('ea_url', [$this, 'getAdminUrlGenerator']), - new TwigFunction('ea_csrf_token', [$this, 'renderCsrfToken']), - new TwigFunction('ea_call_function_if_exists', [$this, 'callFunctionIfExists'], ['needs_environment' => true, 'is_safe' => ['html' => true]]), - new TwigFunction('ea_create_field_layout', [$this, 'createFieldLayout']), - new TwigFunction('ea_importmap', [$this, 'renderImportmap'], ['is_safe' => ['html']]), new TwigFunction('ea_form_ealabel', null, ['node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => ['html']]), - // TODO: remove this when Twig 3.15 is published and we can use the 'guard' tag - new TwigFunction('ea_ux_icon', [$this, 'renderIcon'], ['is_safe' => ['html']]), + // deprecated functions + new TwigFunction('ea_call_function_if_exists', [$this, 'callFunctionIfExists'], ['needs_environment' => true, 'is_safe' => ['html' => true], 'deprecation_info' => new DeprecatedCallableInfo('easycorp/easyadmin-bundle', '4.21.0', 'No alternative is provided because it\'s no longer needed thanks to the Twig guard tag.')]), + new TwigFunction('ea_create_field_layout', [$this, 'createFieldLayout'], ['deprecation_info' => new DeprecatedCallableInfo('easycorp/easyadmin-bundle', '4.8.0', 'No alternative is provided because it\'s no longer needed thanks to the new rendering engine')]), + new TwigFunction('ea_csrf_token', [$this, 'renderCsrfToken'], ['deprecation_info' => new DeprecatedCallableInfo('easycorp/easyadmin-bundle', '4.21.0', 'No alternative is provided because it\'s no longer needed thanks to the Twig guard tag.')]), + new TwigFunction('ea_importmap', [$this, 'renderImportmap'], ['is_safe' => ['html'], 'deprecation_info' => new DeprecatedCallableInfo('easycorp/easyadmin-bundle', '4.21.0', 'No alternative is provided because it\'s no longer needed thanks to the Twig guard tag.')]), + new TwigFunction('ea_ux_icon', [$this, 'renderIcon'], ['is_safe' => ['html'], 'deprecation_info' => new DeprecatedCallableInfo('easycorp/easyadmin-bundle', '4.21.0', 'No alternative is provided because it\'s no longer needed thanks to the Twig guard tag.')]), ]; } @@ -60,8 +61,9 @@ public function getFilters(): array return [ new TwigFilter('ea_flatten_array', [$this, 'flattenArray']), new TwigFilter('ea_filesize', [$this, 'fileSize']), - new TwigFilter('ea_apply_filter_if_exists', [$this, 'applyFilterIfExists'], ['needs_environment' => true]), new TwigFilter('ea_as_string', [$this, 'representAsString']), + // deprecated filters + new TwigFilter('ea_apply_filter_if_exists', [$this, 'applyFilterIfExists'], ['needs_environment' => true, 'deprecation_info' => new DeprecatedCallableInfo('easycorp/easyadmin-bundle', '4.21.0', 'No alternative is provided because it\'s no longer needed thanks to the Twig guard tag.')]), ]; } @@ -216,12 +218,6 @@ public function renderCsrfToken(string $tokenId): string public function createFieldLayout(?FieldCollection $fieldDtos): FieldLayoutDto { - trigger_deprecation( - 'easycorp/easyadmin-bundle', - '4.8.0', - 'The "ea_create_field_layout()" Twig function is deprecated in favor of "ea_create_form_layout()" and it will be removed in 5.0.0.', - ); - return FormLayoutFactory::createFromFieldDtos($fieldDtos); } diff --git a/templates/components/Icon.html.twig b/templates/components/Icon.html.twig index a64c9da5d5..429ff53b34 100644 --- a/templates/components/Icon.html.twig +++ b/templates/components/Icon.html.twig @@ -6,7 +6,8 @@ {% elseif icon.svgContents %} {{ icon.svgContents|raw }} {% else %} - {# TODO: replace this by a call to ux_icon() function directly when Twig 3.15 is released and we can use the 'guard' tag #} - {{ ea_ux_icon(icon.name, attributes.all) }} + {% guard function ux_icon %} + {{ ux_icon(icon.name, attributes.all) }} + {% endguard %} {% endif %} diff --git a/templates/crud/form_theme.html.twig b/templates/crud/form_theme.html.twig index 9ac0f6441d..aada67fe88 100644 --- a/templates/crud/form_theme.html.twig +++ b/templates/crud/form_theme.html.twig @@ -362,7 +362,11 @@ {% if download_uri|default('') is empty %}
{% if formTypeOptions.imagine_pattern is defined and formTypeOptions.imagine_pattern is not empty %} - + {% guard filter imagine_filter %} + + {% else %} + + {% endguard %} {% else %} {% endif %} @@ -372,7 +376,11 @@ {% if formTypeOptions.imagine_pattern is defined and formTypeOptions.imagine_pattern is not empty %} - + {% guard filter imagine_filter %} + + {% else %} + + {% endguard %} {% else %} {% endif %} @@ -380,7 +388,11 @@
{% if formTypeOptions.imagine_pattern is defined and formTypeOptions.imagine_pattern is not empty %} - + {% guard filter imagine_filter %} + + {% else %} + + {% endguard %} {% else %} {% endif %} diff --git a/templates/crud/includes/_delete_form.html.twig b/templates/crud/includes/_delete_form.html.twig index 8009519c83..8cd4aec70b 100644 --- a/templates/crud/includes/_delete_form.html.twig +++ b/templates/crud/includes/_delete_form.html.twig @@ -1,6 +1,8 @@ {# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #}
- + {% guard function csrf_token %} + + {% endguard %}