Reading list Switch to dark mode

    Hide Mass Actions Based On Some Conditions in Magento 2

    Updated 4 March 2024

    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,

    Searching for an experienced
    Magento 2 Company ?
    Find out More
    <?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 👨‍💻

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home