0

Here, I am using layout xml file to render block grid columns, but facing issue to render specific column on condition basis, here I want to show/hide column on basis of user role but not able to do this.

Please help me with this problem, how can I achieve this?

I am sharing my layout xml file code:

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="supplier.order.grid.container">
            <block class="Magento\Backend\Block\Widget\Grid" name="supplier.grid" as="grid">
                <arguments>
                    <argument name="id" xsi:type="string">purchaseOrderGrid</argument>
                    <argument name="dataSource" xsi:type="object">Vendor\Module\Model\ResourceModel\Order\Collection</argument>
                    <argument name="use_ajax" xsi:type="string">1</argument>
                    <argument name="default_sort" xsi:type="string">po_id</argument>
                    <argument name="default_dir" xsi:type="string">desc</argument>
                    <argument name="grid_url" xsi:type="url" path="*/*/orderGrid"/>
                </arguments>
                <block class="Magento\Backend\Block\Widget\Grid\ColumnSet" as="grid.columnSet" name="permission.user.grid.columnSet">
                    <arguments>
                        <argument name="id" xsi:type="string">purchaseOrderGrid</argument>
                        <argument name="rowUrl" xsi:type="array">
                            <item name="generatorClass" xsi:type="string">Magento\Backend\Model\Widget\Grid\Row\UrlGenerator</item>
                            <item name="path" xsi:type="string">*/*/edit</item>
                            <item name="extraParamsTemplate" xsi:type="array">
                                <item name="po_id" xsi:type="string">getId</item>
                            </item>
                        </argument>
                    </arguments>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="po_id">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">ID</argument>
                            <argument name="sortable" xsi:type="string">true</argument>
                            <argument name="index" xsi:type="string">po_id</argument>
                            <argument name="column_css_class" xsi:type="string">col-id</argument>
                            <argument name="header_css_class" xsi:type="string">col-id</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="po_reference">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Reference</argument>
                            <argument name="type" xsi:type="string">text</argument>
                            <argument name="index" xsi:type="string">po_reference</argument>
                            <argument name="escape" xsi:type="string">1</argument>
                            <argument name="column_css_class" xsi:type="string">col-name</argument>
                            <argument name="header_css_class" xsi:type="string">col-name</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="po_sup_id">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Supplier</argument>
                            <argument name="type" xsi:type="string">options</argument>
                            <argument name="options" xsi:type="options" model="Vendor\Module\Model\ResourceModel\Supplier\Collection"/>
                            <argument name="index" xsi:type="string">po_sup_id</argument>
                            <argument name="column_css_class" xsi:type="string">col-status</argument>
                            <argument name="header_css_class" xsi:type="string">col-status</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="po_manager">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Manager</argument>
                            <argument name="type" xsi:type="string">options</argument>
                            <argument name="options" xsi:type="options" model="Vendor\Module\Model\Order\Manager"/>
                            <argument name="index" xsi:type="string">po_manager</argument>
                            <argument name="column_css_class" xsi:type="string">col-status</argument>
                            <argument name="header_css_class" xsi:type="string">col-status</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="po_warehouse_id">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Warehouse</argument>
                            <argument name="type" xsi:type="string">options</argument>
                            <argument name="options" xsi:type="options" model="Vendor\Module\Model\Source\Warehouse"/>
                            <argument name="index" xsi:type="string">po_warehouse_id</argument>
                            <argument name="column_css_class" xsi:type="string">col-status</argument>
                            <argument name="header_css_class" xsi:type="string">col-status</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="po_eta">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">ETA</argument>
                            <argument name="type" xsi:type="string">date</argument>
                            <argument name="index" xsi:type="string">po_eta</argument>
                            <argument name="escape" xsi:type="string">1</argument>
                            <argument name="column_css_class" xsi:type="string">col-name</argument>
                            <argument name="header_css_class" xsi:type="string">col-name</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="po_status">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Status</argument>
                            <argument name="type" xsi:type="string">options</argument>
                            <argument name="options" xsi:type="options" model="Vendor\Module\Model\Order\Status"/>
                            <argument name="index" xsi:type="string">po_status</argument>
                            <argument name="column_css_class" xsi:type="string">col-status</argument>
                            <argument name="header_css_class" xsi:type="string">col-status</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="po_delivery_progress">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Delivery progress</argument>
                            <argument name="type" xsi:type="string">number</argument>
                            <argument name="index" xsi:type="string">po_delivery_progress</argument>
                            <argument name="escape" xsi:type="string">1</argument>
                            <argument name="column_css_class" xsi:type="string">col-name</argument>
                            <argument name="header_css_class" xsi:type="string">col-name</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="po_grandtotal">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Grand total</argument>
                            <argument name="renderer" xsi:type="string">\Vendor\Module\Block\Order\Grid\Renderer\Total</argument>
                            <argument name="index" xsi:type="string">po_grandtotal</argument>
                            <argument name="escape" xsi:type="string">1</argument>
                            <argument name="column_css_class" xsi:type="string">col-name a-right</argument>
                            <argument name="header_css_class" xsi:type="string">col-name</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="po_invoice">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Invoices</argument>
                            <argument name="renderer" xsi:type="string">\Vendor\Module\Block\Order\Grid\Renderer\Invoice</argument>
                            <argument name="filter" xsi:type="string">\Vendor\Module\Block\Order\Grid\Filter\Invoice</argument>
                            <argument name="index" xsi:type="string">po_id</argument>
                            <argument name="sortable" xsi:type="string">false</argument>
                            <argument name="escape" xsi:type="string">1</argument>
                            <argument name="column_css_class" xsi:type="string">col-name a-right</argument>
                            <argument name="header_css_class" xsi:type="string">col-name</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="po_type">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Type</argument>
                            <argument name="type" xsi:type="string">options</argument>
                            <argument name="options" xsi:type="options" model="Vendor\Module\Model\Order\Type"/>
                            <argument name="index" xsi:type="string">po_type</argument>
                            <argument name="column_css_class" xsi:type="string">col-status</argument>
                            <argument name="header_css_class" xsi:type="string">col-status</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="po_shipping_tracking">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Tracking #</argument>
                            <argument name="type" xsi:type="string">text</argument>
                            <argument name="index" xsi:type="string">po_shipping_tracking</argument>
                            <argument name="escape" xsi:type="string">1</argument>
                            <argument name="column_css_class" xsi:type="string">col-name</argument>
                            <argument name="header_css_class" xsi:type="string">col-name</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="po_missing_price">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Missing prices</argument>
                            <argument name="type" xsi:type="string">options</argument>
                            <argument name="options" xsi:type="options" model="Vendor\Module\Model\Source\YesNo"/>
                            <argument name="index" xsi:type="string">po_missing_price</argument>
                            <argument name="column_css_class" xsi:type="string">col-status</argument>
                            <argument name="header_css_class" xsi:type="string">col-status</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="po_verified">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Verified</argument>
                            <argument name="type" xsi:type="string">options</argument>
                            <argument name="options" xsi:type="options" model="Vendor\Module\Model\Source\YesNo"/>
                            <argument name="index" xsi:type="string">po_verified</argument>
                            <argument name="column_css_class" xsi:type="string">col-status</argument>
                            <argument name="header_css_class" xsi:type="string">col-status</argument>
                        </arguments>
                    </block>
                </block>
                <block class="Magento\Backend\Block\Widget\Grid\Massaction" name="adminhtml.indexer.grid.grid.massaction" as="grid.massaction">
                    <arguments>
                        <argument name="massaction_id_field" xsi:type="string">po_id</argument>
                        <argument name="form_field_name" xsi:type="string">entity_ids</argument>
                        <argument name="use_select_all" xsi:type="string">1</argument>
                        <argument name="options" xsi:type="array">
                            <item name="export" xsi:type="array">
                                <item name="label" xsi:type="string" translate="true">Export</item>
                                <item name="url" xsi:type="string">*/*/massExport</item>
                            </item>
                        </argument>
                    </arguments>
                </block>
            </block>

        </referenceBlock>
    </body> </page>

Thanks!

1 Answer 1

0

In your app/code/Test/Testimonial/view/adminhtml/ui_component/testimonial_blog_listing.xml

add in the column tag the class attribute:

 <column name="nameofyourcolumn" class="YourVendor\YourModule\Ui\Component\Listing\Column\MyColumn">

MyColumn class is:

<?php

namespace YourVendor\YourModule\Ui\Component\Listing\Column;

use Magento\Ui\Component\Listing\Columns\Column;

use Magento\Framework\DataObject;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;

class MyColumn extends Column{

    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        \Magento\Backend\Model\Auth\Session $authSession,
        array $components = [],
        array $data = []
    ) {
        if(!in_array(1,$authSession->getUser()->getRoles())){
            $data = [];
        }
        parent::__construct($context, $uiComponentFactory,$components, $data);
    }

}

in the $authSession variable you have all information about the authenticated user. In the if which you can read above, I want that if the authenticated user is not an administrator (1 is the ID of the administrators group for me) don't show the column. To achieve this I put an empty array in $data variable, which contains all data printed in the column, as result I don't see anymore the column.

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.