0

I have a data patch which installs the custom customer attribute (of type "text"). It is EAV attribute, I specify 'is_used_in_grid' => 1 and life's good: in the admin I navigate to Admin -> Customers -> All Customers and I see that attribute at the end of the column (depends on the position (sort_order) really). Entity type code, in this case, is 'customer' (Customer:ENTITY constant can be referenced).

The problem is (described below) when I want to add a customer ADDRESS attribute (of type "select"). I've gone through so many articles, pages, videos, documentation and still couldn't find an answer to my problem. So I have another data patch where I create my custom customer ADDRESS attribute, there is no constants to reference (apart from Metadata class), the entity type code for this case is 'customer_address'. So I have my script:

public function apply()
{
    /** @var CustomerSetup $customerSetup */
    $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
    $attributeName = $this->getAttributeName();
    $entityTypeCode = self::CUSTOMER_ADDRESS_ENTITY;
    try {
        $customerSetup->addAttribute($entityTypeCode,
            $attributeName,
            [
                'is_used_in_grid' => 1,
                'is_visible_in_grid' => 1,
                'label' => 'Neighbourhood Desirability',
                'unique' => 0,
                'required' => 0,
                'position' => 1040,
                'system' => 0,
                'type' => 'int',
                'input' => 'select',
                'source' => NeighbourhoodDesirability::class,
            ]
        );
    } catch (Exception $e) {
        $this->logger->error($e->getMessage());
    }

    try {
        $entity = $customerSetup->getEavConfig()->getEntityType($entityTypeCode);
        $attributeSetId = $entity->getDefaultAttributeSetId();
        /** @var $attributeSet AttributeSet */
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
        $attribute = $customerSetup->getEavConfig()->getAttribute($entityTypeCode, $attributeName);
        $attribute->addData([
            'used_in_forms' => $this->getFormIds(),
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
        ]);
        $this->attributeResource->save($attribute);
    } catch (Exception $e) {
        $this->logger->error($e->getMessage());
    }

    return $this;
}

/**
 * @return string
 */
public function getAttributeName()
{
    return 'neighbourhood_desirability';
}

/**
 * @return string[]
 */
public function getFormIds()
{
    return [
        'adminhtml_customer_address',
    ];
}

It works, it creates the attribute. And no, I don't need to create any extension attributes or anything like that, it just works; I see the attribute in the admin customer address form, I can choose the option from my source model and I can save it. Then I can view the saved value in the form again, all good!

But what is not good is that I can't see this attribute in the customer ADDRESS grid. I can see this column in the actual Customer grid, and the value is there. But I am super confused: I have specified the attribute type code to be "customer_address", not "customer". So why on earth this field is displayed in the Customer grid instead of the Customer Address grid? When we specify "is_used_in_grid" attribute (with value 1 or true) which grid is assumed? Isn't it suppose to be defined by the entity type code? For products it is a product grid, for customers it is a customer grid. So why on earth when I want it to be displayed for customer address grid, it displays it in the customer grid instead of the address one?

This is only the first part of the issue. The second one is that if I create a custom customer_address_listing.xml file and add a new column it finally becomes visible from the customer address table.

<?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="neighbourhood_desirability" component="Magento_Ui/js/grid/columns/select" sortOrder="95">
            <settings>
                <options class="KodeKonstrukt\CustomAttributesExample\Model\Customer\Address\Attribute\Source\NeighbourhoodDesirability"/>
                <filter>select</filter>
                <dataType>select</dataType>
                <label translate="true">Neighbourhood Desirability</label>
            </settings>
        </column>
    </columns>
</listing>

...but the value is freakin empty. I did some debugging and that attribute value is not even loaded in the data provider. By the way the original data provider is set in the customer_address_listing.xml sitting under the vendor/magento/module-customer/view/adminhtml/ui_component directory:

<dataProvider class="Magento\Customer\Ui\Component\Listing\Address\DataProvider" name="customer_address_listing_data_source">
    <settings>
        <requestFieldName>id</requestFieldName>
        <primaryFieldName>entity_id</primaryFieldName>
    </settings>
</dataProvider>

The EAV attribute is not there. How come? Why this problem doesn't appear in customer grid attributes? How can I make sure the attribute is loaded without the need to add and configure any indexer.xml files? It does work for customer grid without any fuss, so wondering is there a way to be able to have that same structure for the customer address customisation.

Any help?

Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.