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

ModelAutocompleteType: Impossible to create custom types #7476

Closed soullivaneuh closed 2 years ago

soullivaneuh commented 3 years ago

Environment

Sonata packages

show

``` $ composer show --latest 'sonata-project/*' sonata-project/admin-bundle 3.102.0 4.0.0 The missing Symfony Admin Generator sonata-project/block-bundle 3.23.0 4.7.0 Symfony SonataBlockBundle sonata-project/cache 2.1.1 2.2.0 Cache library sonata-project/core-bundle 3.20.0 3.20.0 Symfony SonataCoreBundle (abandoned) Package sonata-project/core-bundle is abandoned, you should avoid using it. No replacement was suggested. sonata-project/doctrine-extensions 1.12.0 1.13.1 Doctrine2 behavioral extensions sonata-project/doctrine-orm-admin-bundle 3.33.0 4.0.0 Integrate Doctrine ORM into the SonataAdminBundle sonata-project/exporter 2.6.2 2.7.0 Lightweight Exporter library sonata-project/form-extensions 0.1.2 1.9.0 Symfony form extensions sonata-project/intl-bundle 2.7.0 2.10.2 Symfony SonataIntlBundle sonata-project/twig-extensions 0.1.1 1.7.0 Sonata twig extensions ```

Symfony packages

show

``` $ composer show --latest 'symfony/*' symfony/asset v5.2.4 v5.3.4 Manages URL generation and versioning of web assets such as CSS s... symfony/browser-kit v5.2.0 v5.3.4 Symfony BrowserKit Component symfony/cache v5.2.6 v5.3.7 Provides an extended PSR-6, PSR-16 (and tags) implementation symfony/cache-contracts v2.4.0 v2.4.0 Generic abstractions related to caching symfony/config v4.4.20 v5.3.4 Helps you find, load, combine, autofill and validate configuratio... symfony/console v4.4.21 v5.3.7 Eases the creation of beautiful and testable command line interfaces symfony/contracts v1.1.0 v2.4.0 A set of abstractions extracted out of the Symfony components symfony/css-selector v5.0.8 v5.3.4 Symfony CssSelector Component symfony/debug v4.4.20 v4.4.27 Provides tools to ease debugging PHP code symfony/debug-bundle v4.4.8 v5.3.4 Symfony DebugBundle symfony/dependency-injection v4.4.21 v5.3.7 Allows you to standardize and centralize the way objects are cons... symfony/deprecation-contracts v2.4.0 v2.4.0 A generic function and convention to trigger deprecation notices symfony/doctrine-bridge v4.4.21 v5.3.7 Provides integration for Doctrine with various Symfony components symfony/dom-crawler v5.2.0 v5.3.7 Symfony DomCrawler Component symfony/dotenv v4.4.8 v5.3.7 Registers environment variables from a .env file symfony/error-handler v4.4.21 v5.3.7 Provides tools to manage errors and ease debugging PHP code symfony/event-dispatcher v4.4.20 v5.3.7 Provides tools that allow your application components to communic... symfony/event-dispatcher-contracts v1.1.9 v2.4.0 Generic abstractions related to dispatching event symfony/expression-language v4.4.20 v5.3.7 Provides an engine that can compile and evaluate expressions symfony/filesystem v4.4.21 v5.3.4 Provides basic utilities for the filesystem symfony/finder v5.2.4 v5.3.7 Finds files and directories via an intuitive fluent interface symfony/flex v1.11.0 v1.15.3 Composer plugin for Symfony symfony/form v4.4.21 v5.3.7 Allows to easily create, process and reuse HTML forms symfony/framework-bundle v4.4.21 v5.3.7 Provides a tight integration between Symfony components and the S... symfony/http-client v5.3.2 v5.3.7 Provides powerful methods to fetch HTTP resources synchronously o... symfony/http-client-contracts v2.4.0 v2.4.0 Generic abstractions related to HTTP clients symfony/http-foundation v4.4.20 v5.3.7 Defines an object-oriented layer for the HTTP specification symfony/http-kernel v4.4.21 v5.3.7 Provides a structured process for converting a Request into a Res... symfony/inflector v5.2.6 v5.3.4 Converts words between their singular and plural forms (English o... Package symfony/inflector is abandoned, you should avoid using it. Use use `EnglishInflector` from the String component instead instead. symfony/intl v4.4.20 v5.3.7 Provides a PHP replacement layer for the C intl extension that in... symfony/lock v4.4.8 v5.3.4 Symfony Lock Component symfony/messenger v4.4.19 v5.3.7 Helps applications send and receive messages to/from other applic... symfony/mime v5.2.6 v5.3.7 Allows manipulating MIME messages symfony/monolog-bridge v5.0.8 v5.3.7 Symfony Monolog Bridge symfony/monolog-bundle v3.5.0 v3.7.0 Symfony MonologBundle symfony/options-resolver v4.4.20 v5.3.7 Provides an improved replacement for the array_replace PHP function symfony/orm-pack v2.1.0 v2.1.0 A pack for the Doctrine ORM symfony/phpunit-bridge v4.4.8 v5.3.7 Symfony PHPUnit Bridge symfony/polyfill-ctype v1.22.1 v1.23.0 Symfony polyfill for ctype functions symfony/polyfill-iconv v1.15.0 v1.23.0 Symfony polyfill for the Iconv extension symfony/polyfill-intl-grapheme v1.22.1 v1.23.1 Symfony polyfill for intl's grapheme_* functions symfony/polyfill-intl-icu v1.22.1 v1.23.0 Symfony polyfill for intl's ICU-related data and classes symfony/polyfill-intl-idn v1.22.1 v1.23.0 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions symfony/polyfill-intl-normalizer v1.22.1 v1.23.0 Symfony polyfill for intl's Normalizer class and related functions symfony/polyfill-mbstring v1.22.1 v1.23.1 Symfony polyfill for the Mbstring extension symfony/polyfill-php72 v1.22.1 v1.23.0 Symfony polyfill backporting some PHP 7.2+ features to lower PHP ... symfony/polyfill-php73 v1.22.1 v1.23.0 Symfony polyfill backporting some PHP 7.3+ features to lower PHP ... symfony/polyfill-php80 v1.22.1 v1.23.1 Symfony polyfill backporting some PHP 8.0+ features to lower PHP ... symfony/polyfill-uuid v1.23.0 v1.23.0 Symfony polyfill for uuid functions symfony/process v4.4.8 v5.3.7 Symfony Process Component symfony/property-access v4.4.20 v5.3.7 Provides functions to read and write from/to an object or array u... symfony/property-info v5.2.4 v5.3.7 Extracts information about PHP class' properties using metadata o... symfony/proxy-manager-bridge v4.4.20 v5.3.4 Provides integration for ProxyManager with various Symfony compon... symfony/psr-http-message-bridge v1.3.0 v2.1.1 PSR HTTP message bridge symfony/routing v4.4.20 v5.3.7 Maps an HTTP request to a set of configuration variables symfony/security v4.4.26 v4.4.30 Provides a complete security system for your web application symfony/security-acl v3.1.1 v3.2.0 Symfony Security Component - ACL (Access Control List) symfony/security-bundle v4.4.21 v5.3.7 Provides a tight integration of the Security component into the S... symfony/serializer v4.4.20 v5.3.4 Handles serializing and deserializing data structures, including ... symfony/serializer-pack v1.0.4 v1.0.4 A pack for the Symfony serializer symfony/service-contracts v2.4.0 v2.4.0 Generic abstractions related to writing services symfony/stopwatch v5.2.4 v5.3.4 Provides a way to profile code symfony/string v5.2.6 v5.3.7 Provides an object-oriented API to strings and deals with bytes, ... symfony/swiftmailer-bundle v3.4.0 v3.5.2 Symfony SwiftmailerBundle symfony/templating v4.4.20 v5.3.7 Provides all the tools needed to build any kind of template system symfony/translation v4.4.21 v5.3.7 Provides tools to internationalize your application symfony/translation-contracts v2.4.0 v2.4.0 Generic abstractions related to translation symfony/twig-bridge v4.4.21 v5.3.7 Provides integration for Twig with various Symfony components symfony/twig-bundle v4.4.20 v5.3.4 Provides a tight integration of Twig into the Symfony full-stack ... symfony/validator v4.4.21 v5.3.7 Provides tools to validate values symfony/var-dumper v4.4.21 v5.3.7 Provides mechanisms for walking through any arbitrary PHP variable symfony/var-exporter v5.2.4 v5.3.7 Allows exporting any serializable PHP data structure to plain PHP... symfony/web-link v4.4.21 v5.3.4 Manages links between resources symfony/web-profiler-bundle v4.4.8 v5.3.5 Symfony WebProfilerBundle symfony/webpack-encore-bundle v1.7.3 v1.12.0 Integration with your Symfony app & Webpack Encore! symfony/yaml v4.4.21 v5.3.6 Loads and dumps YAML files ```

PHP version

$ php -v
PHP 7.4.13 (cli) (built: Dec  1 2020 04:25:48) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.13, Copyright (c), by Zend Technologies

Subject

I have a User entity used on many doctrine relation of my project. The related ModelAutocompleteType is a bit complex and used many times on filters and form configuration:

$datagridMapper->add($fieldName, ModelAutocompleteFilter::class, [], [
    'field_options' => [
        'property' => [
            'username',
            'firstname',
            'lastname',
        ],
        'to_string_callback' => static fn (User $entity) => $entity->getFullNameWithUsername(),
    ],
]);
$formMapper
    ->add('customer', ModelAutocompleteType::class, [
        'property' => [
            'username',
            'firstname',
            'lastname',
        ],
        'to_string_callback' => static fn (User $entity) => $entity->getFullNameWithUsername(),
    ])
;

It works, but become a bit noisy. In order to reduce the code base, I created a custom UserModelAutocompleteType with default options:

namespace App\Form\Type\Admin;

use App\Entity\User;
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class UserModelAutocompleteType extends AbstractType
{
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'property' => [
                'username',
                'firstname',
                'lastname',
            ],
            'to_string_callback' => static fn (User $entity) => $entity->getFullNameWithUsername(),
        ]);
    }

    public function getParent()
    {
        return ModelAutocompleteType::class;
    }
}

Then I implemented it on both form fields and datagrid filters:

$datagridMapper->add($fieldName, ModelAutocompleteFilter::class, [], [
    'field_type' => UserModelAutocompleteFilter::class,
]);
$formMapper->add('customer', UserModelAutocompleteFilter::class);

This is the official way to override a form type with Symfony. However, this is not working with sonata with two crashes.

On the filter request submission:

RuntimeException:
To retrieve autocomplete items, you should add filter "" to "App\Admin\UserAdmin" in configureDatagridFilters() method.

  at vendor/php/sonata-project/admin-bundle/src/Action/RetrieveAutocompleteItemsAction.php:179
  at Sonata\AdminBundle\Action\RetrieveAutocompleteItemsAction->__invoke(object(Request))
     (vendor/php/symfony/http-kernel/HttpKernel.php:158)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor/php/symfony/http-kernel/HttpKernel.php:80)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor/php/symfony/http-kernel/Kernel.php:201)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (public/index.php:34)    

On the form render:

TypeError:
Argument 1 passed to Sonata\AdminBundle\Form\DataTransformer\ModelToIdPropertyTransformer::__construct() must implement interface Sonata\AdminBundle\Model\ModelManagerInterface, null given, called in /app/vendor/php/sonata-project/admin-bundle/src/Form/Type/ModelAutocompleteType.php on line 42

  at /app/vendor/php/sonata-project/admin-bundle/src/Form/DataTransformer/ModelToIdPropertyTransformer.php:73
  at Sonata\AdminBundle\Form\DataTransformer\ModelToIdPropertyTransformer->__construct(null, null, array('username', 'firstname', 'lastname'), false, object(Closure))
     (/app/vendor/php/sonata-project/admin-bundle/src/Form/Type/ModelAutocompleteType.php:42)

For the form part, I noticed this is caused by the special AbstractFormContractor that deals only with some form type for options feeding: https://github.com/sonata-project/SonataAdminBundle/blob/bc3871e06cffc7498058bb225a29a43cb74f51ff/src/Builder/AbstractFormContractor.php#L78

Adding my class to the list "solve" the form issue, but not the filter one.

As a quite basic way to build form is not available and was introduce quite lately on v3, may we have a bugfix for this branch?

Regards

dmaicher commented 3 years ago

Can you try upgrading sonata-project/admin-bundle and sonata-project/doctrine-orm-admin-bundle to the latest 3.x releases? We might have fixed those issues (at least the form rendering issue) via https://github.com/sonata-project/SonataAdminBundle/pull/7301 and https://github.com/sonata-project/SonataDoctrineORMAdminBundle/pull/1473

soullivaneuh commented 3 years ago

@dmaicher This indeed fix the form issue part. However, we still have the filter request issue:

RuntimeException:
To retrieve autocomplete items, you MUST add the filter "" to the App\Admin\UserAdmin::configureDatagridFilters() method.

  at vendor/php/sonata-project/admin-bundle/src/Action/RetrieveAutocompleteItemsAction.php:194
  at Sonata\AdminBundle\Action\RetrieveAutocompleteItemsAction->__invoke(object(Request))
     (vendor/php/symfony/http-kernel/HttpKernel.php:158)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor/php/symfony/http-kernel/HttpKernel.php:80)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor/php/symfony/http-kernel/Kernel.php:201)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (public/index.php:34)        
VincentLanglet commented 3 years ago

Related to this: https://github.com/sonata-project/SonataDoctrineORMAdminBundle/blob/4.x/src/Builder/DatagridBuilder.php#L84 ?

soullivaneuh commented 3 years ago

@VincentLanglet Possible. BTW, I have an another concern with the CallbackFilter combination, related to this: https://github.com/sonata-project/SonataAdminBundle/issues/7478

VincentLanglet commented 3 years ago

Related to this: https://github.com/sonata-project/SonataDoctrineORMAdminBundle/blob/4.x/src/Builder/DatagridBuilder.php#L84 ?

Do you have time to try a bug fix by introducing the isAnyInstanceOf method to make the class check and tell us if it fix the issue ?

VincentLanglet commented 2 years ago

Closing since