1

I have to add two fields to the customer's address grid, for this I have to add a left join to the Magento collection. I have created the following custom module:

Vendor/Module/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

Vendor/Module/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

Vendor/Module/view/adminhtml/ui_component/customer_address_listing.xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <columns name="customer_address_columns">
        <column name="first_field">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">11</item>
                    <item name="label" xsi:type="string" translate="true">First Field</item>
                </item>
            </argument>
        </column>
        <column name="second_field">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">12</item>
                    <item name="label" xsi:type="string" translate="true">Second Field</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

Vendor/Module/etc/adminhtml/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\Model\ResourceModel\Address\Grid\Collection">
        <plugin name="customer_address_grid_collection" type="\Vendor\Module\Model\Plugin\ResourceModel\Address\Collection"/>
    </type>
</config>

Vendor/Module/Model/Plugin/ResourceModel/Address/Collection.php

<?php
namespace Vendor\Module\Model\Plugin\ResourceModel\Address; 
 
class Collection
{
 
    public static $table = 'customer_address_entity';
    public static $leftJoinTable = 'customer_address_entity_varchar'; 

 
    public function beforeLoad($intercepter, $collection)
    {        
        if ($collection->getMainTable() === $collection->getConnection()->getTableName(self::$table)) {
 
            $leftJoinTableName = $collection->getConnection()->getTableName(self::$leftJoinTable);
 
            $collection 
                ->getSelect()
                ->joinLeft(
                    ['first_field_table'=>$leftJoinTableName],
                    'main_table.entity_id = first_field_table.entity_id and first_field_table.attribute_id=188',
                    [
                        'first_field'=>'first_field_table.value'
                    ]
                )->joinLeft(
                    ['second_field_table'=>$leftJoinTableName],
                    'main_table.entity_id = second_field_table.entity_id and second_field_table.attribute_id=189',
                    [
                        'second_field'=>'second_field_table.value'                   
                    ]
                );
 
            $where = $collection->getSelect()->getPart(\Magento\Framework\DB\Select::WHERE);
 
            $collection->getSelect()->setPart(\Magento\Framework\DB\Select::WHERE, $where)->group('main_table.entity_id');
 
            echo $collection->getSelect()->__toString();die;
        }
        return $collection;
    }
}

The columns appear in the grid but do not include any fields. I have put a logger in my Collection.php class but it does not run, I have also tried removing a semicolon from this class so that it breaks but nothing happens, therefore it is not going through the class at any time.

If anyone can help me I would really appreciate it. I am using Magento version 2.3.5-p1

1 Answer 1

1

I don't see anything wrong with your module's di.xml. The plugin gets loaded just fine for me on 2.3.4. Is your module properly enabled?

php bin/magento module:enable Vendor_Module

And did you clean cache/generated after enabling?

After making sure that is done. Your beforeLoad should also be changed. This is the function signature from Magento\Framework\Data\Collection\AbstractDb:

public function load($printQuery = false, $logQuery = false)

So your beforeLoad should return these parameters instead of $collection:

public function beforeLoad($collection, $printQuery = false, $logQuery = false)
    {        
        
        if (...) {
            $collection->getSelect()->joinLeft(...)
            ...Etc
        }

        return [$printQuery, $logQuery];
    }

If your plugin is still not loaded after enabling the module and clearing generated, then there might be another module interfering or overriding Magento\Customer\Model\ResourceModel\Address\Grid\Collection.

1
  • Works! The error was in the parameters of the function and the return, I have modified it as you say in your answer and it already works perfectly. Thank you very much.
    – Noty
    Commented Apr 16, 2021 at 23:57

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.