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

Key "template" does not exist when merging _action options #3828

Closed soullivaneuh closed 4 years ago

soullivaneuh commented 8 years ago

Error on 3.x.

To reproduce, setup this tiny AdminExtension class:

final class ListActionsAdminExtension extends AdminExtension
{
    public function configureListFields(ListMapper $listMapper)
    {
        $options = [
            'actions' => [
                'show' => [],
                'edit' => [],
                'delete' => [],
            ],
        ];

        if ($listMapper->has('_action')) {
            $listMapper->get('_action')->mergeOptions($options);
        } else {
            $listMapper->add('_action', null, $options);
        }
    }
}

This class will merge default actions to defined actions if defined, adding them if not.

And this admin class:

class ApiKeyAdmin extends Admin
{
    protected function configureListFields(ListMapper $list)
    {
        $list
            ->add('name')
            ->add('_action', 'actions', [
                'actions'   => [
                    'reset'     => ['template' => 'admin/api_key/list__action_reset.html.twig'],
                ],
            ])
        ;
    }
}

And them define the services:

services:
    admin.extension.list_actions:
        class: AppBundle\Admin\Extension\ListActionsAdminExtension
        tags:
            - { name: sonata.admin.extension, global: true }
    admin.api_key:
        class: AppBundle\Admin\ApiKeyAdmin
        arguments: [ ~, AppBundle\Entity\ApiKey, ~ ]
        tags:
            - { name: sonata.admin, manager_type: orm, group: API Auth, label: Api Key }

Finally, go to your api keys list. You will get this error:

Key "template" does not exist as the array is empty in SonataAdminBundle:CRUD:list__action.html.twig at line 17

It seems internal default template are auto-filled when adding the _action, but is lost if you merge your options.

soullivaneuh commented 8 years ago

Found why: https://github.com/sonata-project/SonataDoctrineORMAdminBundle/blob/3781cfd2b67cc11bab2d705a883a12ca4df15319/Builder/ListBuilder.php#L166-L175

buildActionFieldDescription is called here: https://github.com/sonata-project/SonataAdminBundle/blob/0f08d0d462d718be7a7832fef215a8a6a5589ebb/Datagrid/ListMapper.php#L116

So it does effect only when you call $listMapper->add.

BTW, actions templates mapping should not be done on the doctrine orm at all.

But I don't really know how to handle it.

@sonata-project/contributors Do you have some suggestions?

soullivaneuh commented 8 years ago

Temporary and ugly workaround waiting this issue resolution:

$options = [
    'actions' => [
        'show' => ['template' => 'SonataAdminBundle:CRUD:list__action_show.html.twig'],
        'edit' => ['template' => 'SonataAdminBundle:CRUD:list__action_edit.html.twig'],
        'delete' => ['template' => 'SonataAdminBundle:CRUD:list__action_delete.html.twig'],
    ],
];
greg0ire commented 8 years ago

Several solutions :

stale[bot] commented 4 years ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

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

Deltachaos commented 3 years ago

I have the same issue

VincentLanglet commented 3 years ago

Just don't use mergeOptions, it's not working and this method was removed from Sonata in the v4.