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

Docs improvement how to add filters to tab menu #2607

Closed webdevilopers closed 9 years ago

webdevilopers commented 9 years ago

Since the tab menu is part of the list action it should be mentioned in the docs that you can use the configureTabMenu method for adding filters. For instance:

use Sonata\AdminBundle\Admin\Admin;
use Knp\Menu\ItemInterface as MenuItemInterface;
use Sonata\CoreBundle\Form\Type\EqualType;

class FooAdmin extends Admin
{
    protected function configureTabMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
    {
        if (!$childAdmin && !in_array($action, array('edit', 'show', 'list'))) {
            return;
        }

        if ($action == 'list') {
            $filterParameters = $this->getFilterParameters();

            // Add or override filter parameters
            $filterParameters['bar'] = array(
                        'type'  => EqualType::TYPE_IS_EQUAL, // equal to
                        'value' => 'baz'
                    );

            // Add filters to uri of tab
            $menu->addChild('Show Baz only', array('uri' => $this->generateUrl('list', array(
                    'filter' => $filterParameters
                ))));

            return;
        }
    }
}

Should I create a PR for this section? http://sonata-project.org/bundles/admin/master/doc/reference/advanced.html#dropdowns-in-tab-menu

It could also be added to the custom CRUDController chapters:

Example usage:

return new RedirectResponse($this->admin->generateUrl('list', array('filter' => $this->admin->getFilterParameters())));

Source: http://russische-musik.vs137150.vserver.de/v/Sonata/AdminBundle/Controller/CRUDController:addFlash

Related to:

pulzarraider commented 9 years ago

This will be nice tip in the docs.

webdevilopers commented 9 years ago

Done, see:

I left out the DoctrineORMAdmin chapter http://sonata-project.org/bundles/doctrine-orm-admin/master/doc/tutorial/creating_your_first_admin_class/defining_crud_controller.html. since it doesn't feature a lot of code atm anyway.