credits: gevonden foto accordioniste volendams kostuum j 20 via photopin (license). posted on June 28th, 2016.

Adding Custom Category Attributes to Magento 2.1

Because Categories need Customization as well!

Magento 2.1 was released several days ago, and oh boy ... Much has changed! I mean, just look at the release notes! If you're already working with Magento 2 and you've added some custom attributes to your categories, you might have noticed that your tab with options is no longer there. Simply because all tabs aren't there! It seems that in Magento 2.1, Magento has chosen for an accordeon-approach to render the category tree:

screenshot 1

Now what just happened? Prior before Magento 2.1 you could simply add an attribute to your setup-script like you would have done in Magento 1 and the attribute would show up just fine, no additional work required:

/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create();

        'type'         => 'varchar',
        'label'        => 'Custom Attribute',
        'group'        => 'Example',
        'input'        => 'test',
        'global'       => Attribute::SCOPE_GLOBAL,
        'visible'      => true,
        'required'     => false,
        'user_defined' => false,
        'sort_order'   => 100

Now, above snippet creates a new EAV-attribute for the category entity type. With this code we created an attribute in the database. Now we just need an interface / input field in the backend so our client can set the proper value. How do we do that?


With the Magento 2.1 update, the category form in the adminhtml is also created by XML-configuration, just like many other things in Magento. The file responsible for this is located in  vendor/magento/module-catalog/view/adminhtml/ui_component/category_form.xml . Go ahead, just take a peek at the file and you'll see the XML Layout for all UI components on the category edit-page.

Now, we've created our attribute in the database, now we just need to add it to the XML tree. Because Magento merges all XML-files into one huge layout file, you can add a similar file to your own module. Create a file called category_form.xml in view/adminhtml/ui_component and put the following in it:

<form xmlns:xsi=""
    <fieldset name="example">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Example</item>
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="sortOrder" xsi:type="number">100</item>
        <field name="custom_attribute">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">1</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">Custom Attribute</item>

Flush the cache and there you go:

screenshot 2

Footnote: Magento 2.1 has a small bug where the general fieldset has no sortOrder. I already created an issue for this and my best guess is this will most likely be solved in Magento 2.1.1.

Attributes Magento 2 Categories