Hide Columns Controls Button in Magento 2 Admin UI for Specific User Role

magento2PHPuicomponent

I have to hide "Columns" control button for specific user roles in admin UI grid. Looks like it's managed through xml <columnsControls name="columns_controls">. I tried to manage via meta but it seems UI grid not able to manage via PHP meta.

Is there any way to hide & show "Columns" control button in UI admin grid please suggest / guide me.

enter image description here

Thanks for your help.

Best Answer

It's little bit tricky, but i hope this way can help you

app/code/Acme/StackExchange/etc/adminhtml/di.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Ui\Component\Container">
        <plugin name="Acme_StackExchange_Plugin_Ui_Component_Listing_ColumnsControlsPlugin"
                type="Acme\StackExchange\Plugin\Ui\Component\Listing\ColumnsControlsPlugin" sortOrder="10"/>
    </type>
</config>

app/code/Acme/StackExchange/Plugin/Ui/Component/Listing/ColumnsControlsPlugin.php

<?php
declare(strict_types=1);

namespace Acme\StackExchange\Plugin\Ui\Component\Listing;

class ColumnsControlsPlugin
{
    public function beforePrepare(\Magento\Ui\Component\Container $subject): array
    {
        $listingName = $subject->getContext()->getNamespace();
        $component   = $subject->getName();

        // example restrict columns_controls for sales_order_grid
        if ($listingName === 'sales_order_grid' && $component === 'columns_controls') {
            $config = $subject->getConfiguration();
            $config['displayArea'] = false;
            $subject->setData('config', $config);
        }

        return [];
    }
}
Related Topic