Sonata admin:每批操作的特定batch_confirmation模板

问题描述 投票:1回答:2

今天在工作中,我必须在Sonata项目中创建自定义批处理操作。

没问题,问题是:

我想为我的新批处理操作设置一个特定的batch_confirmation模板,我需要它对其他操作没有影响。

如果没有解决方案,我将覆盖默认的batch_confirmation模板,在我的sonata配置中更改它,并制作许多'if'语句以保持我的其他管理类的基本确认,但我认为这不是更清洁的替代方案。

有人已经有这样的问题吗?

symfony override sonata-admin
2个回答
4
投票

添加自定义批处理操作时,您需要在控制器中执行自定义操作,该操作将释放普通CRUD控制器,请参阅:https://sonata-project.org/bundles/admin/master/doc/reference/batch_actions.html

而不是返回重定向响应,就像您在文档中看到的那样:您可以返回任何模板,例如:

return $this->render(
     'YourBundle:Batch:confirmation.html.twig',
      $templateParams
);

在此确认模板中,您可以添加自定义html。当您想要确认批处理方法时,您需要一个表单,该表单会使用您需要的数据回发到您的自定义操作。

从batch_confirmation.html.twig将表单复制到您自己的确认模板。在原始batch_confirmation模板中,设置隐藏字段以检查用户是否确认:<input type="hidden" name="confirmation" value="ok">

如果用户确认(检查请求对象),请检查自定义操作。然后执行批处理操作。 (您还可以返回第二步模板,以构建向导)。

最后扔一些flash消息

$this->addFlash('sonata_flash_success', 'everything is ok!'); 

并返回列表网址:

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

1
投票

即使它有点晚,但对于遇到相同问题的新手来说,解决方案在configureBatchActions函数中很简单,你可以分析确认模板:

protected function configureBatchActions($actions)
{

        $actions['YourAction'] = [
            'ask_confirmation' => true,
            'template' => 'YourTemplate'
        ];

    return $actions;
}
© www.soinside.com 2019 - 2024. All rights reserved.