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

[RFC] Use EntityType instead of ModelListType and ModelType #6347

Closed core23 closed 2 years ago

core23 commented 4 years ago

Feature Request

This is just an idea I had when looking at an old issue that is still present.

There is a problem if you use custom blocks that rely on an entity. You have to use the ModelListType and define a field description and some other technical stuff. This is kind of okay, if you want to use the block editing function only inside the sonata page admin. But if you want to edit the same block in some non-sonata page (e.g. user frontend page), you can't reuse the block.

To resolve this problem, we could use the EntityType which is a symfony standard component that renders a list of all entity values. We could then define some FormTypeExtension that adds all functionality to this type (if you are inside admin context).

Here's an example code if you want to use the EntityType inside an admin class.

class ContactAdmin extends AbstractAdmin
{
    // ...

    protected function configureFormFields(FormMapper $formMapper): void
    {
        $formMapper
            ->add('name', TextareaType::class)
            ->add('user', EntityType::class, [
                'required' => false,
            ]);
    }
}

And an other example for a block. Depending on where you render this block edit form, you should see a normal entity type list or an entity type picker (if you are inside an admin).

class ContactBlockService extends AbstractBlockService implements EditableBlockService
{
    // ...

    public function configureEditForm(FormMapper $form, BlockInterface $block): void
    {
        $form->add('settings', ImmutableArrayType::class, [
            'keys' => [
                ['title', TextType::class, [
                    'required' => false,
                    'label'    => 'form.label_title',
                ]],
                ['user', EntityType::class, [
                    'label'    => 'form.label_user',
                    'class' => User::class,
                    'required' => false,
                ]],
            ],
        ]);
    }
}
wbloszyk commented 4 years ago

Inject admin to Block will not be working?

IMO we should move all SonataFormType to form-extensions. I know this will be very hard things to do. Some of it require Admin but should be change for something new like CRUDInterface which will be part of admin on the feature. This will allow use sonata awesome form without admin(people will be able to easy configure own CRUD for ModelType). For me it should be milestone 5.

core23 commented 4 years ago

Inject admin to Block will not be working?

That's the current solution for most blocks (e.g. https://github.com/sonata-project/SonataMediaBundle/blob/3.x/src/Block/MediaBlockService.php#L144), but it looks hacky. This will add a hard admin dependency to the block, also if you want to use the block for the non-admin user in the frontend.

IMO we should move all SonataFormType to form-extensions. I know this will be very hard things to do. Some of it require Admin but should be change for something new like CRUDInterface which will be part of admin on the feature.

This will not solve the problem, because you only use the block inside the admin context, not the frontend user context.

github-actions[bot] commented 3 years ago

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

VincentLanglet commented 2 years ago

There is a problem if you use custom blocks that rely on an entity. You have to use the ModelListType and define a field description and some other technical stuff.

I don't fully understand the issue since I personally already use the EntityType in the admin.

        $form
            ->add('contactReason', EntityType::class, [
                'class'        => ContactReason::class,
                'choice_label' => function (ContactReason $contactReason): ?string {
                    return $contactReason->getReason();
                },
                'group_by'     => function (ContactReason $contactReason): ?string {
                    $reasonFamily = $contactReason->getReasonFamily();

                    return null !== $reasonFamily ? $this->getTranslator()->trans($reasonFamily) : null;
                },
                'expanded'     => false,
                'required'     => false,
            ]);

Where are you force to use those types ?

core23 commented 2 years ago

I don't fully understand the issue since I personally already use the EntityType in the admin.

Yes, you can use the EntityType for a block, but you can't create a new entity or use advanced filtering, when displaying the this block in an admin form (e.g. using the PageBundle).

I'd like to have the ModelListType functionality for an EditableBlockService without coupling the block to the admin bundle.

github-actions[bot] commented 2 years ago

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