Search code examples
symfonysonata-admin

Symfony2 - Override Sonata AdminBundle filter


I need to override the filter in Sonata AdminBundle to use a totally different kind of filter using images.

For now it's a html form:

/**
* @param DatagridMapper $datagridMapper
*/
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
   $datagridMapper
       ->add('orderIdentifier', null, ['label' => 'N°'])
       ->add('orderDeliveryAddress', null, ['label' => 'Client'])
       ->add('partner.name', null, ['label' => 'Partenaire'])
       ->add('postal', null, ['label' => 'Code postal'])
       ->add('product.code', null, ['label' => 'Produit'])
       ->add('volume', null, ['label' => 'Volume', 'template'])
       ->add('deliveryType', null, ['label' => 'Type de livraison'])
       ->add('createdAt', null, ['label' => 'Date'])
       ->add('state', null, array('show_filter' => true), 'choice', array(
              'choices' => $this->getConfigurationPool()->getContainer()->get('fm.command.order_status_manager')->countAllOrderByStatus(),
          ))
   ;
}

How can I totally override this method?


Solution

  • I found a way to override the template:

    We override the controller to add my new logic:

    namespace Site\AdminBundle\Controller;
    
    use Sonata\AdminBundle\Controller\CRUDController as Controller;
    
    class CommandManagementController extends Controller
    {
        public function listAction()
        {
            $request = $this->getRequest();
    
            $this->admin->checkAccess('list');
    
            $preResponse = $this->preList($request);
            if ($preResponse !== null) {
                return $preResponse;
            }
    
            if ($listMode = $request->get('_list_mode')) {
                $this->admin->setListMode($listMode);
            }
    
            $datagrid = $this->admin->getDatagrid();
            $formView = $datagrid->getForm()->createView();
    
            // set the theme for the current Admin Form
            $this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());
    
            return $this->render('Admin/Command/list.html.twig', array(
                'action' => 'list',
                'form' => $formView,
                'datagrid' => $datagrid,
                'csrf_token' => $this->getCsrfToken('sonata.batch'),
            ), null, $request);
        }
    }
    

    The twig template:

    {% extends 'SonataAdminBundle:CRUD:base_list.html.twig' %}
    
    {% block list_filters %}
            {# Your HTML here #}
    {% endblock %}