Search code examples
symfonysonata-adminsymfony-sonata

How to process each entity in SonataAdminBundle list view?


How to apply some code to each entity being displayed in Admin's list view?

For example, if I have a TagManager and need to load tags for each entity being displayed, how do I do that? Is there a method to override in entity's Admin or can I bind to some list form event? I could not find a place to do that.

I don't wan't to bind to entity's onLoad event.


Solution

  • EDIT: In your entityAdminController :

    public function listAction()
    {
        if (false === $this->admin->isGranted('LIST')) {
            throw new AccessDeniedException();
        }
    
        $datagrid = $this->admin->getDatagrid();
        $formView = $datagrid->getForm()->createView();
    
        foreach($datagrid->getResults() as $object)
        {
            //do what you want with $object
        }
    
        // set the theme for the current Admin Form
        $this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());
    
        return $this->render($this->admin->getTemplate('list'), array(
            'action'   => 'list',
            'form'     => $formView,
            'datagrid' => $datagrid
        ));
    }