Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@
"symfony/security-bundle": "^5.4|^6.0|^7.0",
"symfony/string": "^5.4|^6.0|^7.0",
"symfony/translation": "^5.4|^6.0|^7.0",
"symfony/twig-bridge": "^5.4,>=5.4.48|^6.0,>=6.4.16|^7.0,>=7.1.9",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified as ^5.4.48|^6.4.16|^7.1.9

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll make this change, thanks 🙏

"symfony/twig-bundle": "^5.4|^6.0|^7.0",
"symfony/uid": "^5.4|^6.0|^7.0",
"symfony/ux-twig-component": "^2.21",
"symfony/validator": "^5.4|^6.0|^7.0",
"twig/extra-bundle": "^3.17",
"twig/html-extra": "^3.17",
"twig/twig": "^3.15"
"twig/twig": "^3.20"
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.4|3.5.x-dev",
Expand Down
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 @@ -226,12 +228,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
13 changes: 10 additions & 3 deletions templates/includes/_css_assets.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
{# @var assets \EasyCorp\Bundle\EasyAdminBundle\Dto\AssetDto[] #}
{% for css_asset in assets %}
{% set href = asset(css_asset.value, css_asset.packageName) %}
<link rel="stylesheet" href="{{ (css_asset.preload ? ea_call_function_if_exists('preload', href, { as: 'style', nopush: css_asset.nopush }))|default(href) }}"
{%- for attr, value in css_asset.htmlAttributes %} {{ attr }}="{{ value|e('html') }}"{% endfor %}>
{% if css_asset.preload %}
{% guard function preload %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the preload function is not defined, shouldn't this fallback to the non-preload styles instead of not loading the CSS at all ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In most Symfony projects, yes. In this project, we always check (and throw an exception) if you don't have the needed code for some feature that you are using (see e.g.

if (!class_exists('Symfony\\Component\\WebLink\\Link')) {
throw new \RuntimeException(sprintf('You are trying to preload an asset called "%s" but WebLink component is not installed in your project. Try running "composer require symfony/web-link"', $this->dto->getValue()));
}
)

Then, if we always check first, why do we need this guard tag? Because our checks are at runtime and Twig fails at compile-time if some function doesn't exist. That's why we need the compile-time protection that guard brings. Thanks!

{% set href = asset(css_asset.value, css_asset.packageName) %}
<link rel="stylesheet" href="{{ preload(href, { as: 'style', nopush: css_asset.nopush }) }}"
{%- for attr, value in css_asset.htmlAttributes %} {{ attr }}="{{ value|e('html') }}"{% endfor %}>
{% endguard %}
{% else %}
<link rel="stylesheet" href="{{ asset(css_asset.value, css_asset.packageName) }}"
{%- for attr, value in css_asset.htmlAttributes %} {{ attr }}="{{ value|e('html') }}"{% endfor %}>
{% endif %}
{% endfor %}
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 %}
13 changes: 10 additions & 3 deletions templates/includes/_js_assets.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
{# @var assets \EasyCorp\Bundle\EasyAdminBundle\Dto\AssetDto[] #}
{% for js_asset in assets %}
{% set src = asset(js_asset.value, js_asset.packageName) %}
<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' }}
{%- for attr, value in js_asset.htmlAttributes %} {{ attr }}="{{ value|e('html') }}"{% endfor %}></script>
{% if js_asset.preload %}
{% guard function preload %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

{% set src = asset(js_asset.value, js_asset.packageName) %}
<script src="{{ preload(src, { as: 'script', nopush: js_asset.nopush }) }}" {{ js_asset.async ? 'async' }} {{ js_asset.defer ? 'defer' }}
{%- for attr, value in js_asset.htmlAttributes %} {{ attr }}="{{ value|e('html') }}"{% endfor %}></script>
{% endguard %}
{% else %}
<script src="{{ asset(js_asset.value, js_asset.packageName) }}" {{ js_asset.async ? 'async' }} {{ js_asset.defer ? 'defer' }}
{%- for attr, value in js_asset.htmlAttributes %} {{ attr }}="{{ value|e('html') }}"{% endfor %}></script>
{% endif %}
{% endfor %}