2

I want to hide "ADD NEW CUSTOMER" button on customer_grid page for certain admin roles, how to specify Specify ACL's for ui_components..?

enter image description here

3

app/code/SR/MagentoCommunity/view/adminhtml/ui_component/customer_listing.xml


<?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">
    <settings>
        <buttons>
            <button name="add" class="SR\MagentoCommunity\Ui\Customer\AddButton"/>
        </buttons>
    </settings>
</listing>

app/code/SR/MagentoCommunity/Ui/Customer/AddButton.php

Change Magento_Cms::save to your new role


<?php
namespace SR\MagentoCommunity\Ui\Customer;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
use Magento\Framework\AuthorizationInterface;

class AddButton implements ButtonProviderInterface
{
    /**
     * Url Builder
     *
     * @var \Magento\Framework\UrlInterface
     */
    protected $urlBuilder;

    /**
     * @var AuthorizationInterface
     */
    private $authorization;

    /**
     * AddButton constructor.
     *
     * @param \Magento\Backend\Block\Widget\Context $context
     * @param AuthorizationInterface $authorization
     */
    public function __construct(
        \Magento\Backend\Block\Widget\Context $context,
        AuthorizationInterface $authorization
    ) {
        $this->urlBuilder = $context->getUrlBuilder();
        $this->authorization = $authorization;
    }

    /**
     * @return array
     */
    public function getButtonData()
    {
        if (!$this->authorization->isAllowed('Magento_Cms::save')) {
            return [];
        }

        return [
            'label' => __('Add New Customer'),
            'on_click' => sprintf("location.href = '%s';", $this->getUrl('*/*/new')),
            'class' => 'primary',
            'sort_order' => 10
        ];
    }

    /**
     * Generate url by route and parameters
     *
     * @param   string $route
     * @param   array $params
     * @return  string
     */
    public function getUrl($route = '', $params = [])
    {
        return $this->urlBuilder->getUrl($route, $params);
    }
}
  • Magento_Cms ..? – Magento Dev Feb 18 at 13:59
  • You change this Magento_Cms::save to your role id, that I already said. I tested using that in my local. – Sohel Rana Feb 18 at 14:12
  • I don't know what is your role, so create new one acl.xml, and change here – Sohel Rana Feb 18 at 14:13
  • okay, are you able to hide that button for different role other than admin..? – Magento Dev Feb 18 at 14:19
  • yes, why not. Did you try that? – Sohel Rana Feb 18 at 14:21

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.