I need to create a special input type in adminhtml product form. My input will display as a table but save data as a standard text input.
I need to create a "Catalog Input Type for Store Owner". With input renderer is text
File: Magento/Customattribute/etc/module.xml
<?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="Magento_Customattribute" setup_version="0.0.1" />
</config>
File: Magento/Customattribute/etc/config.xml
<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>
<customattributetype>customattributetype</customattributetype>
</input_types>
</validator_data>
</general>
</default>
</config>
File: Magento/Customattribute/etc/adminhtml/events.xml
<?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="custom_attributes_type" instance="Magento\Customattribute\Observer\AddAttributeTypeObserver" />
</event>
</config>
File: Magento/Customattribute/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Model\ResourceModel\Attribute" type="Magento\Customattribute\Model\Catalog\ResourceModel\Attribute" />
</config>
File: Magento/Customattribute/Model/Catalog/ResourceModel/Attribute.php
<?php
namespace Magento\Customattribute\Model\Catalog\ResourceModel;
class Attribute extends \Magento\Catalog\Model\ResourceModel\Attribute
{
protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
{
if ($object->getFrontendInput() === 'customattributetype') {
$object->setBackendType('varchar');
$object->setData('frontend_input_renderer', 'Magento\Customattribute\Block\Attribute\Renderer');
}
return parent::_beforeSave($object);
}
}
File: Magento/Customattribute/etc/config.xml
<?php
namespace Magento\Customattribute\Block\Attribute;
class Renderer extends \Magento\Framework\Data\Form\Element\Text
{
public function getHtml()
{
$this->addClass('customattributetype-input');
return parent::getHtml();
}
}
File: Magento/Customattribute/Observer/AddAttributeTypeObserver.php
<?php
namespace Magento\Customattribute\Observer;
use Magento\Framework\Event\ObserverInterface;
class AddAttributeTypeObserver implements ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
$response = $observer->getEvent()->getResponse();
$types = $response->getTypes();
$types[] = [
'value' => 'customattributetype',
'label' => __('customattributetype'),
];
$response->setTypes($types);
return $this;
}
}
File: Magento/Customattribute/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Magento_Customattribute',
__DIR__
);
============================================================
It has created successfully a "Catalog Input Type for Store Owner"
I go to Stores -> Product-> Add New Attribute with "Catalog Input Type for Store Owner" = "customattributetype". Click Save Attribute
Add the new attribute to the Default attribute set
Product -> Catalog -> Add Product (simple product)
It has an error:
"Fatal error: Uncaught TypeError: Argument 2 passed to Magento\Framework\View\Element\UiComponentFactory::argumentsResolver() must be of the type array, null given, called in /var/www/html/magento2.3.0/vendor/magento/framework/View/Element/UiComponentFactory.php on line 223 and defined in /var/www/html/magento2.3.0/vendor/magento/framework/View/Element/UiComponentFactory.php:174 Stack trace: #0 /var/www/html/magento2.3.0/vendor/magento/framework/View/Element/UiComponentFactory.php(223): Magento\Framework\View\Element\UiComponentFactory->argumentsResolver('percent', NULL) #1 /var/www/html/magento2.3.0/vendor/magento/module-ui/Component/Form/Field.php(85): Magento\Framework\View\Element\UiComponentFactory->create('percent', 'percent', Array) #2 /var/www/html/magento2.3.0/vendor/magento/framework/View/Layout/Generator/UiComponent.php(164): Magento\Ui\Component\Form\Field->prepare() #3 /var/www/html/magento2.3.0/vendor/magento/framework/View/Layout/Generator/UiComponent.php(161): Magento\Framework\View\Layout\Generator\UiCompo in /var/www/html/magento2.3.0/vendor/magento/framework/View/Element/UiComponentFactory.php on line 174"
Please help me