In this blog, I’ll explain how to hide mass actions based on some conditions like for some admin roles or users. Here we will see how can we remove the whole mass actions or remove only some of the mass actions.
First you need to provide your own class to be used for the mass action, for this in the ui component xml file add class attribute in the massaction tag as shown below,
<massaction name="listing_massaction" class="Webkul\ModuleName\Ui\Component\MassAction"> ..... </massaction>
Now let’s first see how to remove the whole mass action container,
<?php namespace Webkul\ModuleName\Ui\Component; class MassAction extends \Magento\Ui\Component\MassAction { public function prepare() { parent::prepare(); if ( {{CONDITION}} ) { //this will empty the mass-action's actions $this->setData('config', (array)[]); } } }
Here the {{CONDITION}} represents the condition you want to use for removing the actions, it can be something like check of admin users or admin user role or even check of ACL authorisation.
Now let’s see how can we remove only few of the actions,
<?php namespace Webkul\ModuleName\Ui\Component; class MassAction extends \Magento\Ui\Component\MassAction { public function prepare() { parent::prepare(); if ( {{CONDITION}} ) { $config = $this->getConfiguration(); $notAllowedActions = ['delete']; $allowedActions = []; foreach ($config['actions'] as $action) { if (!in_array($action['type'], $notAllowedActions)) { $allowedActions[] = $action; } } $config['actions'] = $allowedActions; $this->setData('config', (array)$config); } } }
Here we have removed the ‘delete’ action from the mass-action.
Thanks for reading this blog. Feel free to comment if you face any issue.
Happy Coding 👨💻
Be the first to comment.