1

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>
  • Same issue here. Did you manage it in the end? – George May 22 '17 at 16:20
1

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>
  • I've been pulled off the project for now because we have dropped Magento2 for the time being. I don't feel right accepting the solution without testing it, however I upvoted, and thanks for the answer. It sounds valid. – CarComp May 22 '17 at 17:31
0

Had the same issue. Did it this way in the end:

http://undefined.gr/site/2017/05/22/magento-2-1-creating-a-new-customer-attribute-and-adding-it-to-the-customers-grid/

Been looking around for this but couldn’t find a complete solution on this so I am sharing my findings.

I wanted to add an external reference code to the customer entity, form and grid, so…

indexer.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Indexer/etc/indexer.xsd">
    <indexer id="customer_grid" class="Magento\Framework\Indexer\Action\Entity" primary="customer">
        <fieldset name="customer" source="Magento\Customer\Model\ResourceModel\Customer\Collection" provider="Magento\Customer\Model\Indexer\AttributeProvider">
            <field name="customer_ref_code" xsi:type="filterable" dataType="varchar"/>
        </fieldset>
    </indexer>
</config>

customer_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_columns" class="Magento\Customer\Ui\Component\Listing\Columns">
        <column name="ref_code">
            <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">Ref Code</item>
                    <item name="sortOrder" xsi:type="number">5</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

customer_form.xml

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">

    <fieldset name="customer">
            <field name="ref_code">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="sortOrder" xsi:type="number">0</item>
                        <item name="dataType" xsi:type="string">string</item>
                        <item name="formElement" xsi:type="string">input</item>
                        <item name="label" xsi:type="string" translate="true">Ref Code</item>
                        <item name="disabled" xsi:type="boolean">true</item>
                        <item name="dataScope" xsi:type="string">ref_code</item>
                        <item name="source" xsi:type="string">customer_form.customer_form_data_source.customer</item>
                    </item>
                </argument>
            </field> 
    </fieldset>
</form>

InstallData.php

...

private function addCustomerField($customerSetup, $label, $code, $position = 1000)
    {
        $customerSetup->addAttribute(Customer::ENTITY, $code, [
                'type' => 'varchar',
                'label' => $label,
                'input' => 'text',
                'required' => false,
                'visible' => true,
                'user_defined' => true,
                'sort_order' => $position,
                'position' => $position,
                'system' => 0
            ]
        );

        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();

        /** @var $attributeSet AttributeSet */
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, $code)
            ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'is_used_in_grid' => true,
                'is_filterable_in_grid' => true,
                'is_searchable_in_grid' => true,
                'used_in_forms' => [ "adminhtml_customer" ]
            ]);

        $attribute->save();
    }

One thing is that I noticed that for some reason, during my various attempts, the fields is_used_in_grid, is_visible_in_grid, is_filterable_in_grid and is_searchable_in_grid where not set correctly so I had to go in there and manually set the to “1”. Then the field was ready to go on the edit form and on the grid.

Had to run indexer:reindex customer_grid to get the grid filled properly (if you use the flat catalog model).

The only thing I haven’t figured out yet is how to set put the new attribute in a different tab/group (fieldset) and keep it’s data. Seems to be ignoring my attempts.

  • 1
    links expire over time, please add the gist of it or key components to your answer if possible. – NathanielR Dec 22 '17 at 7:26
  • Yeah George it'd be great if you had any examples of what that page used to show – John May 23 '18 at 9:21
  • Apologies the site was down. Updated. Will try to add code here too – George May 24 '18 at 13:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.