Magento 2.1 – How to Add Category Attribute to Custom Attribute Group

attributescategoryinstall-scriptmagento-2.1magento2

I try to create a category attribute in a Magento 2 install script (or how do we call these now?)

In Magento 1, I could specify anything as group and the group would be created. In Magento 2 this seems not to be the case anymore, so I created the group explicitly first:

$eavSetup->addAttributeGroup(
    Category::ENTITY,
    $eavSetup->getDefaultAttributeSetId(Category::ENTITY),
    'My group name',
    99
);
$eavSetup->addAttribute(
    Category::ENTITY,
    'my_attribute_code',
    [
        'type'              => 'int',
        'input'             => 'select',
        'source'            => Boolean::class,
        'label'             => 'My attribute',
        'group'             => 'My group name',
        'visible'           => 1,
    ]
);

The attribute and attribute group is created in the database (eav_attribute and eav_attribute_group, as well as the relation in eav_entity_attribute) but I still do not see the attribute in the backend. What am I missing?

Best Answer

Thanks to @SohelRana I found out that you now have to write a bunch of additional XML to make anything appear in the category form. Since the answer linked above does not explain how to add a new group, I'll share my result (for one boolean attribute) here:

view/adminhtml/ui_component/category_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="my_group_name">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string">My group name</item>
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="sortOrder" xsi:type="number">99</item>
            </item>
        </argument>
        <field name="my_attribute_code">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">10</item>
                    <item name="dataType" xsi:type="string">boolean</item>
                    <item name="formElement" xsi:type="string">checkbox</item>
                    <item name="source" xsi:type="string">category</item>
                    <item name="prefer" xsi:type="string">toggle</item>
                    <item name="label" xsi:type="string" translate="true">My Attribute</item>
                    <item name="valueMap" xsi:type="array">
                        <item name="true" xsi:type="string">1</item>
                        <item name="false" xsi:type="string">0</item>
                    </item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">false</item>
                    </item>
                    <item name="default" xsi:type="string">0</item>
                    <item name="scopeLabel" xsi:type="string">[STORE VIEW]</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

The argument element within fieldset is necessary to define the properties of the new group. Anything related to attribute group and the admin forms in the database, i.e. from the install script seems to be ignored.