在 Sonata 管理实体的显示模板中添加自定义表单 [英] Adding a custom form inside the show template of a Sonata Admin Entity
问题描述
我想在 Sonata Admin 演出模板中生成一个小表格.到目前为止,我所做的是在自定义 CRUD 中为从 Sonata 的默认 CRUD 扩展的特定实体(订单)创建函数;
I want to generate a small form inside a Sonata Admin show template. What I have done so far is creating the function in the custom CRUD for that specific entity (order) that extends from Sonata's default CRUD;
public function approveOrderAction($id = null)
{
$request = $this->getRequest();
$id = $request->get($this->admin->getIdParameter());
$order = $this->admin->getObject($id);
$approveForm = $this->createFormBuilder($order)
->add('reqSecondApprover', 'checkbox', array('label' => 'Require second Approval', 'required' => false))
->add('secondApprover', 'choice', array('choices' => Crud::getWhatever(array('Developer')), 'required' => false))
->getForm();
$approveForm->handleRequest($request);
if ($approveForm->isSubmitted() && $approveForm->isValid()) {
$secondApproval = $request->request->get('form');
$approval = $approveForm->getData();
if (isset($secondApproval['reqSecondApprover'])) {
$order->setStatus(PmodOrder::STATUS_PARTLY_APPROVED);
} else {
$order->setStatus(PmodOrder::STATUS_APPROVED);
$order->setSecondApprover(null);
}
$em->persist($approval);
$em->flush();
return new RedirectResponse($this->admin->generateUrl('show'));
}
return $this->render('AppBundle:PmodOrder:order_approve.html.twig', array(
'order' => $order,
'form' => $approveForm->createView(),
));
}
在我的 orderAdmin 中,我有 configShowFields
方法;
In my orderAdmin I have the configShowFields
method;
protected function configureShowFields(ShowMapper $showMapper)
{
$order = $this->getSubject();
$showMapper
->with('General')
->add('createdBy', null, array('label' => 'Requested By'))
->add('createdAt', null, array('label' => 'Date Requested'))
->with('Order Details')
->add('orderRows', NULL, array('template' => 'AppBundle:PmodOrderRow:orderrow_overview.html.twig'))
->end()
->with('Actions')
->add('actions', NULL, array('template' => 'AppBundle:PmodOrderAction:order_actions.html.twig', 'route' => 'approve'))
->end()
;
}
order_actions
模板是这样的,会根据订单的状态和登录的人显示相关的功能,那么这么多不同的路由怎么工作?;
The order_actions
template looks like this and will show the relevant functionality according to the status of the order and who is logged in, thus how do work with so many diffent routes?;
<td>
{% if app.user.id == object.firstApprover and object.status == 1%}
{{ render(controller('AppBundle:PmodOrderCRUD:approveOrder', { 'id': object.id })) }}
{% elseif app.user.id == object.secondApprover and object.status == 2 %}
<a href="{{ path('order_second_approve', { 'id': object.id })}}" class="btn btn-primary"><i class="fa fa-check"></i> Approve</a>
<a href="{{ path('order_disapprove', { 'id': object.id })}}" class="btn btn-default"><i class="fa fa-times"></i> Disapprove</a>
{% elseif app.user == object.createdBy and object.status == 3 %}
<a href="{{ path('order_place', { 'id': object.id })}}" class="btn btn-primary">Place Order</a>
<a href="{{ path('order_place', { 'id': object.id })}}" class="btn btn-default">Cancel Order</a>
{% else %}
-
{% endif %}
</td>
尝试此操作时出现错误;
When trying this I get an error;
模板渲染过程中抛出异常(没有为控制器定义 _sonata_admin
ApBundle\Controller\PmodOrderCRUDController
和当前路线 ``") 在AppBundle:PmodOrderAction:order_actions.html.twig 在第 3 行.
An exception has been thrown during the rendering of a template ("There is no
_sonata_admin
defined for the controllerApBundle\Controller\PmodOrderCRUDController
and the current route ``") in AppBundle:PmodOrderAction:order_actions.html.twig at line 3.
我从文档中了解到我需要使用这个 configureRoutes
方法;
I understand from the documentation that I need to make use of this configureRoutes
method;
protected function configureRoutes(RouteCollection $collection)
{
$collection->add('clone', $this->getRouterIdParameter().'/clone');
}
但我无法让它工作,我不确定如何呈现表单而不是简单的链接按钮.
But I can't get it to work and I am not sure about how to render forms instead of a simple link button.
有人可以帮我解决我的问题吗?
Can somebody please help me fix my problem?
推荐答案
_sonata_admin
(路由)属性被 SonataAdminBundle
用于获取所需的管理实例($this->admin
) 并能够配置/处理您的请求:
The _sonata_admin
(route) attribute is used by SonataAdminBundle
to get the required admin instance ($this->admin
) and be able to configure/process your requests:
添加正确的路由定义后:
After to add the right route definition:
protected function configureRoutes(RouteCollection $collection)
{
$collection->add('approve_order', $this->getRouterIdParameter().'/approve');
}
您还需要添加 _sonata_admin
代码以生成对 approveOrderAction()
的正确请求:
You need to add also the _sonata_admin
code to generate the right request to approveOrderAction()
:
{{ render(controller('QiBssFrontendBundle:PmodOrderCRUD:approveOrder', { 'id': object.id, '_sonata_admin': '...' })) }}
举个简单的例子:
您有一个 Order
实体及其管理类:OrderAdmin
到 PurchaseBundle
,所以这是 OrderAdmin 的 Sonata 服务定义
类(Yaml):
You have an Order
entity and its admin class: OrderAdmin
into PurchaseBundle
, so this is the Sonata's service definition for OrderAdmin
class (Yaml):
services:
purchase_bundle.admin.order_admin:
class: PurchaseBundle\Admin\OrderAdmin
arguments:
- ~
- PurchaseBundle\Entity\Order
- ~
tags:
- { name: 'sonata.admin', manager_type: orm }
现在,基于您自己的approveOrderAction()
,您可以通过以下方式呈现此操作:
Now, based on your own approveOrderAction()
, you can render this action in the follows way:
{{ render(controller('PurchaseBundle:OrderAdmin:approveOrder', { 'id': object.id, '_sonata_admin': 'purchase_bundle.admin.order_admin' })) }}
只需添加管理员代码:'purchase_bundle.admin.order_admin'
就可以了!
Just you have to add the admin code: 'purchase_bundle.admin.order_admin'
and it should work!
这篇关于在 Sonata 管理实体的显示模板中添加自定义表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!