3

Is there a standard/best practices way to add UI for third party category attributes in Magento 2.1?

In previous versions of Magento 1 and Magento 2, the user interface at

Products -> Categories

would respect the frontend_input values set on the attribute object of the EAV Category object. However, Magento 2.1 seems to have the UI rendering code with a formal ui_component/category_form.xml that ignores (or seems to ignore?) the frontend_input setting of the EAV objects.

Is there a standard "best practice" way for third party developers to add fields for their custom category attributes? Or is this an area where we're left to glop around on our own for a solution?

1 Answer 1

2

The idea is that the new UI components in Magento offer way more flexibility in terms of user experience. Centralizing all the configuration in the category_form.xml ui file simplifies adding new attributes to the Admin Panel.

Adding the new fields to the form isn't any different than adding fields to any other form, there is no distinction between category EAV attributes and other parts of the system. (I imagine they will eventually move away from EAV for categories, but that is just speculation.)

<?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="assign_products">
        <field name="my_attribute">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">40</item>
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="notice" xsi:type="string">Text below the field</item>
                    <item name="label" xsi:type="string" translate="true">My Label</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

I did encounter a small related bug when you're adding new form fields to the categories. https://github.com/magento/magento2/issues/5187

4
  • +1, useful, thanks! The EAV attributes had the ability to render a custom form element using a PHP class. Does the new system offer anything like this? (i.e. a default way via EAV configuration to render a custom field, and/or a way to render a custom field via a ui_component) Jul 20, 2016 at 16:59
  • Yeah, there is, an example of this is github.com/magento/magento2/blob/develop/app/code/Magento/… which outputs the category tree in a custom component. Jul 21, 2016 at 9:57
  • I'm not sure I follow how that's a custom component. The highlighted text looks like it render a standard select element with the KnockoutJS template ui/grid/filters/elements/ui-select -- what am I missing? Jul 21, 2016 at 17:34
  • I too was wondering the same thing. How could I render a form element from PHP class (like 1.x), instead from KnockoutJS template. @AlanStorm did you find a way to do this (the old "frontend_input" method)?
    – Shathish
    May 18, 2017 at 6:49

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.