Question

i want to add custom column to customer listing grid. i could add header to grid but there is no data related my custom table. the reason why this occurs is because my Collection.php is not called when customer page loaded. how can i fix it? also does this has relation to customer_eav_attribute?

```app/code/MyCompany/MyModule/

├── Model
│   └── ResourceModel
│       └── Customer
│           └── Grid
│               └── Collection.php
├── etc
│   ├── Adminhtml
│   │   └── di.xml
│   ├── db_schema.xml
│   └── module.xml
├── registration.php
└── view
    └── adminhtml
        └── ui_component
            └── customer_listing.xml
my collection is 
<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace MyCompany\MyModule\Model\ResourceModel\Customer\Grid;

use Magento\Customer\Ui\Component\DataProvider\Document;
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 Psr\Log\LoggerInterface as Logger;

class Collection extends \Magento\Customer\Model\ResourceModel\Grid\Collection
{
    /**
     * @inheritdoc
     */
    protected $document = Document::class;

    /**
     * Initialize dependencies.
     *
     * @param EntityFactory $entityFactory
     * @param Logger $logger
     * @param FetchStrategy $fetchStrategy
     * @param EventManager $eventManager
     * @param string $mainTable
     * @param string $resourceModel
     */
    public function __construct(
        EntityFactory $entityFactory,
        Logger $logger,
        FetchStrategy $fetchStrategy,
        EventManager $eventManager,
        $mainTable = 'customer_grid_flat',
        $resourceModel = '\Magento\Customer\Model\ResourceModel\Customer'
    ) {
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
    }


    protected function _initSelect()
    {
        parent::_initSelect();
   
        $this->getSelect()->join(
            ['secondTable' => $this->getTable('custom_table')],
            'main_table.entity_id = secondTable.entity_id',
            ['want_to_add as custom_column']
        );
    }

    /**
     * Modify for website_id code same in location type and location list
     */
    public function addFieldToFilter($field, $condition = null)
    {
        if ($field === 'custom_column') {
            $field = 'secondTable.want_to_add';
        }

        return parent::addFieldToFilter($field, $condition);
    }
}
etc/Adminhtml/di.xml is 
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
    <arguments>
        <argument name="collections" xsi:type="array">
            <item name="customer_listing_data_source" xsi:type="string">MyCompany\MyModule\Model\ResourceModel\Customer\Grid\Collection</item>
        </argument>
    </arguments>
</type>
Was it helpful?

Solution

It seems the issue is in your folder name it should be etc/adminhtml not etc/Adminhtml

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top