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

Twig 2.11 causes: "Template "@SonataAdmin/CRUD/base_edit_form.html.twig" cannot be used as a trait." error #5564

Closed IonBazan closed 5 years ago

IonBazan commented 5 years ago

Environment

Sonata packages

$ composer show --latest 'sonata-project/*'
sonata-project/admin-bundle              3.48.3 3.48.3 The missing Symfony Admin Generator
sonata-project/block-bundle              3.15.0 3.15.0 Symfony SonataBlockBundle
sonata-project/cache                     2.0.1  2.0.1  Cache library
sonata-project/core-bundle               3.17.0 3.17.0 Symfony SonataCoreBundle
sonata-project/datagrid-bundle           2.5.0  3.0.0  Symfony SonataDatagridBundle
sonata-project/doctrine-extensions       1.3.0  1.3.0  Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.9.0  3.9.0  Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/exporter                  2.0.1  2.0.1  Lightweight Exporter library

Symfony packages

$ composer show --latest 'symfony/*'
symfony/asset                      v4.3.0  v4.3.0  Symfony Asset Component
symfony/browser-kit                v4.3.0  v4.3.0  Symfony BrowserKit Component
symfony/cache                      v4.3.0  v4.3.0  Symfony Cache component with PSR-6, PSR-16, and tags
symfony/cache-contracts            v1.1.1  v1.1.1  Generic abstractions related to caching
symfony/config                     v4.3.0  v4.3.0  Symfony Config Component
symfony/console                    v4.3.0  v4.3.0  Symfony Console Component
symfony/css-selector               v4.3.0  v4.3.0  Symfony CssSelector Component
symfony/debug                      v4.3.0  v4.3.0  Symfony Debug Component
symfony/debug-bundle               v4.3.0  v4.3.0  Symfony DebugBundle
symfony/debug-pack                 v1.0.7  v1.0.7  A debug pack for Symfony projects
symfony/dependency-injection       v4.3.0  v4.3.0  Symfony DependencyInjection Component
symfony/doctrine-bridge            v4.3.0  v4.3.0  Symfony Doctrine Bridge
symfony/dom-crawler                v4.3.0  v4.3.0  Symfony DomCrawler Component
symfony/dotenv                     v4.3.0  v4.3.0  Registers environment variables from a .env file
symfony/event-dispatcher           v4.3.0  v4.3.0  Symfony EventDispatcher Component
symfony/event-dispatcher-contracts v1.1.1  v1.1.1  Generic abstractions related to dispatching event
symfony/expression-language        v4.3.0  v4.3.0  Symfony ExpressionLanguage Component
symfony/filesystem                 v4.3.0  v4.3.0  Symfony Filesystem Component
symfony/finder                     v4.3.0  v4.3.0  Symfony Finder Component
symfony/flex                       v1.2.5  v1.2.5  Composer plugin for Symfony
symfony/form                       v4.3.0  v4.3.0  Symfony Form Component
symfony/framework-bundle           v4.3.0  v4.3.0  Symfony FrameworkBundle
symfony/http-foundation            v4.3.0  v4.3.0  Symfony HttpFoundation Component
symfony/http-kernel                v4.3.0  v4.3.0  Symfony HttpKernel Component
symfony/inflector                  v4.3.0  v4.3.0  Symfony Inflector Component
symfony/intl                       v4.3.0  v4.3.0  A PHP replacement layer for the C intl extension that includes additional data from the ICU library.
symfony/maker-bundle               v1.11.6 v1.11.6 Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.
symfony/mime                       v4.3.0  v4.3.0  A library to manipulate MIME messages
symfony/monolog-bridge             v4.3.0  v4.3.0  Symfony Monolog Bridge
symfony/monolog-bundle             v3.3.1  v3.3.1  Symfony MonologBundle
symfony/options-resolver           v4.3.0  v4.3.0  Symfony OptionsResolver Component
symfony/panther                    v0.3.0  v0.3.0  A browser testing and web scraping library for PHP and Symfony.
symfony/phpunit-bridge             v4.3.0  v4.3.0  Symfony PHPUnit Bridge
symfony/polyfill-intl-icu          v1.11.0 v1.11.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-intl-idn          v1.11.0 v1.11.0 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-mbstring          v1.11.0 v1.11.0 Symfony polyfill for the Mbstring extension
symfony/polyfill-php73             v1.11.0 v1.11.0 Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions
symfony/process                    v4.3.0  v4.3.0  Symfony Process Component
symfony/profiler-pack              v1.0.4  v1.0.4  A pack for the Symfony web profiler
symfony/property-access            v4.3.0  v4.3.0  Symfony PropertyAccess Component
symfony/property-info              v4.3.0  v4.3.0  Symfony Property Info Component
symfony/routing                    v4.3.0  v4.3.0  Symfony Routing Component
symfony/security-acl               v3.0.2  v3.0.2  Symfony Security Component - ACL (Access Control List)
symfony/security-bundle            v4.3.0  v4.3.0  Symfony SecurityBundle
symfony/security-core              v4.3.0  v4.3.0  Symfony Security Component - Core Library
symfony/security-csrf              v4.3.0  v4.3.0  Symfony Security Component - CSRF Library
symfony/security-guard             v4.3.0  v4.3.0  Symfony Security Component - Guard
symfony/security-http              v4.3.0  v4.3.0  Symfony Security Component - HTTP Integration
symfony/service-contracts          v1.1.2  v1.1.2  Generic abstractions related to writing services
symfony/stopwatch                  v4.3.0  v4.3.0  Symfony Stopwatch Component
symfony/templating                 v4.3.0  v4.3.0  Symfony Templating Component
symfony/test-pack                  v1.0.5  v1.0.5  A pack for functional and end-to-end testing within a Symfony app
symfony/translation                v4.3.0  v4.3.0  Symfony Translation Component
symfony/translation-contracts      v1.1.2  v1.1.2  Generic abstractions related to translation
symfony/twig-bridge                v4.3.0  v4.3.0  Symfony Twig Bridge
symfony/twig-bundle                v4.3.0  v4.3.0  Symfony TwigBundle
symfony/validator                  v4.3.0  v4.3.0  Symfony Validator Component
symfony/var-dumper                 v4.3.0  v4.3.0  Symfony mechanism for exploring and dumping PHP variables
symfony/var-exporter               v4.3.0  v4.3.0  A blend of var_export() + serialize() to turn any serializable data structure to plain PHP code
symfony/web-profiler-bundle        v4.3.0  v4.3.0  Symfony WebProfilerBundle
symfony/yaml                       v4.3.0  v4.3.0  Symfony Yaml Component

PHP version

$ php -v
PHP 7.3.2 (cli) (built: Feb  9 2019 02:11:33) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.2, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.2, Copyright (c) 1999-2018, by Zend Technologies
    with Xdebug v2.7.2, Copyright (c) 2002-2019, by Derick Rethans

Subject

Steps to reproduce

$ composer require twig/twig 2.11.0

Navigate to edit path of any of your Models.

Expected results

Edit form is rendered.

Actual results

Twig\Error\RuntimeError:
Template "@SonataAdmin/CRUD/base_edit_form.html.twig" cannot be used as a trait.

  at vendor/sonata-project/admin-bundle/src/Resources/views/CRUD/base_edit.html.twig:38

This was working perfectly on v2.10.0.

kricha commented 5 years ago

Same error

valkars commented 5 years ago

Because of this https://github.com/twigphp/Twig/blob/2.x/doc/tags/macro.rst#macros-scoping I have errors: Variable "form_helper" does not exist in vendor/sonata-project/admin-bundle/src/Resources/views/CRUD/base_edit_form.html.twig (line 72).

kunicmarko20 commented 5 years ago

This looks like a bc break in twig then?

fabpot commented 5 years ago

Scoping is wider than before, so this should not be a problem. Can anyone share a small project that exhibit this issue so that I can investigate it more?

ggabrovski commented 5 years ago

Small project: https://github.com/ggabrovski/sonata-admin-twig-2.11

Configure your DB. To create the database and add sample data you can load the fixtures php bin/console doctrine:database:create php bin/console doctrine:migrations:migrate php bin/console doctrine:fixtures:load

URL to reproduce the error: https://127.0.0.1:8000/admin/app/book/1/edit

fabpot commented 5 years ago

The patch is to import the macros in the sonata_tab_content:

            {% block sonata_tab_content %}
                    {% import "@SonataAdmin/CRUD/base_edit_form_macro.html.twig" as form_helper %}

You're hitting an edge case that worked by chance (and was deprecated in Twig 1).

greg0ire commented 5 years ago

Thanks @fabpot ! @IonBazan , can you please make a PR with these changes?

fnowacki commented 5 years ago

Here you are 🙂 https://github.com/sonata-project/SonataAdminBundle/pull/5566

dqblanco commented 5 years ago

Thanks!!!

ghost commented 5 years ago

Still having "Template "@SonataAdmin/CRUD/base_edit_form.html.twig" cannot be used as a trait."

Sonata packages

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

sonata-project/admin-bundle              3.49.0 3.49.0 The missing Symfony Admin Generator
sonata-project/block-bundle              3.15.0 3.15.0 Symfony SonataBlockBundle
sonata-project/cache                     2.0.1  2.0.1  Cache library
sonata-project/core-bundle               3.17.0 3.17.0 Symfony SonataCoreBundle
sonata-project/datagrid-bundle           2.5.0  3.0.0  Symfony SonataDatagridBundle
sonata-project/doctrine-extensions       1.3.0  1.3.0  Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.9.0  3.9.0  Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/exporter                  2.0.1  2.0.1  Lightweight Exporter library

Symfony packages

composer show --latest 'symfony/*'

Restricting packages listed in "symfony/symfony" to "4.2.*"
symfony/asset                 v4.2.9  v4.2.9  Symfony Asset Component
symfony/cache                 v4.2.9  v4.2.9  Symfony Cache component with PSR-6, PSR-16, and tags
symfony/config                v4.2.9  v4.2.9  Symfony Config Component
symfony/console               v4.2.9  v4.2.9  Symfony Console Component
symfony/contracts             v1.1.2  v1.1.2  A set of abstractions extracted out of the Symfony components
symfony/debug                 v4.2.9  v4.2.9  Symfony Debug Component
symfony/dependency-injection  v4.2.9  v4.2.9  Symfony DependencyInjection Component
symfony/doctrine-bridge       v4.2.9  v4.2.9  Symfony Doctrine Bridge
symfony/dotenv                v4.2.9  v4.2.9  Registers environment variables from a .env file
symfony/event-dispatcher      v4.2.9  v4.2.9  Symfony EventDispatcher Component
symfony/expression-language   v4.2.9  v4.2.9  Symfony ExpressionLanguage Component
symfony/filesystem            v4.2.9  v4.2.9  Symfony Filesystem Component
symfony/finder                v4.2.9  v4.2.9  Symfony Finder Component
symfony/flex                  v1.2.5  v1.2.5  Composer plugin for Symfony
symfony/form                  v4.2.9  v4.2.9  Symfony Form Component
symfony/framework-bundle      v4.2.9  v4.2.9  Symfony FrameworkBundle
symfony/http-foundation       v4.2.9  v4.2.9  Symfony HttpFoundation Component
symfony/http-kernel           v4.2.9  v4.2.9  Symfony HttpKernel Component
symfony/inflector             v4.2.9  v4.2.9  Symfony Inflector Component
symfony/intl                  v4.2.9  v4.2.9  A PHP replacement layer for the C intl extension that includes additional data from the ICU library.
symfony/maker-bundle          v1.11.6 v1.11.6 Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.
symfony/monolog-bridge        v4.2.9  v4.2.9  Symfony Monolog Bridge
symfony/monolog-bundle        v3.3.1  v3.3.1  Symfony MonologBundle
symfony/options-resolver      v4.2.9  v4.2.9  Symfony OptionsResolver Component
symfony/phpunit-bridge        v4.2.9  v4.3.0  Symfony PHPUnit Bridge
symfony/polyfill-intl-icu     v1.11.0 v1.11.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-mbstring     v1.11.0 v1.11.0 Symfony polyfill for the Mbstring extension
symfony/polyfill-php72        v1.11.0 v1.11.0 Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions
symfony/process               v4.2.9  v4.2.9  Symfony Process Component
symfony/profiler-pack         v1.0.4  v1.0.4  A pack for the Symfony web profiler
symfony/property-access       v4.2.9  v4.2.9  Symfony PropertyAccess Component
symfony/property-info         v4.2.9  v4.2.9  Symfony Property Info Component
symfony/routing               v4.2.9  v4.2.9  Symfony Routing Component
symfony/security-acl          v3.0.2  v3.0.2  Symfony Security Component - ACL (Access Control List)
symfony/security-bundle       v4.2.9  v4.2.9  Symfony SecurityBundle
symfony/security-core         v4.2.9  v4.2.9  Symfony Security Component - Core Library
symfony/security-csrf         v4.2.9  v4.2.9  Symfony Security Component - CSRF Library
symfony/security-guard        v4.2.9  v4.2.9  Symfony Security Component - Guard
symfony/security-http         v4.2.9  v4.2.9  Symfony Security Component - HTTP Integration
symfony/serializer            v4.2.9  v4.2.9  Symfony Serializer Component
symfony/stopwatch             v4.2.9  v4.2.9  Symfony Stopwatch Component
symfony/templating            v4.2.9  v4.2.9  Symfony Templating Component
symfony/translation           v4.2.9  v4.2.9  Symfony Translation Component
symfony/twig-bridge           v4.2.9  v4.2.9  Symfony Twig Bridge
symfony/twig-bundle           v4.2.9  v4.2.9  Symfony TwigBundle
symfony/validator             v4.2.9  v4.2.9  Symfony Validator Component
symfony/var-dumper            v4.2.9  v4.2.9  Symfony mechanism for exploring and dumping PHP variables
symfony/var-exporter          v4.2.9  v4.2.9  A blend of var_export() + serialize() to turn any serializable data structure to plain PHP code
symfony/web-link              v4.2.9  v4.2.9  Symfony WebLink Component
symfony/web-profiler-bundle   v4.2.9  v4.2.9  Symfony WebProfilerBundle
symfony/webpack-encore-bundle v1.6.0  v1.6.0  Integration with your Symfony app & Webpack Encore!
symfony/yaml                  v4.2.9  v4.2.9  Symfony Yaml Component

Docker environment

Docker Docker version 19.03.0-beta5, build 4a18bf4 Docker compose docker-compose version 1.24.0, build 0aa59064 Used image wodby/php:7.3-dev-4.11.11 Hosted on Ubuntu MATE 19.04

The weird fact is that my team - using the same composer.json and composer.lock - doesn't have this behaviour; but they're working on Mac machines.

We tried at the same time on our different machines:

Downgrading to twig 2.10.0 works for me as reported here https://github.com/sonata-project/SonataAdminBundle/issues/5568#issuecomment-498299716.

ggabrovski commented 5 years ago

Try to delete manually you cache folder, some of my colleagues which are working on windows have the same problem.

greg0ire commented 5 years ago

Can people still having the bug test #5570 and report back please?

ghost commented 5 years ago

https://github.com/sonata-project/SonataAdminBundle/pull/5570/files works! but ONLY if I manually delete cache directory like @ggabrovski suggested, console cache:clear doesn't to the trick

Simperfit commented 5 years ago

I still have the error :/

greg0ire commented 5 years ago

@Simperfit told me it was now fixed for him (by upgrading Twig, is that right?)

Simperfit commented 5 years ago

Yes that's right !

TeLiXj commented 5 years ago

I have this problem if extend only a portion of the file and don't know how to fix it unless overwrite all the file

{% extends '@!SonataAdmin/CRUD/base_edit_form.html.twig' %}

{% block sonata_form_actions %}
...
{% endblock %}

Sonata packages

$ composer show --latest 'sonata-project/*'
sonata-project/admin-bundle              3.50.0          3.50.0             The missing Symfony Admin Generator
sonata-project/block-bundle              3.15.0          3.15.0             Symfony SonataBlockBundle
sonata-project/cache                     2.0.1           2.0.1              Cache library
sonata-project/core-bundle               3.17.0          3.17.0             Symfony SonataCoreBundle
sonata-project/datagrid-bundle           2.5.0           3.0.0              Symfony SonataDatagridBundle
sonata-project/doctrine-extensions       1.3.0           1.3.0              Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.9.0           3.9.0              Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/easy-extends-bundle       2.5.0           2.5.0              Symfony SonataEasyExtendsBundle
sonata-project/exporter                  2.0.1           2.0.1              Lightweight Exporter library
sonata-project/intl-bundle               2.6.0           2.6.0              Symfony SonataIntlBundle

Symfony packages

$ composer show --latest 'symfony/*'
symfony/contracts             v1.1.0  v1.1.5  A set of abstractions extracted out of the Symfony components
symfony/flex                  v1.2.7  v1.2.7  Composer plugin for Symfony
symfony/http-client           v4.3.1  v4.3.1  Symfony HttpClient component
symfony/http-client-contracts v1.1.5  v1.1.5  Generic abstractions related to HTTP clients
symfony/maker-bundle          v1.11.6 v1.11.6 Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.
symfony/mime                  v4.3.1  v4.3.1  A library to manipulate MIME messages
symfony/monolog-bundle        v3.4.0  v3.4.0  Symfony MonologBundle
symfony/phpunit-bridge        v3.4.28 v4.3.1  Symfony PHPUnit Bridge
symfony/polyfill-apcu         v1.11.0 v1.11.0 Symfony polyfill backporting apcu_* functions to lower PHP versions
symfony/polyfill-ctype        v1.11.0 v1.11.0 Symfony polyfill for ctype functions
symfony/polyfill-iconv        v1.11.0 v1.11.0 Symfony polyfill for the Iconv extension
symfony/polyfill-intl-icu     v1.11.0 v1.11.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-intl-idn     v1.11.0 v1.11.0 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-mbstring     v1.11.0 v1.11.0 Symfony polyfill for the Mbstring extension
symfony/polyfill-php56        v1.11.0 v1.11.0 Symfony polyfill backporting some PHP 5.6+ features to lower PHP versions
symfony/polyfill-php70        v1.11.0 v1.11.0 Symfony polyfill backporting some PHP 7.0+ features to lower PHP versions
symfony/polyfill-php72        v1.11.0 v1.11.0 Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions
symfony/polyfill-php73        v1.11.0 v1.11.0 Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions
symfony/polyfill-util         v1.11.0 v1.11.0 Symfony utilities for portability of PHP codes
symfony/security-acl          v3.0.2  v3.0.2  Symfony Security Component - ACL (Access Control List)
symfony/swiftmailer-bundle    v3.2.8  v3.2.8  Symfony SwiftmailerBundle
symfony/symfony               v3.4.28 v4.3.1  The Symfony PHP framework
symfony/webpack-encore-bundle v1.6.0  v1.6.0  Integration with your Symfony app & Webpack Encore!