2

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?

2
  • Do you want to add a new Tab or a new Field? Because a tab named Products in Category already exists and it's not quite clear what you mean by "access the current category product collection".
    – adjco
    Commented Nov 6, 2016 at 15:09
  • Well, in the code it's called field... but you are right, it's another tab in the end. sorry for the late response, didn't had much time lately. Commented Nov 8, 2016 at 23:52

2 Answers 2

3

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>
4
  • Thanks, Corefix. I'll test it tomorrow, didn't had much time lately, sry. Will report back! Commented Nov 8, 2016 at 23:54
  • Ahh... category_form.xml is a ui_component definition in the module_catalog. And because i've added one to my own module it gets extended?! Good to know :) Commented Nov 9, 2016 at 21:32
  • If we need to save that data from the inserted field. Can we save that in our custom table? If yes then how?
    – Zeeshan
    Commented May 3, 2019 at 15:38
  • How can you display field value on the front-end?
    – Sam
    Commented Jul 14, 2020 at 8:25
0

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:
https://i.sstatic.net/6JULB.png
https://i.sstatic.net/c3YE6.png

Hope it's help.

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.