Add Custom Catalog Input Type in Magento 2
Today we will see how to add custom catalog input type in Magento 2. An event adminhtml_product_attribute_types is dispatched when the catalog input types are displayed. This event can be used to add custom catalog input types.
First in etc/config.xml file inside your module
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> <default> <general> <validator_data> <input_types> <custom>custom</custom> </input_types> </validator_data> </general> </default> </config>
Next in etc/adminhtml/events.xml file inside your module
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="adminhtml_product_attribute_types"> <observer name="webkul.custom.attribute" instance="Webkul\CustomAttributeType\Observer\AddCustomAttributeType" /> </event> </config>
At last in your observer file path mentioned above
<?php namespace Webkul\CustomAttributeType\Observer; use Magento\Framework\Event\ObserverInterface; class AddCustomAttributeType implements ObserverInterface { /** * Add new attribute type to manage attributes interface * * @param \Magento\Framework\Event\Observer $observer * @return $this */ public function execute(\Magento\Framework\Event\Observer $observer) { // adminhtml_product_attribute_types try { $response = $observer->getEvent()->getResponse(); $types = $response->getTypes(); $types[] = [ 'value' => 'custom', 'label' => __('Custom') ]; $response->setTypes($types); return $this; } catch(\Exception $e) { echo $e->getMessage(); } } }
This will add a new catalog input type.
For more details you can see this magento file
Magento/Weee/Observer/AddWeeeTaxAttributeTypeObserver.php
That’s it.
Happy coding
Be the first to comment.