Question

I want to hide or remove Export button from custom admin grid for all admin user except admin role.

I have added below code to add export button on the grid in my ui component file.

<exportButton name="export_button"/>

Can anyone suggest how can i achieve that?

Thanks in advance...!!!

Was it helpful?

Solution

Try the following way:

<exportButton name="export_button" class="SR\OrderRefundReason\Ui\Component\ExportButton"/>

Now create following class:

app/code/SR/OrderRefundReason/Ui/Component/ExportButton.php

<?php
namespace SR\OrderRefundReason\Ui\Component;

use Magento\Framework\AuthorizationInterface;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\UrlInterface;

class ExportButton extends \Magento\Ui\Component\ExportButton
{
    /**
     * @var AuthorizationInterface
     */
    private $authorization;

    /**
     * ExportButton constructor.
     *
     * @param ContextInterface $context
     * @param UrlInterface $urlBuilder
     * @param AuthorizationInterface $authorization
     * @param array $components
     * @param array $data
     */
    public function __construct(
        ContextInterface $context,
        UrlInterface $urlBuilder,
        AuthorizationInterface $authorization,
        array $components = [],
        array $data = []
    ) {
        parent::__construct($context, $urlBuilder, $components, $data);
        $this->authorization = $authorization;
    }

    /**
     * @return void
     */
    public function prepare()
    {
        parent::prepare();
        $context = $this->getContext();
        $config = $this->getData('config');
        if (isset($config['options'])) {
            $options = [];
            foreach ($config['options'] as $option) {
                $additionalParams = $this->getAdditionalParams($config, $context);
                $option['url'] = $this->urlBuilder->getUrl($option['url'], $additionalParams);
                $options[] = $option;
            }
            $config['options'] = $options;

            if (!$this->authorization->isAllowed('Magento_Cms::save')) {
                $config = [];
            }

            $this->setData('config', $config);
        }
    }
}

Modify the following line with your proper ACL permission here I am use Magento_Cms::save as an example:

if (!$this->authorization->isAllowed('Magento_Cms::save')) {
    $config = [];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top