SonataAdminBundle icon indicating copy to clipboard operation
SonataAdminBundle copied to clipboard

[RFC] Use EntityType instead of ModelListType and ModelType

Open core23 opened this issue 3 years ago • 5 comments

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,
                ]],
            ],
        ]);
    }
}

core23 avatar Aug 29 '20 17:08 core23

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.

wbloszyk avatar Aug 30 '20 10:08 wbloszyk

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.

core23 avatar Sep 03 '20 06:09 core23

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.

github-actions[bot] avatar Mar 02 '21 09:03 github-actions[bot]

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 ?

VincentLanglet avatar Jan 31 '22 20:01 VincentLanglet

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.

core23 avatar Feb 12 '22 21:02 core23

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.

github-actions[bot] avatar Aug 12 '22 09:08 github-actions[bot]