7

I have different user role for my website. For a particular user role, I want to remove "Delete" from mass action dropdown which displays on customer grid in admin section.

The Delete option is got rendered from

magento\vendor\magento\module-customer\view\adminhtml\ui_component\customer_listing.xml file.

I want to show this Delete option only for user role1 and want to hide for user role2.

How can we do this?

8

This can be done by creating a new class for MassActions:

<?php
namespace YourVendor\YourModule\Ui;

class MassAction extends \Magento\Ui\Component\MassAction
{
    private $authorization;

    public function __construct(
        \Magento\Framework\View\Element\UiComponent\ContextInterface $context,
        \Magento\Framework\AuthorizationInterface $authorization,
        $components,
        array $data
    ) {
        $this->authorization = $authorization;
        parent::__construct($context, $components, $data);
    }

    public function prepare()
    {
        parent::prepare();
        $config = $this->getConfiguration();
        if (!$this->authorization->isAllowed('YourVendor_YourModule::the_acl_youd_like_to_use')) {
            $allowedActions = [];
            foreach ($config['actions'] as $action) {
                if ('delete' != $action['type']) {
                    $allowedActions[] = $action;
                }
            }
            $config['actions'] = $allowedActions;
        }
        $this->setData('config', (array)$config);
    }
}

Add setting it to customer listing with help of app/code/YourVendor/YourModule/view/adminhtml/ui_component/customer_listing.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <listingToolbar name="listing_top">
        <massaction name="listing_massaction" class="YourVendor\YourModule\Ui\MassAction"/>
    </listingToolbar>
</listing>
  • It's worked for me – SCC Sep 4 '17 at 10:13
  • Works in CE 2.3.1 – Garry Jun 16 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.