sonata-project / SonataAdminBundle

The missing Symfony Admin Generator
https://docs.sonata-project.org/projects/SonataAdminBundle
MIT License
2.11k stars 1.26k forks source link

Deprecated Twig warnings: Nesting a block definition under a non-capturing node #5362

Closed Hanmac closed 4 years ago

Hanmac commented 5 years ago

Environment

Sonata packages

$ composer show --latest 'sonata-project/*'

sonata-project/admin-bundle              3.43.0             = 3.43.0
sonata-project/block-bundle              3.13.0             = 3.13.0
sonata-project/cache                     2.0.1              = 2.0.1
sonata-project/core-bundle               3.13.4             = 3.13.4
sonata-project/datagrid-bundle           2.4.0              = 2.4.0
sonata-project/doctrine-extensions       1.1.3              = 1.1.3
sonata-project/doctrine-orm-admin-bundle 3.6.3              = 3.6.3
sonata-project/easy-extends-bundle       2.5.0              = 2.5.0
sonata-project/exporter                  1.10.0             = 1.10.0
sonata-project/user-bundle               dev-master cdf296c = dev-master cdf296c

Symfony packages

$ composer show --latest 'symfony/*'
symfony/monolog-bundle     v3.3.1  = v3.3.1
symfony/phpunit-bridge     v3.4.20 ~ v4.2.1
symfony/polyfill-apcu      v1.10.0 = v1.10.0
symfony/polyfill-ctype     v1.10.0 = v1.10.0
symfony/polyfill-intl-icu  v1.10.0 = v1.10.0
symfony/polyfill-mbstring  v1.10.0 = v1.10.0
symfony/polyfill-php56     v1.10.0 = v1.10.0
symfony/polyfill-php70     v1.10.0 = v1.10.0
symfony/polyfill-util      v1.10.0 = v1.10.0
symfony/security-acl       v3.0.1  = v3.0.1
symfony/swiftmailer-bundle v2.6.1  ~ v3.2.4
symfony/symfony            v3.4.20 ~ v4.2.1

Twig packages

$ composer show --latest "twig/*"
twig/extensions v1.5.4 = v1.5.4
twig/twig       v2.6.0 = v2.6.0

PHP version

$ php -v
PHP 7.1.23 (cli) (built: Oct 10 2018 00:37:01) ( ZTS MSVC14 (Visual C++ 2015) x86 )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
    with Xdebug v2.6.1, Copyright (c) 2002-2018, by Derick Rethans

Subject

Twig since 2.5 shows Deprecated message that some features in the CRUD template will cause a Syntax Error in next Twig version.

Steps to reproduce

Twig Changelog:

* 2.5.0 (2018-07-13)

* deprecated using the spaceless tag at the root level of a child template (noop anyway)
* deprecated the possibility to define a block in a non-capturing block in a child template

Actual results

php.INFO: User Deprecated: Nesting a block definition under a non-capturing node in "@SonataAdmin/CRUD/list_choice.html.twig" at line 22 is deprecated since version 2.5.0 and will become a syntax error in 3.0. {"exception":"[object] (ErrorException(code: 0): User Deprecated: Nesting a block definition under a non-capturing node in \"@SonataAdmin/CRUD/list_choice.html.twig\" at line 22 is deprecated since version 2.5.0 and will become a syntax error in 3.0. at \\vendor\\twig\\twig\\lib\\Twig\\Parser.php:348)"} []
OskarStark commented 5 years ago

Thank you for submitting this 👍🏻

Can you provide a PR?

Hanmac commented 5 years ago

I don't know what the right thing would be, does the twig message mean it needs an outer block?

OskarStark commented 5 years ago

Can you try this and change if the deprecation is gone? list_choice.html.twig

{% block field %}
- {% spaceless %}
    {% if field_description.options.choices is defined %}
        {% if field_description.options.multiple is defined and field_description.options.multiple==true and value is iterable %}

            {% set result = '' %}
            {% set delimiter = field_description.options.delimiter|default(', ') %}

            {% for val in value %}
                {% if result is not empty %}
                    {% set result = result ~ delimiter %}
                {% endif %}

                {% if field_description.options.choices[val] is defined %}
                    {% if field_description.options.catalogue is not defined %}
                        {% set result = result ~ field_description.options.choices[val] %}
                    {% else %}
                        {% set result = result ~ field_description.options.choices[val]|trans({}, field_description.options.catalogue) %}
                    {% endif %}
                {% else %}
                    {% set result = result ~ val %}
                {% endif %}
            {% endfor %}

            {% set value = result %}

        {% elseif value in field_description.options.choices|keys %}
            {% if field_description.options.catalogue is not defined %}
                {% set value = field_description.options.choices[value] %}
            {% else %}
                {% set value = field_description.options.choices[value]|trans({}, field_description.options.catalogue) %}
            {% endif %}
        {% endif %}
    {% endif %}

    {{ value }}
- {% endspaceless %}
{% endblock %}
Hanmac commented 5 years ago

@OskarStark i managed to do some testing, spaceless isn't the problem there.

this would fix the warning

diff --git a/src/Resources/views/CRUD/list_choice.html.twig b/src/Resources/views/CRUD/list_choice.html.twig
index 35e9cc15..39e811f7 100644
--- a/src/Resources/views/CRUD/list_choice.html.twig
+++ b/src/Resources/views/CRUD/list_choice.html.twig
@@ -18,14 +18,14 @@ file that was distributed with this source code.
 %}
 {% set x_editable_type = field_description.type|sonata_xeditable_type %}

-{% if is_editable and x_editable_type %}
-    {% block field_span_attributes %}
+{% block field_span_attributes %}
+    {% if is_editable and x_editable_type %}
         {% spaceless %}
             {{ parent() }}
             data-source="{{ field_description|sonata_xeditable_choices|json_encode }}"
         {% endspaceless %}
-    {% endblock %}
-{% endif %}
+    {% endif %}
+{% endblock %}

 {% block field %}
 {% spaceless %}

this is probably true for other CRUD templates

stale[bot] commented 4 years ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.