Skip to content

Commit da8f4e2

Browse files
committed
feature #6656 Use the Twig guard tag (javiereguiluz)
This PR was squashed before being merged into the 4.x branch. Discussion ---------- Use the Twig guard tag Twig 3.5 added the nice `guard` tag (https://twig.symfony.com/doc/3.x/tags/guard.html) to solve problems like the ones we have. So, let's use it. Commits ------- 99f4f73 Use the Twig guard tag
2 parents da2c12d + 99f4f73 commit da8f4e2

File tree

10 files changed

+61
-29
lines changed

10 files changed

+61
-29
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@
3333
"symfony/security-bundle": "^5.4|^6.0|^7.0",
3434
"symfony/string": "^5.4|^6.0|^7.0",
3535
"symfony/translation": "^5.4|^6.0|^7.0",
36+
"symfony/twig-bridge": "^5.4,>=5.4.48|^6.0,>=6.4.16|^7.0,>=7.1.9",
3637
"symfony/twig-bundle": "^5.4|^6.0|^7.0",
3738
"symfony/uid": "^5.4|^6.0|^7.0",
3839
"symfony/ux-twig-component": "^2.21",
3940
"symfony/validator": "^5.4|^6.0|^7.0",
4041
"twig/extra-bundle": "^3.17",
4142
"twig/html-extra": "^3.17",
42-
"twig/twig": "^3.15"
43+
"twig/twig": "^3.20"
4344
},
4445
"require-dev": {
4546
"doctrine/doctrine-fixtures-bundle": "^3.4|3.5.x-dev",

src/Twig/EasyAdminTwigExtension.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Contracts\Translation\TranslatableInterface;
1515
use Symfony\Contracts\Translation\TranslatorInterface;
1616
use Symfony\UX\Icons\Twig\UXIconRuntime;
17+
use Twig\DeprecatedCallableInfo;
1718
use Twig\Environment;
1819
use Twig\Error\RuntimeError;
1920
use Twig\Extension\AbstractExtension;
@@ -45,13 +46,13 @@ public function getFunctions(): array
4546
{
4647
return [
4748
new TwigFunction('ea_url', [$this, 'getAdminUrlGenerator']),
48-
new TwigFunction('ea_csrf_token', [$this, 'renderCsrfToken']),
49-
new TwigFunction('ea_call_function_if_exists', [$this, 'callFunctionIfExists'], ['needs_environment' => true, 'is_safe' => ['html' => true]]),
50-
new TwigFunction('ea_create_field_layout', [$this, 'createFieldLayout']),
51-
new TwigFunction('ea_importmap', [$this, 'renderImportmap'], ['is_safe' => ['html']]),
5249
new TwigFunction('ea_form_ealabel', null, ['node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => ['html']]),
53-
// TODO: remove this when Twig 3.15 is published and we can use the 'guard' tag
54-
new TwigFunction('ea_ux_icon', [$this, 'renderIcon'], ['is_safe' => ['html']]),
50+
// deprecated functions
51+
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.')]),
52+
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')]),
53+
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.')]),
54+
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.')]),
55+
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.')]),
5556
];
5657
}
5758

@@ -60,8 +61,9 @@ public function getFilters(): array
6061
return [
6162
new TwigFilter('ea_flatten_array', [$this, 'flattenArray']),
6263
new TwigFilter('ea_filesize', [$this, 'fileSize']),
63-
new TwigFilter('ea_apply_filter_if_exists', [$this, 'applyFilterIfExists'], ['needs_environment' => true]),
6464
new TwigFilter('ea_as_string', [$this, 'representAsString']),
65+
// deprecated filters
66+
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.')]),
6567
];
6668
}
6769

@@ -226,12 +228,6 @@ public function renderCsrfToken(string $tokenId): string
226228

227229
public function createFieldLayout(?FieldCollection $fieldDtos): FieldLayoutDto
228230
{
229-
trigger_deprecation(
230-
'easycorp/easyadmin-bundle',
231-
'4.8.0',
232-
'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.',
233-
);
234-
235231
return FormLayoutFactory::createFromFieldDtos($fieldDtos);
236232
}
237233

templates/components/Icon.html.twig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
{% elseif icon.svgContents %}
77
{{ icon.svgContents|raw }}
88
{% else %}
9-
{# TODO: replace this by a call to ux_icon() function directly when Twig 3.15 is released and we can use the 'guard' tag #}
10-
{{ ea_ux_icon(icon.name, attributes.all) }}
9+
{% guard function ux_icon %}
10+
{{ ux_icon(icon.name, attributes.all) }}
11+
{% endguard %}
1112
{% endif %}
1213
</span>

templates/crud/form_theme.html.twig

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,11 @@
362362
{% if download_uri|default('') is empty %}
363363
<div class="ea-lightbox-thumbnail">
364364
{% if formTypeOptions.imagine_pattern is defined and formTypeOptions.imagine_pattern is not empty %}
365-
<img style="cursor: initial" src="{{ (asset_helper is same as(true) ? asset(image_uri) : image_uri)|ea_apply_filter_if_exists('imagine_filter', formTypeOptions.imagine_pattern) }}">
365+
{% guard filter imagine_filter %}
366+
<img style="cursor: initial" src="{{ (asset_helper is same as(true) ? asset(image_uri) : image_uri)|imagine_filter(formTypeOptions.imagine_pattern) }}">
367+
{% else %}
368+
<img style="cursor: initial" src="{{ (asset_helper is same as(true) ? asset(image_uri) : image_uri) }}">
369+
{% endguard %}
366370
{% else %}
367371
<img style="cursor: initial" src="{{ asset_helper is same as(true) ? asset(image_uri) : image_uri }}">
368372
{% endif %}
@@ -372,15 +376,23 @@
372376

373377
<a href="#" class="ea-lightbox-thumbnail" data-ea-lightbox-content-selector="#{{ _lightbox_id }}">
374378
{% if formTypeOptions.imagine_pattern is defined and formTypeOptions.imagine_pattern is not empty %}
375-
<img src="{{ (asset_helper is same as(true) ? asset(image_uri) : image_uri)|ea_apply_filter_if_exists('imagine_filter', formTypeOptions.imagine_pattern) }}">
379+
{% guard filter imagine_filter %}
380+
<img src="{{ (asset_helper is same as(true) ? asset(image_uri) : image_uri)|imagine_filter(formTypeOptions.imagine_pattern) }}">
381+
{% else %}
382+
<img src="{{ (asset_helper is same as(true) ? asset(image_uri) : image_uri) }}">
383+
{% endguard %}
376384
{% else %}
377385
<img src="{{ asset_helper is same as(true) ? asset(image_uri) : image_uri }}">
378386
{% endif %}
379387
</a>
380388

381389
<div id="{{ _lightbox_id }}" class="ea-lightbox">
382390
{% if formTypeOptions.imagine_pattern is defined and formTypeOptions.imagine_pattern is not empty %}
383-
<img src="{{ (asset_helper is same as(true) ? asset(download_uri) : download_uri)|ea_apply_filter_if_exists('imagine_filter', formTypeOptions.imagine_pattern) }}">
391+
{% guard filter imagine_filter %}
392+
<img src="{{ (asset_helper is same as(true) ? asset(download_uri) : download_uri)|imagine_filter(formTypeOptions.imagine_pattern) }}">
393+
{% else %}
394+
<img src="{{ (asset_helper is same as(true) ? asset(download_uri) : download_uri) }}">
395+
{% endguard %}
384396
{% else %}
385397
<img src="{{ asset_helper is same as(true) ? asset(download_uri) : download_uri }}">
386398
{% endif %}

templates/crud/includes/_delete_form.html.twig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #}
22
<form class="d-none" method="post" id="delete-form">
3-
<input type="hidden" name="token" value="{{ ea_csrf_token('ea-delete') }}" />
3+
{% guard function csrf_token %}
4+
<input type="hidden" name="token" value="{{ csrf_token('ea-delete') }}" />
5+
{% endguard %}
46
</form>
57

68
<div id="modal-delete" class="modal fade" tabindex="-1">
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
{# @var assets \EasyCorp\Bundle\EasyAdminBundle\Dto\AssetDto[] #}
22
{% for css_asset in assets %}
3-
{% set href = asset(css_asset.value, css_asset.packageName) %}
4-
<link rel="stylesheet" href="{{ (css_asset.preload ? ea_call_function_if_exists('preload', href, { as: 'style', nopush: css_asset.nopush }))|default(href) }}"
5-
{%- for attr, value in css_asset.htmlAttributes %} {{ attr }}="{{ value|e('html') }}"{% endfor %}>
3+
{% if css_asset.preload %}
4+
{% guard function preload %}
5+
{% set href = asset(css_asset.value, css_asset.packageName) %}
6+
<link rel="stylesheet" href="{{ preload(href, { as: 'style', nopush: css_asset.nopush }) }}"
7+
{%- for attr, value in css_asset.htmlAttributes %} {{ attr }}="{{ value|e('html') }}"{% endfor %}>
8+
{% endguard %}
9+
{% else %}
10+
<link rel="stylesheet" href="{{ asset(css_asset.value, css_asset.packageName) }}"
11+
{%- for attr, value in css_asset.htmlAttributes %} {{ attr }}="{{ value|e('html') }}"{% endfor %}>
12+
{% endif %}
613
{% endfor %}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{# @var assets \EasyCorp\Bundle\EasyAdminBundle\Dto\AssetDto[] #}
22
{% for encore_asset in assets %}
3-
{{ ea_call_function_if_exists('encore_entry_link_tags', encore_asset.value, encore_asset.webpackPackageName, encore_asset.webpackEntrypointName, encore_asset.htmlAttributes) }}
3+
{% guard function encore_entry_link_tags %}
4+
{{ encore_entry_link_tags(encore_asset.value, encore_asset.webpackPackageName, encore_asset.webpackEntrypointName, encore_asset.htmlAttributes) }}
5+
{% endguard %}
46
{% endfor %}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{# @var assets \EasyCorp\Bundle\EasyAdminBundle\Dto\AssetDto[] #}
22
{% for encore_asset in assets %}
3-
{{ ea_call_function_if_exists('encore_entry_script_tags', encore_asset.value, encore_asset.webpackPackageName, encore_asset.webpackEntrypointName, encore_asset.htmlAttributes) }}
3+
{% guard function encore_entry_script_tags %}
4+
{{ encore_entry_script_tags(encore_asset.value, encore_asset.webpackPackageName, encore_asset.webpackEntrypointName, encore_asset.htmlAttributes) }}
5+
{% endguard %}
46
{% endfor %}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
{# @var assets \EasyCorp\Bundle\EasyAdminBundle\Dto\AssetDto[] #}
2-
{{ ea_importmap(assets|map(asset => asset.value)) }}
2+
{% guard function importmap %}
3+
{{ importmap(assets|map(asset => asset.value)) }}
4+
{% endguard %}
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
{# @var assets \EasyCorp\Bundle\EasyAdminBundle\Dto\AssetDto[] #}
22
{% for js_asset in assets %}
3-
{% set src = asset(js_asset.value, js_asset.packageName) %}
4-
<script src="{{ (js_asset.preload ? ea_call_function_if_exists('preload', src, { as: 'script', nopush: js_asset.nopush }))|default(src) }}" {{ js_asset.async ? 'async' }} {{ js_asset.defer ? 'defer' }}
5-
{%- for attr, value in js_asset.htmlAttributes %} {{ attr }}="{{ value|e('html') }}"{% endfor %}></script>
3+
{% if js_asset.preload %}
4+
{% guard function preload %}
5+
{% set src = asset(js_asset.value, js_asset.packageName) %}
6+
<script src="{{ preload(src, { as: 'script', nopush: js_asset.nopush }) }}" {{ js_asset.async ? 'async' }} {{ js_asset.defer ? 'defer' }}
7+
{%- for attr, value in js_asset.htmlAttributes %} {{ attr }}="{{ value|e('html') }}"{% endfor %}></script>
8+
{% endguard %}
9+
{% else %}
10+
<script src="{{ asset(js_asset.value, js_asset.packageName) }}" {{ js_asset.async ? 'async' }} {{ js_asset.defer ? 'defer' }}
11+
{%- for attr, value in js_asset.htmlAttributes %} {{ attr }}="{{ value|e('html') }}"{% endfor %}></script>
12+
{% endif %}
613
{% endfor %}

0 commit comments

Comments
 (0)