0

I have created 2 category attributes. They are appearing in my eav_attribute table. But I can't see them in the category admin form. I'm under magento 2.4.

Attribute creation

$eavSetup->addAttribute(
    Category::ENTITY,
    'svg',
    [
        'type'         => 'varchar',
        'label'        => 'Category svg',
        'input'        => 'textarea',
        'sort_order'   => 100,
        'visible'      => true,
        'required'     => false,
        'user_defined' => true,
        'default'      => null,
        'global' => ScopedAttributeInterface::SCOPE_STORE,
        'group' => 'Content'

    ]
);
$eavSetup->addAttribute(
    Category::ENTITY,
    'color',
    [
        'type'         => 'varchar',
        'label'        => 'Background color',
        'input'        => 'text',
        'sort_order'   => 101,
        'visible'      => true,
        'required'     => false,
        'user_defined' => true,
        'default'      => null,
        'global' => ScopedAttributeInterface::SCOPE_STORE,
        'group' => 'Content'
    ]
);

I thought doing that was enough, but digging a little bit I saw we also had to write a custom view/adminhtml/ui_component/category_form.xml

category form xml

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="content">
        <field name="svg">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">100</item>
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">textarea</item>
                    <item name="label" xsi:type="string" translate="true">SVG</item>
                    <item name="required" xsi:type="boolean">false</item>
                </item>
            </argument>
        </field>
        <field name="color">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">101</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">color</item>
                    <item name="required" xsi:type="boolean">false</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

Thanks for your assistance.

4
  • You code is correct and should show in category form.could you please share full file path ?I just use your code and can see 2 new fields in form
    – Pawan
    Jan 14, 2021 at 14:50
  • app/code/Joy/Category/view/adminhtml/ui_component/category_form.xml where Joy/Category is a new module but I don't think there is something wrong in the path
    – Claims
    Jan 14, 2021 at 14:57
  • file path is also correct, just make sure you module is installed...
    – Pawan
    Jan 14, 2021 at 15:00
  • it is otherwise it would not have create the attributes
    – Claims
    Jan 14, 2021 at 15:05

1 Answer 1

0

I had an issue before using 'type' => 'varchar', instead of 'varchar' use 'text'

Something like:

$eavSetup->addAttribute(
    Category::ENTITY,
    'svg',
    [
        'type'         => 'text',
        'length'       => 255,
        'label'        => 'Category svg',
        'input'        => 'textarea',
        'sort_order'   => 100,
        'visible'      => true,
        'required'     => false,
        'user_defined' => true,
        'default'      => null,
        'global' => ScopedAttributeInterface::SCOPE_STORE,
        'group' => 'Content'

    ]
);

$eavSetup->addAttribute(
    Category::ENTITY,
    'color',
    [
        'type'         => 'text',
        'length'       => 255,
        'label'        => 'Background color',
        'input'        => 'text',
        'sort_order'   => 101,
        'visible'      => true,
        'required'     => false,
        'user_defined' => true,
        'default'      => null,
        'global' => ScopedAttributeInterface::SCOPE_STORE,
        'group' => 'Content'
    ]
);

and try creating a custom fieldset in category_form.xml

<fieldset name="custom_fieldset">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Custom Fieldset</item>
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="sortOrder" xsi:type="number">300</item>
            </item>
        </argument>
        <field name="svg">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="label" xsi:type="string" translate="true">svg</item>
                    <item name="prefer" xsi:type="string">toggle</item>
                </item>
            </argument>
        </field>

        <field name="color">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="label" xsi:type="string" translate="true">color</item>
                    <item name="prefer" xsi:type="string">toggle</item>
                </item>
            </argument>
        </field>
</fieldset>

If that's not enough there's the declaration of the setup, that's how i've done it inside the upgrade:

$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
            $eavSetup->addAttribute(
                \Magento\Catalog\Model\Category::ENTITY,
                'color',
                [
                    'type' => 'text',
                    'length' => 255,
                    'label' => 'Color',
                    'input' => 'text',
                    'source' => '',
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                    'visible' => true,
                    'required' => false,
                    'user_defined' => false,
                    'default' => '',
                    'group' => ''
                ]
            );

The last thing that could possibly resolve is inside module.xml (Vendor_Module/etc/module.xml) to add the sequence (this way it will have to load Magento_Catalog first, and then your customization)

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.2.5">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>
8
  • Same issue with your solution sadly :(
    – Claims
    Jan 14, 2021 at 15:14
  • let's see if that's a problem with the content fieldset, could you please try creating a custom fieldset? I've edited my answer Jan 14, 2021 at 15:22
  • Still no change with your product form. Almost like it was totally ignored.
    – Claims
    Jan 14, 2021 at 15:29
  • the last thing i can try is the module.xml, i've edited the last part of the answer Jan 14, 2021 at 15:34
  • Already have it ^^ ` <sequence> <module name="Magento_Store" /> <module name="Magento_Catalog" /> </sequence>`
    – Claims
    Jan 14, 2021 at 15:36

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.