Some grids doesn't load in Magento2 admin

by samurai_code   Last Updated May 16, 2019 03:09 AM

I added a Customer Group column in Customer grid during order creation For Magento2, Then I filtered this grid base on two specific Customer Group.

Customer grid is working fine but module affected on some other grids of admin. For example "Customer Groups" grid in Customer menu doesn't load. Also "Themes" grid and "Configuration" grid in "Content" menu are not loading.

Layout file is

[vendor]\[module]\view\adminhtml\layout\sales_order_create_customer_block.xml

Like this

<?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.customer.grid.container">
            <arguments>
                <argument name="dataSource" xsi:type="object">\[vendor]\[module]\Model\ResourceModel\Order\Customer\Collection</argument>
            </arguments>
        </referenceBlock>
        <referenceBlock name="adminhtml.customer.grid.columnSet">
            <block class="Magento\Backend\Block\Widget\Grid\Column" as="group" after="name">
                <arguments>
                    <argument name="header" xsi:type="string" translate="true">Group</argument>
                    <argument name="index" xsi:type="string">customer_group</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

dataSource class is

\[vendor]\[module]\Model\ResourceModel\Order\Customer\Collection.php

Like this:

<?php
namespace \[vendor]\[module]\Model\ResourceModel\Order\Customer;
class Collection extends \Magento\Sales\Model\ResourceModel\Order\Customer\Collection
{
    protected function _initSelect()
    {
        parent::_initSelect();
        $this->joinField(
            'customer_group',
            'customer_group',
            'customer_group_code',
            'customer_group_id=group_id',
            null,
            'left'
        );
        return $this;
    }
}

di.xml

<?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\Backend\Block\Widget\Grid" type="\[vendor]\[module]\Block\Adminhtml\Custom\Grid" />  
</config>

and this class is for filtering:

Grid.php

<?php
namespace [vendor]\[module]\Block\Adminhtml\Custom;
use Magento\Backend\Block\Widget\Grid as WidgetGrid;
class Grid extends WidgetGrid
    {
         protected function _construct()
         {
             parent::_construct();
         }


         protected function _prepareCollection()
         {
             if(!$this->getParam($this->getVarNameFilter(), null)) {
                  $this->getCollection()->addFieldToFilter('customer_group', array('in' => ['retailer','wholesale']));
              }
              parent::_prepareCollection();
         }
    }


Related Questions


Updated May 14, 2019 00:09 AM

Updated November 22, 2016 08:03 AM

Updated October 11, 2017 08:09 AM

Updated December 09, 2016 08:03 AM

Updated January 04, 2019 09:09 AM