Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sonata admin bundle create and return to list not working

I'm using the SonataAdminBundle to generate a back office in Symfony 2 and manage some entities. I've added all the properties I wanted in the create form, and Soanta generates a view with 3 buttons : Create, Create and return to list, Create and add another.

Now, regardless of what button I chose to save my form, Sonata redirects me to edit. How can I change this? Why is it that it created the buttons but doesn't do the redirect?

like image 558
alexandra Avatar asked Mar 20 '23 22:03

alexandra


1 Answers

I've got same problem but I found how to fix it.

All you need to do is to override function 'redirectTo' in your model admin controller class (the one extending Sonata\AdminBundle\Controller\CRUDController) to this:

/**
 * redirect the user depend on this choice
 *
 * @param object $object
 *
 * @return Response
 */
protected function redirectTo($object)
{
    $url = false;

    if ($this->get('request')->get('btn_update_and_list') !== null) {
        $url = $this->admin->generateUrl('list');
    }
    if ($this->get('request')->get('btn_create_and_list') !== null) {
        $url = $this->admin->generateUrl('list');
    }

    if ($this->get('request')->get('btn_create_and_create') !== null) {
        $params = array();
        if ($this->admin->hasActiveSubClass()) {
            $params['subclass'] = $this->get('request')->get('subclass');
        }
        $url = $this->admin->generateUrl('create', $params);
    }

    if (!$url) {
        $url = $this->admin->generateObjectUrl('edit', $object);
    }

    return new RedirectResponse($url);
}

You can find this function inside CRUDController inside SonataAdminBundle.

The problem is the buttons' values are empty and empty string is treated as false for if conditions.

I thing other option could be to add value to this buttons but I think this solution is much simpler.

This works for me but I can't say it is perfect solution. :)

like image 81
maveron Avatar answered Apr 27 '23 01:04

maveron