2
votes

I have a Sonata admin for an entity with many elements that naturally spans multiple pages in the list view.

What I'd like to do is make it so after editing, or creating a new entity, redirect the user to the appropriate page which displays the edited entity (as opposed to going back to the list's first page which is the default behavior of the sonata admin). The dafult behavior is ok when there are only 1 or 2 pages but when you have tens or even hundreds of pages, navigating back to the correct page becomes quite tedious.

So my question is what is the appropriate way to make this happen?

I'm thinking that it would involve customizing the admin controller for the entity but I'm not sure what the right extension points are. And also, how to utilize the paginator to obtain the correct page to navigate back to.

Another potential hack would be to capture the query parameters state when navigating from the list view to the edit, and then returning the user to the same URL. This won't work correctly for creating new items.

There's also the matter of the state of filters when navigating from the list view (if the user had sorted and/or filtered the list before navigating to the edit page).

2

2 Answers

4
votes

I know I'm late but this can be useful for someone else... Here is the way I've made it, by overriding AdminBundle CRUDController:

<?php
namespace MyProject\AdminBundle\Controller;

use Sonata\AdminBundle\Controller\CRUDController as BaseController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;

class CRUDController extends BaseController
{
    protected function redirectTo($object, Request $request = null)
    {
        $response = parent::redirectTo($object, $request);

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

            $last_list = $this->get('session')->get('last_list');

            if(strstr($last_list['uri'], $url) && !empty($last_list['filters'])) {
                $response = new RedirectResponse($this->admin->generateUrl(
                    'list',
                    array('filter' => $last_list['filters'])
                ));
            }
        }

        return $response;
    }

    public function listAction(Request $request = null)
    {
        $uri_parts = explode('?', $request->getUri(), 2);
        $filters = $this->admin->getFilterParameters();
        $this->get('session')->set('last_list', array('uri' => $uri_parts[0], 'filters' => $filters));

        $response = parent::listAction($request);

        return $response;
    }
}
0
votes

I am having the same problem, I was thinking of passing a variable in the route to the edit page, thus giving you where the request for the edit originated from, then you could redirect to the originating page given the variable.