How to process each entity in SonataAdminBundle li

2019-05-24 08:04发布

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.

1条回答
太酷不给撩
2楼-- · 2019-05-24 08:10

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
    ));
}
查看更多
登录 后发表回答