EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

linkToCrudAction not working

Open w455im opened this issue 3 years ago • 4 comments

Describe the bug linkToCrudAction does not actually link the button to the method supplied in arguments.

To Reproduce

// Controller/Admin/CRUD/ModelCrudController.php
class ModelCrudController {

// ...

public function configureActions(Actions $actions): Actions
{
       $myNewAction= Action::new(
                 'my_new_act',
                 t('action.create_and_add_another', domain: 'EasyAdminBundle')
        )->linkToCrudAction('createNewModel');

        return $actions
                 ->add(Crud::PAGE_NEW, $myNewAction);
}

// ...

public function createNewModel(AdminContext $context) {
        dump($context->getRequest()->request->all());die;
}

// ...

createNewModel is never called when clicking on the corresponding button.

(OPTIONAL) Additional context same problem when updating an existing action, ie

return $actions->update(Crud::PAGE_NEW, Action::SAVE_AND_ADD_ANOTHER, function (Action $action) {
                return $action->linkToCrudAction('createNewModel');
            });

w455im avatar Aug 04 '22 08:08 w455im

Hi,

Because you need to create an action "createNewModel".

linkToCrudAction need an action name, not the method.

dwd-akira avatar Aug 04 '22 12:08 dwd-akira

Hi,

Because you need to create an action "createNewModel".

linkToCrudAction need an action name, not the method.

in the documentation the method name is used. Otherwise, how would I define what function is called when the button is clicked ?

w455im avatar Aug 04 '22 13:08 w455im

I do this and it's working:

public const NEW_ACTION = 'myNewAction';

In configureActions:

Action::new(self::NEW_ACTION , 'Label')
            ->linkToCrudAction(self::NEW_ACTION)

public function myNewAction(...

In you case, i think you can rename my_new_act -> createNewModel

dwd-akira avatar Aug 04 '22 14:08 dwd-akira

in the documentation the method name is used.

I'm agree with you. The doc says:

// this action executes the 'renderInvoice()' method of the current CRUD controller
        $viewInvoice = Action::new('viewInvoice', 'Invoice', 'fa fa-file-invoice')
            ->linkToCrudAction('renderInvoice');

But the definition in Action.php is: public function linkToCrudAction(string $crudActionName): self

I think the comment should be: // this action executes 'viewInvoice()' method of the current CRUD controller, but redirect to 'renderInvoice' action on click (and execute 'renderInvoice()' method)

dwd-akira avatar Aug 04 '22 14:08 dwd-akira