Closed Hanmac closed 5 years ago
I confirm this deprecation, it is the cause for #5573.
@phansys but that wouldn't fix the problem because with newer twig the deprecation would still be there?
That's right. Seems that I've overlooked your point, sorry.
The correct way to achieve the same result is using the filter
filter where possible, or moving the if
condition inside the loop.
See https://twig.symfony.com/doc/2.x/deprecated.html#tags:
Adding an if condition on a for tag is deprecated in Twig 2.10. Use a filter filter or an "if" condition inside the "for" body instead (if your condition depends on a variable updated inside the loop)
I'll update the RFC in order to cover the required bump for 3.x
version to ^1.41
. Do you plan to create a PR fixing this?
i don't know how it would need to be done yet even when using filter, i don't know if we need 1. or if we should already jump to the 2. one for the other MR
my pseudo code is this:
for group in sonata_admin.adminPool.dashboardgroups check if ANY admin in group, hasRoute and hasAccess to create if yes merge group into groups
i think it may be done with filters or with multiple filters, but in general you just need some "any?" method/function that evaluates until it has found an item where the expression returns trueish
Given your first snippet, filter
filter can not be used since the condition is based on a variable (display_group
) updated inside the loop.
yeah thats what i thought there, but display group is only used as flag so the check isn't called more than once because it isn't needed. (twig has no break)
thats why it would need an any filter twig doesn't has yet
my example if any would exist:
{% set groups = [] %}
{% for group in sonata_admin.adminPool.dashboardgroups %}
{% if group.items | any( admin => admin.hasRoute('create') and admin.hasAccess('create')) %}
{% set groups = [group]|merge(groups) %}
{% endif %}
{% endfor %}
it might even work as one-liner
{% set groups = sonata_admin.adminPool.dashboardgroups | filter(group =>group.items | any(admin => admin.hasRoute('create') and admin.hasAccess('create'))) %}
It will require a custom filter. Since this deprecation, Twig doesn't provide an alternative solution to avoid the iteration over a condition which is already reached inside the loop. Maybe you could create an RFC for such filter.
@phansys already did make an request for a Filter/Test function for any
@OskarStark Closed by #5576 ?
Yes, I think @Hanmac. Thank you!
Environment
Sonata packages
Symfony packages
PHP version
Subject
Twig 2.10 does deprecate "if" condition on "for" tag https://twig.symfony.com/doc/2.x/tags/for.html#adding-a-condition
Steps to reproduce
Open Sonata admin area and check the deprecated warnings
Expected results
No Warnings
Actual results
For example this one:
what would be the correct thing if the "if" condition on "for" tag can't be used anymore?