EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
4.09k stars 1.03k forks source link

EasyAdminAutocompleteType V2 to V3 #4194

Closed treeindark closed 3 years ago

treeindark commented 3 years ago

Hello, i upgrade to V3, but i've a problem with autocomplete in collection: in v2 config

- { property: 'instructions', css_class: 'sortable', label: 'Instructions spécials', type: 'collection', type_options: {entry_type: 'App\Form\Admin\InstructionType', by_reference: false}, help: 'Les instructions seront sélectionnables lors de la mise en panier' }
class InstructionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('nom')
            ->add('uniq',null,['label' => 'Choix Unique'])
            ->add('obligatoire',null,['label' => 'Choix Obligatoire'])
            ->add(
                'commentaires',
                EasyAdminAutocompleteType::class,
                array(
                'multiple' =>true,
                //'choice_label' => 'nom',
                'class' => Commentaire::class,
                'label' => 'Commentaires',
                 )
            )

With this i could create instruction with autocomplete in my form.

But in V3, i don't know how to do EasyAdminAutocompleteType don't exist any more

javiereguiluz commented 3 years ago

Your use case is out of the scope of this bundle. We don't provide form types to use them freely in your own Symfony form types. It's technically possible to do so, but we don't if it'd work. In any case, EasyAdminAutocompleteType was renamed to CrudAutocompleteType I think, so maybe you can do something with it. Cheers!

sarim commented 3 years ago

Your use case is out of the scope of this bundle. We don't provide form types to use them freely in your own Symfony form types.

@javiereguiluz Actually it is not out of the scope. Because CollectionField forces you to use a symfony form type as entry type. This forces you out of easyadmin forms and If you want to use CrudAutocompleteType in that symfony type, it doesn't work. I looked through the code and easyadmin has codes to set data-ea-autocomplete-endpoint-url, If I copy over those codes to my symfony form type class, it works. But obviously this is a bad hack, as the code to generate data-ea-autocomplete-endpoint-url might change. Also the significance of the variables used in generating that url is not documented.

This can be improved by:

CrudAutocompleteType should be able to configure itself. It can get AdminUrlGenerator from container, then via options we can pass required information like crud controller, parent crud controller etc... Basically port most of the logic from https://github.com/EasyCorp/EasyAdminBundle/blob/bcc20b301a16655d8aa9ae524c21caf6b18e866d/src/Field/Configurator/AssociationConfigurator.php#L66 to CrudAutocompleteType. Alternatively port that logic to a separate class that AssociationConfigurator calls, this way plain symfony form types (that'll be used in CollectionField::setEntryType) can call that class to configure CrudAutocompleteType too.

Btw Sonata Admin provides its own types for handling collection fields, and it uses associated admin class to make the inner forms, so no custom symfony form is needed. Something like that would be the best solution, but obviously that is a big task.