Magento 2 – Force Custom Grid View for Specific Admin User or Role

admingridmagento2order-grid

We have a special admin user account that should not see certain columns in the admin panel order grid. Let's assume we want to hide the "Bill to name" and "Ship to name" from that specific admin user.

Any recommendations on how that could be achieved?

Best Answer

Add in your module:

sales_order_grid.xml view/adminhtml/ui_component/

<?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">
    <columns name="sales_order_columns">
        <column name="billing_name" class="VendorName\ModuleName\Ui\Component\Listing\Column\BillTo">
            <settings>
                <filter>text</filter>
                <label translate="true">Bill-to Name</label>
            </settings>
        </column>
    </columns>
</listing>

Now add, BillTo.php in Ui/Component/Listing/Column/BillTo.php

<?php

namespace VendorName\ModuleName\Ui\Component\Listing\Column;

use Magento\Framework\Exception\LocalizedException;
use Magento\Ui\Component\Listing\Columns\Column;


class BillTo extends Column
{
    public function prepare()
    {
        if( //Login role or name is restricted column, here you can verify loggedin admin name or role with condition whatever your need is ) {
            $this->_data['config']['componentDisabled'] = true;
        }
        parent::prepare();
    }
}
Related Topic