2

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__
);

============================================================

  1. It has created successfully a "Catalog Input Type for Store Owner"

  2. I go to Stores -> Product-> Add New Attribute with "Catalog Input Type for Store Owner" = "customattributetype". Click Save Attribute

  3. Add the new attribute to the Default attribute set

  4. 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

  • Same Issue. Any Solution to fix this? – Pavithra Feb 14 '19 at 10:42
  • @Pavithra take a look at my answer below if you still got this issue. – fudu Feb 25 at 8:01
0

I was doing after you, and here is my solution:

The reason which cause this issue is you set the 'value' => 'customattributetype', it is not default values.

This value should be "text, price, select, media, multiselect .."
enter image description here

You can check the default values in eav_attribute table after you create a new attribute.
enter image description here

So the solution is you change the 'value' => 'customattributetype', to 'value' => 'text', or something in the default values, then delete/remove the old attribute and create a new one.

And don't forget to put the old attribute out of the attribute set.

| 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.