I am trying to add new custom field in admin user's grid but it's not in UI components. They are generating grid with data object in Magento\User\Block\User. Can any one please provide the solution to override the grid
1 Answer
For adding new field in admin user grid you have to create two files as below
1. Company\Module\view\adminhtml\layout\adminhtml_user_grid_block.xml
<?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="adminhtml.user.grid.container">
<referenceBlock name="adminhtml.user.grid">
<referenceBlock name="permission.user.grid.columnSet">
<block class="Magento\Backend\Block\Widget\Grid\Column" as="store_name" after="email">
<arguments>
<argument name="header" xsi:type="string" translate="true">Store Name</argument>
<argument name="type" xsi:type="string">text</argument>
<argument name="renderer" xsi:type="string">Company\Module\Block\Widget\Grid\Column\Renderer\StoreName</argument>
</arguments>
</block>
</referenceBlock>
</referenceBlock>
</referenceBlock>
</body>
</page>
2. Company\Module\Block\Widget\Grid\Column\Renderer\StoreName.php
<?php
namespace Company\Module\Block\Widget\Grid\Column\Renderer;
use Magento\Backend\Block\Context;
use Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer;
use Company\Module\Model\VendorsFactory;
class StoreName extends AbstractRenderer
{
protected $_vendorsFactory;
public function __construct
(
VendorsFactory $vendorsFactory,
Context $context,
array $data = []
)
{
$this->_vendorsFactory = $vendorsFactory;
parent::__construct($context, $data);
}
public function render(\Magento\Framework\DataObject $row)
{
$userId = $row->getUserId();
$vendorInfo = $this->_vendorsFactory->create()->load($userId, 'user_id');
return !empty($vendorInfo) ? $vendorInfo->getStoreTitle() : "";
}
}