/   /   /  OroCommerce For Developer: Add custom section to Edit Order Page in backend

Note:

For more extensions and themes visit our store

OroCommerce For Developer: Add custom section to Edit Order Page in backend


Oro plaform is flexible system which give ability to customize CRUD page in admin for example you can add a custom section to edit order page and add new form or show custom details .

Let’s start by adding you service :

  1.  
  2.     ibnab_customsection_order.event_listener.order_edit_view_listener:
  3.         class: 'Ibnab\Bundle\CustomSectionBundle\EventListener\OrderEditViewListener'
  4.         arguments:
  5.             - '@translator'
  6.         tags:
  7.             - { name: kernel.event_listener, event: oro_ui.scroll_data.before.order-edit, method: onEdit }
  8.  


Here we handle kernel.event_listener of type oro_ui.scroll_data.before.order-edit to observe and inject of custom section to edit order page by the method  onEdit , 

The content of our class which contain  onEdit function:

  1.  
  2. <?php
  3. namespace Ibnab\Bundle\CustomSectionBundle\EventListener;
  4. use Oro\Bundle\UIBundle\Event\BeforeListRenderEvent;
  5. use Symfony\Contracts\Translation\TranslatorInterface;
  6. use Oro\Bundle\UIBundle\View\ScrollData;
  7. class OrderEditViewListener {
  8.     const CUSTOMSECTION_BLOCK_ID = 'customsection';
  9.     /** @var int */
  10.     const BLOCK_PRIORITY = 100;
  11.     /**
  12.      * @var TranslatorInterface
  13.      */
  14.     protected $translator;
  15.     /**
  16.      * @param TranslatorInterface $translator
  17.      */
  18.     public function __construct(TranslatorInterface $translator) {
  19.         $this->translator = $translator;
  20.     }
  21.     /**
  22.      * @param BeforeListRenderEvent $event
  23.      */
  24.     public function onEdit(BeforeListRenderEvent $event) {
  25.         $entity = $event->getEntity();
  26.         if ($entity != '''') {
  27.             $template = $event->getEnvironment()->render(
  28.                     'IbnabCustomSectionBundle:Order:update_order.html.twig', ['entity' => $entity,'form' => $event->getFormView()]
  29.             );
  30.             $this->addCustomSectionBlock($event->getScrollData(), $template);
  31.         }
  32.     }
  33.     /**
  34.      * @param ScrollData $scrollData
  35.      * @param string $template
  36.      */
  37.     private function addCustomSectionBlock(ScrollData $scrollData, $template) {
  38.         $scrollData->addNamedBlock(self::CUSTOMSECTION_BLOCK_ID, $this->translator->trans('ibnab.customsection.order.sections.customsection.label'), self::BLOCK_PRIORITY);
  39.         $subBlock = $scrollData->addSubBlock(self::CUSTOMSECTION_BLOCK_ID);
  40.         $scrollData->addSubBlockData(self:: CUSTOMSECTION_BLOCK_ID $subBlock, $template, self::CUSTOMSECTION_BLOCK_ID);
  41.     }
  42. }


You can explore  onEdit function which receive as param $event of type  Oro\Bundle\UIBundle\Event\BeforeListRenderEvent which give you ability for example to get current entity by $event->getEntity();  or form by $event->getForm();  so in this section we will render a twig  IbnabCustomSectionBundle:Order:update_order.html.twig and pass to variables $entity and $form, and we finish by call internal method of class  addCustomSectionBlock to add block section with method addNamedBlock of object ScrollData $scrollData , the I of block is  self::CUSTOMSECTION_BLOCK_ID and has a label for tab , after that we add a subBlock = content of block with same id and the result of twig $template .

let’s add our twig template IbnabCustomSectionBundle:Order:update_order.html.twig the full path is Ibnab/Bundle/CustomSectionBundle/Resources/views/Order/ update_order.html.twig an for example the content is :

  1.  
  2. {{ form_row(form.paymentDate) }}
  3.  

for example you custom field  paymentDate whicch get added by migration and from and you can manage save value by form extension 

Comments

IBNAB is a company made of a group of professionals whose work is providing secure open source solutions. Our company strives for reaching magnificent results with each experience and provides professional open source solutions that cover every part of the business process.