Question

In version 2.0.x I was able to add another tab to the category edit page in the backend. But since Magento2 moved to UI Components with version 2.1.x it's broken. I can't get my head around it actually that's why i'm asking here.

Do I have to extend Magento\Catalog\Model\Category\DataProvider? Or is there a smarter way?

I want to add another field to the category edit page where I can access the current category product collection.

Can anyone help me out?

Was it helpful?

Solution

Its not as bad as it seems although not super easy either. Take a look at the.

I assume you have a module creating the new field, you need a bit more definition in order to make the field appear.

You need to add category_form.xml under view/adminhtml/ui_component and in here you define the field structure.

You still need the fields you want to use defined in the eav_attribute table, so make sure these are still present.

<?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="new_tab_id">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">New Tab</item>
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="sortOrder" xsi:type="number">50</item>
            </item>
        </argument>
        <field name="new_field">
            <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">string</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="scopeLabel" xsi:type="string" translate="true">[STORE VIEW]</item>
                    <item name="label" xsi:type="string" translate="true">New field</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

OTHER TIPS


With other answers, you can't saved to database, it only show in backend.
Here's how you create and save the field:

app/code/Jajuma/AssetPreload/Setup/InstallData.php

<?php

namespace Jajuma\AssetPreload\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(\Magento\Catalog\Model\Category::ENTITY, 'lcp_image', [
            'type' => 'varchar',
            'label' => 'LCP Image',
            'input' => 'text',
            'visible' => true,
            'required' => false,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE
        ]);

        $eavSetup->addAttribute(\Magento\Catalog\Model\Category::ENTITY, 'get_category_image', [
            'type' => 'int',
            'label' => 'Get Category image',
            'input' => 'boolean',
            'source'   => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
            'visible' => true,
            'default'  => '1',
            'required' => false,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE
        ]);
    }
}

app/code/Jajuma/AssetPreload/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="asset_preload_category_tab" sortOrder="80">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Asset Preload</item>
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="sortOrder" xsi:type="number">50</item>
            </item>
        </argument>
        <field name="lcp_image">
            <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">string</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="scopeLabel" xsi:type="string" translate="true">[STORE VIEW]</item>
                    <item name="label" xsi:type="string" translate="true">LCP Image</item>
                </item>
            </argument>
        </field>
        <field name="get_category_image">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">20</item>
                    <item name="dataType" xsi:type="string">boolean</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="scopeLabel" xsi:type="string" translate="true">[STORE VIEW]</item>
                    <item name="label" xsi:type="string" translate="true">Get Category image</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="default" xsi:type="number">1</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

And the result:
http://i.imgur.com/e7qhJP8.png
http://i.imgur.com/MQJDrYH.png

Hope it's help.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top