Search code examples
phpsymfonysymfony2-easyadmin

Symfony EasyAdminBundle - How to add a custom action in entity form?


I am using EasyAdminBundle in Symfony 3.1.9.

I managed to customize actions in lists, as well explained here: https://github.com/javiereguiluz/EasyAdminBundle/blob/master/Resources/doc/tutorials/custom-actions.md

But I didn't found any documentation to add custom entity action in forms.

My goal is to add, near the "Save", "Delete" and "Back to list" buttons, a button which saves current entity and redirect to the current edit form (not return to list as default behavior).

entity form edit actions

Thank you in advance


Solution

  • I've probalby made something dirty but it works.

    I've overwrited the editAction :

    public function editAction()
    {
        $response = parent::editAction();
    
        if ($response instanceof RedirectResponse) {            
    
            $request = Request::createFromGlobals();
            return $this->redirect(urldecode($request->request->get('referer')));           
        }
    
        return $response;
    }
    

    The method $this->getCurrentEntity() were unknown.

    I've also overwrited the edit.html.twig to add another button next to the basic one with jQuery:

    var cloned = $( "button.action-save" );
    var clone = cloned.clone();
    cloned.after(clone);
    clone.addClass('action-save-stay')
    clone.html('<i class="fa fa-save"></i>{{ 'action.save_stay'|trans }}');
    
    $('.action-save-stay').bind('click', function(e) {
       e.preventDefault();
       $('input[name="referer"]').val(window.location.href);
       $('form').submit();
    });
    

    It changes the hidden input named referer. By default, easyadmin redirects to the referer contained in the query string.

    Thank you so much to put me in the right direction.