Add custom column filter in customer grid collection in Magento 2


STEP 1
Create a di.xml file inside the app/code/Vendor/Module/etc/ directory. And add the below-mentioned code in this file.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
    <plugin name="sales_order_pending" type="JJ\Salesrep\Plugin\CustomSalesOrderGridCollection" sortOrder="100" />
<<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Customer\Model\ResourceModel\Grid\Collection" type="Vendor\Module\Model\ResourceModel\CustomerGrid\Collection"/>
</config>

STEP 2

Create Collection.php file inside the app/code/Vendor/Module/Model/ResourceModel/CustomerGrid/ directory.

<?php
namespace Vendor\Module\Model\ResourceModel\CustomerGrid;
use Magento\Customer\Model\ResourceModel\Customer;
use Magento\Customer\Ui\Component\DataProvider\Document;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult;
use Psr\Log\LoggerInterface as Logger;
/**
 * Customer Grid Collection Class
 */
class Collection extends \Magento\Customer\Model\ResourceModel\Grid\Collection
{
    public function __construct(
        EntityFactory $entityFactory,
        Logger $logger,
        FetchStrategy $fetchStrategy,
        EventManager $eventManager,
        ResolverInterface $localeResolver,
        \Magento\Backend\Model\Auth\Session $adminSession,
        $mainTable = 'customer_grid_flat',
        $resourceModel = Customer::class,
        TimezoneInterface $timeZone = null
    ) {
        $this->adminSession = $adminSession;
        parent::__construct(
            $entityFactory,
            $logger,
            $fetchStrategy,
            $eventManager,
            $localeResolver,
            $mainTable,
            $resourceModel,
            $timeZone
        );
    }
    protected function _initSelect()
    {
        parent::_initSelect();
        if (!is_null($this->adminSession->getUser())) {
            $roleName = $this->adminSession->getUser()->getRole()->getRoleName();
            $userId = $this->adminSession->getUser()->getId();
            if ($userId && $roleName != "Administrator") {
                //added filter for user_id custom column
                $this->getSelect()->where("main_table.user_id=$userId");
            }
        }
        
        return $this;
    }
}

Leave a comment

Your email address will not be published. Required fields are marked *