Skip to content

Commit

Permalink
Use the Twig guard tag
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed Dec 16, 2024
1 parent 81e468d commit 0b7c25a
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 24 deletions.
22 changes: 9 additions & 13 deletions src/Twig/EasyAdminTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.')]),
];
}

Expand All @@ -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.')]),
];
}

Expand Down Expand Up @@ -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);
}

Expand Down
5 changes: 3 additions & 2 deletions templates/components/Icon.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
</span>
18 changes: 15 additions & 3 deletions templates/crud/form_theme.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,11 @@
{% if download_uri|default('') is empty %}
<div class="ea-lightbox-thumbnail">
{% if formTypeOptions.imagine_pattern is defined and formTypeOptions.imagine_pattern is not empty %}
<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) }}">
{% guard filter imagine_filter %}
<img style="cursor: initial" src="{{ (asset_helper is same as(true) ? asset(image_uri) : image_uri)|imagine_filter(formTypeOptions.imagine_pattern) }}">
{% else %}
<img style="cursor: initial" src="{{ (asset_helper is same as(true) ? asset(image_uri) : image_uri) }}">
{% endguard %}
{% else %}
<img style="cursor: initial" src="{{ asset_helper is same as(true) ? asset(image_uri) : image_uri }}">
{% endif %}
Expand All @@ -372,15 +376,23 @@

<a href="#" class="ea-lightbox-thumbnail" data-ea-lightbox-content-selector="#{{ _lightbox_id }}">
{% if formTypeOptions.imagine_pattern is defined and formTypeOptions.imagine_pattern is not empty %}
<img src="{{ (asset_helper is same as(true) ? asset(image_uri) : image_uri)|ea_apply_filter_if_exists('imagine_filter', formTypeOptions.imagine_pattern) }}">
{% guard filter imagine_filter %}
<img src="{{ (asset_helper is same as(true) ? asset(image_uri) : image_uri)|imagine_filter(formTypeOptions.imagine_pattern) }}">
{% else %}
<img src="{{ (asset_helper is same as(true) ? asset(image_uri) : image_uri) }}">
{% endguard %}
{% else %}
<img src="{{ asset_helper is same as(true) ? asset(image_uri) : image_uri }}">
{% endif %}
</a>

<div id="{{ _lightbox_id }}" class="ea-lightbox">
{% if formTypeOptions.imagine_pattern is defined and formTypeOptions.imagine_pattern is not empty %}
<img src="{{ (asset_helper is same as(true) ? asset(download_uri) : download_uri)|ea_apply_filter_if_exists('imagine_filter', formTypeOptions.imagine_pattern) }}">
{% guard filter imagine_filter %}
<img src="{{ (asset_helper is same as(true) ? asset(download_uri) : download_uri)|imagine_filter(formTypeOptions.imagine_pattern) }}">
{% else %}
<img src="{{ (asset_helper is same as(true) ? asset(download_uri) : download_uri) }}">
{% endguard %}
{% else %}
<img src="{{ asset_helper is same as(true) ? asset(download_uri) : download_uri }}">
{% endif %}
Expand Down
4 changes: 3 additions & 1 deletion templates/crud/includes/_delete_form.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #}
<form class="d-none" method="post" id="delete-form">
<input type="hidden" name="token" value="{{ ea_csrf_token('ea-delete') }}" />
{% guard function csrf_token %}
<input type="hidden" name="token" value="{{ csrf_token('ea-delete') }}" />
{% endguard %}
</form>

<div id="modal-delete" class="modal fade" tabindex="-1">
Expand Down
4 changes: 3 additions & 1 deletion templates/includes/_css_assets.html.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{# @var assets \EasyCorp\Bundle\EasyAdminBundle\Dto\AssetDto[] #}
{% for css_asset in assets %}
{% if css_asset.preload %}
<link rel="preload" href="{{ ea_call_function_if_exists('preload', asset(css_asset.value, css_asset.packageName), { as: 'style', nopush: css_asset.nopush }) }}"
{% guard function preload %}
<link rel="preload" href="{{ preload(asset(css_asset.value, css_asset.packageName), { as: 'style', nopush: css_asset.nopush }) }}"
{% endguard %}
{% for attr, value in css_asset.htmlAttributes %}{{ attr }}="{{ value|e('html_attr') }}" {% endfor %}>
{% else %}
<link rel="stylesheet" href="{{ asset(css_asset.value, css_asset.packageName) }}"
Expand Down
4 changes: 3 additions & 1 deletion templates/includes/_encore_link_tags.html.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{# @var assets \EasyCorp\Bundle\EasyAdminBundle\Dto\AssetDto[] #}
{% for encore_asset in assets %}
{{ ea_call_function_if_exists('encore_entry_link_tags', encore_asset.value, encore_asset.webpackPackageName, encore_asset.webpackEntrypointName, encore_asset.htmlAttributes) }}
{% guard function encore_entry_link_tags %}
{{ encore_entry_link_tags(encore_asset.value, encore_asset.webpackPackageName, encore_asset.webpackEntrypointName, encore_asset.htmlAttributes) }}
{% endguard %}
{% endfor %}
4 changes: 3 additions & 1 deletion templates/includes/_encore_script_tags.html.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{# @var assets \EasyCorp\Bundle\EasyAdminBundle\Dto\AssetDto[] #}
{% for encore_asset in assets %}
{{ ea_call_function_if_exists('encore_entry_script_tags', encore_asset.value, encore_asset.webpackPackageName, encore_asset.webpackEntrypointName, encore_asset.htmlAttributes) }}
{% guard function encore_entry_script_tags %}
{{ encore_entry_script_tags(encore_asset.value, encore_asset.webpackPackageName, encore_asset.webpackEntrypointName, encore_asset.htmlAttributes) }}
{% endguard %}
{% endfor %}
4 changes: 3 additions & 1 deletion templates/includes/_importmap.html.twig
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
{# @var assets \EasyCorp\Bundle\EasyAdminBundle\Dto\AssetDto[] #}
{{ ea_importmap(assets|map(asset => asset.value)) }}
{% guard function importmap %}
{{ importmap(assets|map(asset => asset.value)) }}
{% endguard %}
4 changes: 3 additions & 1 deletion templates/includes/_js_assets.html.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{# @var assets \EasyCorp\Bundle\EasyAdminBundle\Dto\AssetDto[] #}
{% for js_asset in assets %}
{% if js_asset.preload %}
<link rel="preload" href="{{ ea_call_function_if_exists('preload', asset(js_asset.value, js_asset.packageName), { as: 'script', nopush: js_asset.nopush }) }}"
{% guard function preload %}
<link rel="preload" href="{{ preload(asset(js_asset.value, js_asset.packageName), { as: 'script', nopush: js_asset.nopush }) }}"
{% endguard %}
{% for attr, value in js_asset.htmlAttributes %}{{ attr }}="{{ value|e('html_attr') }}" {% endfor %}>
{% else %}
<script src="{{ asset(js_asset.value, js_asset.packageName) }}" {{ js_asset.async ? 'async' }} {{ js_asset.defer ? 'defer' }}
Expand Down

0 comments on commit 0b7c25a

Please sign in to comment.