0

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...!!!

1 Answer 1

3

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 = [];
}
3
  • It's working for me.thanks...!!! Commented Aug 2, 2019 at 6:38
  • Great.. Worked for me. Thanks.. Commented Oct 18, 2022 at 10:10
  • which file is needed to modify the export _button Commented May 8, 2023 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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