I'm facing to a very ridiculous trouble, I added a custom product attribute with source model, backend model as a select box. Everything works fine (my custom attribute will get the values from a custom table) except the input_renderer
part.
Here is what I tried, (running on default mode)
Setup\MySetup.php
public function getDefaultEntities()
{
return [
'catalog_product' => [
'entity_model' => 'Magento\Catalog\Model\ResourceModel\Product',
'attribute_model' => 'Magento\Catalog\Model\ResourceModel\Eav\Attribute',
'table' => 'catalog_product_entity',
'additional_attribute_table' => 'catalog_eav_attribute',
'entity_attribute_collection' => 'Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection',
'attributes' => [
'custom_ids' => [
'type' => 'static',
'input' => 'select',
'label' => 'Custom Attribute',
'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
'backend' => 'Vendor\Module\Model\Product\Attribute\Backend\CustomAttr',
'source' => 'Vendor\Module\Model\Product\Attribute\Source\CustomAttr',
'input_renderer' => 'Vendor\Module\Block\Adminhtml\Product\Helper\Form\CustomAttr',
'required' => false,
'sort_order' => 9,
'visible' => true,
'group' => 'General',
'is_used_in_grid' => false,
'is_visible_in_grid' => false,
'is_filterable_in_grid' => false,
],
];
}
Block\Adminhtml\Product\Helper\Form\CustomAttr.php
class CustomAttr extends \Magento\Framework\Data\Form\Element\Select
{
public function getAfterElementHtml()
{
if (!$this->isAllowed()) {
return '';
}
$htmlId = $this->getHtmlId();
$suggestPlaceholder = __('start typing to search custom attribute');
$selectorOptions = $this->jsonEncoder->encode($this->getSelectorOptions());
$backendUrl = $this->backendData->getUrl('vendor_module/customattr_index/index/suggestCustomAttr');
$return
= <<<HTML
/** Some HTML and JavaScript here **/
HTML;
return $return;
}
}
I tried to die()
inside getAfterElementHtml()
method, but nothing happens. I even go to the database and change the value of column frontend_input_renderer
in table catalog_eav_attribute
to something doesn't exists.
But there is NO ERROR at all !!! Magento 2 just simply to denied to call my input renderer 🙂
Anyone here have any suggestions or a best pratices for this?
Thank you.
Best Answer
TL;DR: If you need to customise your custom attribute in product edit form Magento 2, use PHP modifier in UI components instead of input renderer.
I found the solution myself.
As we all knew that Magento 2 using UI components, the solution I'm talking about here is PHP modifier in UI components.
From Magento 2 devdocs:
By default, Magento 2 using PHP modifiers to render EAV attribute. They never use
input_renderer
to do so. If you are reading here, I assume that you need few examples, so here you are :)Above is example of two important file which ping Magento 2 to render your custom attribute fields in product edit form to whatever you want. You will need to customise with javascript if you want it plays smoothly.
And yes, welcome to Magento 2 most painful's world - UI components.