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