2

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?

4

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>
| improve this answer | |
  • Attribute is created, how to retrieve it by applying filter with 1? – jafar pinjar Nov 13 '18 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>

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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