Magento – How does one add a custom filter to the admin customer grid for a custom attribute in Mage2 CE

admincustom-attributesfiltergridmagento2

I'm working on a module that helps sync our CRM database with Magento2 as needed. One of the fields that syncs over is the ID of the person in the CRM database. This ID is well known as a 'go-to' field in our company to know which person we are talking about. I need to add a filter to the Magento admin UI customer grid for this custom attribute which I have added programatically. I've searched, and I have found no solutions.

I'm searching for a solution which I can add to our module programatically. This needs to work in a CI environment, when the module is added to the latest version of Magento, the filter needs to be automatically added to the options in admin. No manual admin configuration explanations please. I don't think that is possible even, but still…

As I ask this question, I'm going to begin reverse engineering how Magento Core creates the filters. Hopefully, by the time I gain some knowledge on how they are built, I'll be able to implement any solution provided with ease.

EDIT:
I've added the following code to my /view/adminhtml/ui_componenet/customer_listing.xml file in my module. The field appears, and so does the filter, but it is not populated with the value inside the customer object.

<?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_columns" class="Magento\Customer\Ui\Component\Listing\Columns">
        <column name="constit_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Constit ID</item>
                    <item name="sortOrder" xsi:type="number">5</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

Best Answer

Add this line to your <item name="config"> (between filter and label):

<item name="add_field" xsi:type="boolean">true</item>

I had the same issue but for the Product Grid. I'm sure this won't be much different.

Also you misspelled ui_component in your post but I assume that's correct in your module.

If that doesn't work, maybe the grid isn't loading your attribute, like I had with another custom attribute supplier I made on Order. To retrieve and display the attribute on the Order Grid I had to add this code to my module's etc/di.xml:

    <!-- Column supplier in Order Grid-->
    <virtualType name="Magento\Sales\Model\ResourceModel\Order\Grid" type="Magento\Sales\Model\ResourceModel\Grid">
        <arguments>
            <argument name="columns" xsi:type="array">
                <item name="supplier" xsi:type="string">sales_order.supplier</item>
            </argument>
        </arguments>
    </virtualType>
Related Topic