/   /   /   /  OroCommerce B2B : show hide actions in datagrid by permissions

Note:

For more extensions and themes visit our store

OroCommerce B2B : show hide actions in datagrid by permissions


By default, oro platform show all actions which get added to your data gird . If you need to hide or show some options depending on the data of the entry, you can manage by adding action_configuration which using a service that decides whether or not an action is visible for an entry:

  1. </p>
  2.  
  3. <p>datagrids:
  4.     ibnab-acbundle-orders-grid:
  5.        # … code 
  6.         properties:
  7.             id: ~
  8.             enable_link:
  9.                 type:       url
  10.                 route:      ibnab_customer_checkout_status
  11.                 params:     [ id ]
  12.             disable_link:
  13.                 type:       url
  14.                 route:      ibnab_customer_checkout_status
  15.                 params:     [ id ]
  16.         actions:
  17.             enable:
  18.                 type:         ajax
  19.                 label:        ibnab.acbundle.frontend.enable
  20.                 acl_resource: oro_order_frontend_view
  21.                 icon:         check
  22.                 link:         enable_link
  23.             disable:
  24.                 type:         ajax
  25.                 label:        ibnab.acbundle.frontend.disable
  26.                 acl_resource: oro_order_frontend_view
  27.                 icon:         close
  28.                 link:         disable_link
  29.         action_configuration: ['@ibnab_app_order.action.visibility_provider', getActionsVisibility]
  30.  

@ibnab_app_order.action.visibility_provider is a public service , the function  getActionsVisibility contain your statement responsible to show or hide some actions . Full code of the class :

  1.  
  2. <?php
  3. namespace Ibnab\Bundle\ReplenishmentOrderBundle\Datagrid;
  4. use Oro\Bundle\DataGridBundle\Datasource\ResultRecordInterface;
  5. class ActionsVisibilityProvider
  6. {
  7.     /**
  8.      * @param ResultRecordInterface $record
  9.      * @param array $actions
  10.      * @return array
  11.      */
  12.     public function getActionsVisibility(ResultRecordInterface $record, array $actions)
  13.     {
  14.         $actions = array_keys($actions);
  15.         $visibility = [];
  16.         foreach ($actions as $action) {
  17.             $visibility[$action] = true;
  18.         }       
  19.             if (array_key_exists('enable', $visibility)) {
  20.                 $visibility['enable'] = $record->getValue('status') == 0 ? true : false;
  21.             }
  22.             if (array_key_exists('disable', $visibility)) {
  23.                 $visibility['disable'] = $record->getValue('status') == 1 ? true : false;
  24.             }        
  25.         return $visibility;
  26.     }
  27. }
  28.  

Firstly we have started by foreach which affect true to all actions stocked on $visibility table :

  1.  
  2.         foreach ($actions as $action) {
  3.             $visibility[$action] = true;
  4.         } 
  5.  

After that we test if some actions has exist like - enable  Or disable - and related to value of status field of current line of datagrid we show or hide those actions:
  1.  
  2.             if (array_key_exists('enable', $visibility)) {
  3.                 $visibility['enable'] = $record->getValue('status') == 0 ? true : false;
  4.             }
  5.             if (array_key_exists('disable', $visibility)) {
  6.                 $visibility['disable'] = $record->getValue('status') == 1 ? true : false;
  7.             }  
  8.  

And finally we return our table by  return $visibility .

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.