We learn how to create custom category attributes using InstallData.php in Magento 2. When we develop modules or themes we need some custom attributes in category form for some requirements. We can add different type of category attribute like toggle, select, text, WYSIWYG editor etc. type files in Category form.
We used the InstallData.php file for creating custom category attributes. InstallData.php file places at your module path app/code/VendoreName/ModuleName/Setup/InstallData.php. This file executes only once when our module is installing the first time. After the module is successfully installed InstallData.php file does not execute. It means the InstallData.php file only executes once while the module first time installed.
If you don’t know about InstallData.php then click here
Here, we creating one custom toggle input in Category Form
- Step-1: Create registration.php at app/code/Dolphin/CategoryAttribute
1 2 3 4 5 6 | <?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Dolphin_CategoryAttribute', __DIR__ ); |
- Step-2: Create module.xml at app/code/Dolphin/CategoryAttribute/etc
1 2 3 4 5 6 7 8 9 10 | <?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="Dolphin_CategoryAttribute" setup_version="1.0.0"> <sequence> <module name="Magento_Catalog"/> <module name="Magento_Backend"/> </sequence> </module> </config> |
- Step-3: Create InstallData.php at app/code/Dolphin/CategoryAttribute/Setup
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <?php namespace Dolphin\CategoryAttribute\Setup; use Magento\Eav\Model\Entity\Attribute\Source\Boolean; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; class InstallData implements InstallDataInterface { private $eavSetupFactory; public function __construct(EavSetupFactory $eavSetupFactory) { $this->eavSetupFactory = $eavSetupFactory; } public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); /* Remove Category Attribute */ $eavSetup->removeAttribute(\Magento\Catalog\Model\Category::ENTITY, 'custom_toggle'); /* Add Category Attribute */ $eavSetup->addAttribute( \Magento\Catalog\Model\Category::ENTITY, 'custom_toggle', [ 'type' => 'int', 'label' => 'Custom Toggle', 'input' => 'boolean', 'source' => Boolean::class, 'visible' => true, 'required' => false, 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE, 'group' => 'General Information', 'default' => '0', ] ); $setup->endSetup(); } } |
- Step-4: Create category_form.xml at app/code/Dolphin/CategoryAttribute/view/adminhtml/ui_component
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <?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="general"> <field name="custom_toggle" sortOrder="25" formElement="checkbox"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="source" xsi:type="string">custom_toggle</item> <item name="default" xsi:type="number">0</item> </item> </argument> <settings> <validation> <rule name="required-entry" xsi:type="boolean">false</rule> </validation> <dataType>boolean</dataType> <label translate="true">Custom Toggle</label> <scopeLabel>[STORE VIEW]</scopeLabel> <tooltip> <description translate="true">Your Note Here.</description> </tooltip> </settings> <formElements> <checkbox> <settings> <valueMap> <map name="false" xsi:type="string">0</map> <map name="true" xsi:type="string">1</map> </valueMap> <prefer>toggle</prefer> </settings> </checkbox> </formElements> </field> </fieldset> </form> |
So after you create the above files Please run Magento commands
1 2 3 | php bin/magento setup:upgrade php bin/magento setup:static-content:deploy -f php bin/magento cache:clean |
Check you category form in the admin panel you can show custom toggle attribute like below.
I hope this helps you. For any doubts regarding this topic, please write your doubts in the comments section.
Hi I have written same code in my module and I am not able to save value of custom toggle neither in db I am finding enrty of custom attribute