0

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?

2 Answers 2

1

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();
    }
}
3
  • Thanks. This is working as expected. However, this would require us to add a custom class for all columns in the order grid where we want to customize the visibility. As we are looking at 20+ columns where this has to be done, I'm waiting to see if I can find a solution that requires less custom classes.
    – Louis B.
    Aug 10, 2021 at 3:44
  • @LouisB. You can simply create a one common class and apply to all the columns in ui_component file. Aug 10, 2021 at 3:48
  • In the end I found a solution that works even better for my use case (see below). Accepting your answer as it is also working and has lead me to discover my final solution. Thanks again!
    – Louis B.
    Aug 10, 2021 at 5:11
0

In the end I found the following solution which works for me:

  • Created a plugin for Magento\Ui\Component\Listing\Columns\Column
  • In the afterPrepare method, implemented my own logic to determine if the current column should be visible

Here is a code example:

namespace Vendor\Module\Plugin\Ui\Component\Listing\Columns;

class Column
{
    /**
     * @param \Magento\Ui\Component\Listing\Columns\Column $subject
     */
    public function afterPrepare(\Magento\Ui\Component\Listing\Columns\Column $subject)
    {
        $columnName = $subject->getName();
        $gridName = $subject->getData('js_config')['extends'];
        $configColumn = $subject->getData('config');
        $configColumn['componentDisabled'] = $this->isColumnVisible($gridName, $columnName)
        $subject->setData('config', $configColumn);
    }
    ...

Your Answer

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

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