3

I have created the custom attribute using the solution given here.

Create Custom category Yes/No attribute magento2

The attribute is created and i can see entry in database eav_attribute table.

But when i open the individual category in admin panel. The new custom attribute which is created is not showing up. Please anyone help me is any other setting i need to do?

1
  • Welcome to the world of Magento 2. Why use 10 lines of compact code when 30 lines spread in different files with a dollop of duplication will do!?! May 25, 2022 at 15:58

3 Answers 3

5

enter image description here

Setup/InstallData.php

<?php


namespace Vendor\Module\Setup;

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

class InstallData implements InstallDataInterface
{

    private $eavSetupFactory;

    /**
     * Constructor
     *
     * @param \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Category::ENTITY,
            'is_featured',
            [
                'type' => 'int',
                'label' => 'Is Home Category',
                'input' => 'boolean',
                'sort_order' => 333,
                'source' => '',
                'global' => 1,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => null,
                'group' => 'General Information',
                'backend' => ''
            ]
        );
    }
}

view/adminhtml/ui_component/category_form.xml

<?xml version="1.0" ?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="general">
        <field name="is_featured">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="required" xsi:type="boolean">false</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">false</item>
                    </item>
                    <item name="sortOrder" xsi:type="number">333</item>
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="label" translate="true" xsi:type="string">Is Home Category</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>
1
  • Attribute is created, how to retrieve it by applying filter with 1? Nov 13, 2018 at 3:20
0

You will need to add that field in category form so as to show it. Create an xml file on path: app/code/Vendor/Module/view/adminhtml/ui_component/category_form.xml

Add below code in category_form:

<field name="name-of-you-custom-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">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">Label of the field</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">1</item>
    </item>
</argument>

0

This kind of issue is the normal case when we do the data migration and especially when we have a custom category attribute in the m1.

When you do data migration the data is already transferred to the m2 database but the m2 code structure is obviously diff from m1 so we need extra effort to add the UI component in the custom module.

I.e you have Navision id in the database of m2 as varchar which migrated from m1 but not showing in the backend.

<?xml version="1.0" ?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="general">
        <field name="nav_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="required" xsi:type="boolean">false</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">false</item>
                    </item>
                    <item name="sortOrder" xsi:type="number">333</item>
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="label" translate="true" xsi:type="string">Navision ID</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.