symfony - Adding a custom form inside the show template of a Sonata Admin Entity -
i want generate small form indide sonata admin show template. have done far creating function in custom crud specific entity (order) extends 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(), )); }
in orderadmin have 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() ; }
the order_actions
template looks , show relevant functionality according status of order , logged in, how work many diffent routes?;
<td> {% if app.user.id == object.firstapprover , object.status == 1%} {{ render(controller('appbundle:pmodordercrud:approveorder', { 'id': object.id })) }} {% elseif app.user.id == object.secondapprover , 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 , 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 error;
an exception has been thrown during rendering of template ("there no
_sonata_admin
defined controllerapbundle\controller\pmodordercrudcontroller
, current route ``") in appbundle:pmodorderaction:order_actions.html.twig @ line 3.
i understand documentation need make use of configureroutes
method;
protected function configureroutes(routecollection $collection) { $collection->add('clone', $this->getrouteridparameter().'/clone'); }
but can't work , not sure how render forms instead of simple link button.
can please me fix problem?
the _sonata_admin
(route) attribute used sonataadminbundle
obtain required admin instance ($this->admin
) , able configure/process request:
after add right route definition:
protected function configureroutes(routecollection $collection) { $collection->add('approve_order', $this->getrouteridparameter().'/approve'); }
you need add _sonata_admin
code generate right request approveorderaction()
:
{{ render(controller('qibssfrontendbundle:pmodordercrud:approveorder', { 'id': object.id, '_sonata_admin': '...' })) }}
let's make simple example:
you have order
entity , admin class: orderadmin
purchasebundle
, sonata's service definition orderadmin
class (yaml):
services: purchase_bundle.admin.order_admin: class: purchasebundle\admin\orderadmin arguments: - ~ - purchasebundle\entity\order - ~ tags: - { name: 'sonata.admin', manager_type: orm }
now, based on own approveorderaction()
, can render action in follows way:
{{ render(controller('purchasebundle:orderadmin:approveorder', { 'id': object.id, '_sonata_admin': 'purchase_bundle.admin.order_admin' })) }}
just have add admin code: 'purchase_bundle.admin.order_admin'
, should work!
Comments
Post a Comment