Magento 2.2.5 – Add Custom Field in Product Attribute Form

custom-fieldmagento-2.2.5magento2ui-form

Go to : Store -> Attributes -> Product -> Edit attribute

I want to add custom dropdown as like Used in Product Listing dropdown.

I added it. But, It's display in Properties tab instead of Storefront Properties tab.

Please help me how to add that in Properties tab.

I upload my code and screenshot here.

CompanyName\ModuleName\Block\Adminhtml\Product\Attribute\Edit\Tab\Front.php

<?php

namespace CompanyName\ModuleName\Block\Adminhtml\Product\Attribute\Edit\Tab;

class Front extends \Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab\Front
{
    protected function _prepareForm()
    {

        parent::_prepareForm();
        $attributeObject = $this->_coreRegistry->registry('entity_attribute');
        $form = $this->_formFactory->create(
            ['data' => ['id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post']]
        );

        $yesnoSource = $this->_yesNo->toOptionArray();
        $fieldset = $form->addFieldset(
            'front_fieldset',
            ['legend' => __('Storefront Properties'), 'collapsable' => $this->getRequest()->has('popup')]
        );
        $fieldset->addField(
            'used_in_custom',
            'select',
            [
                'name' => 'used_in_custom',
                'label' => __('Used in Custom'),
                'title' => __('Used in Custom'),
                'note' => __('Depends on design theme.'),
                'values' => $yesnoSource,
            ]
        );
        $this->setForm($form);
        return \Magento\Backend\Block\Widget\Form\Generic::_prepareForm();
    }
}

CompanyName/ModuleName/view/adminhtml/ui_component/product_attribute_add_form.xml

<?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="front_fieldset">
        <field name="used_in_custom" sortOrder="130" formElement="checkbox">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="default" xsi:type="number">1</item>
                </item>
            </argument>
            <settings>
                <notice translate="true">Depends on design theme.</notice>
                <dataType>boolean</dataType>
                <label translate="true">Used in Custom</label>
                <dataScope>used_in_custom</dataScope>
            </settings>
            <formElements>
                <checkbox>
                    <settings>
                        <valueMap>
                            <map name="false" xsi:type="number">0</map>
                            <map name="true" xsi:type="number">1</map>
                        </valueMap>
                        <prefer>toggle</prefer>
                    </settings>
                </checkbox>
            </formElements>
        </field>
     </fieldset>
</form>

enter image description here

Best Answer

Try via plugin method:

CompanyName/ModuleName/etc/adminhtml/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">
    <type name="Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab\Front">
        <plugin name="companyname_attribute_edit_form" type="CompanyName\ModuleName\Plugin\Block\Adminhtml\Product\Attribute\Edit\Tab\Front" sortOrder="1"/>
    </type>
</config>

CompanyName/ModuleName/Plugin/Block/Adminhtml/Product/Attribute/Edit/Tab/Front.php

<?php
namespace CompanyName\ModuleName\Plugin\Block\Adminhtml\Product\Attribute\Edit\Tab;

class Front
{

    /**
     * @var Yesno
     */
    protected $_yesNo;

    /**
     * @param Magento\Config\Model\Config\Source\Yesno $yesNo
     */
    public function __construct(
        \Magento\Config\Model\Config\Source\Yesno $yesNo
    ) {
        $this->_yesNo = $yesNo;
    }

    /**
     * Get form HTML
     *
     * @return string
     */
    public function aroundGetFormHtml(
        \Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab\Front $subject,
        \Closure $proceed
    )
    {

        $yesnoSource = $this->_yesNo->toOptionArray();
        $form = $subject->getForm();
        $fieldset = $form->getElement('front_fieldset');
        $fieldset->addField(
            'used_in_custom',
            'select',
            [
                'name' => 'used_in_custom',
                'label' => __('Used in Custom'),
                'title' => __('Used in Custom'),
                'note' => __('Depends on design theme.'),
                'values' => $yesnoSource,
            ]
        );
        return $proceed();
    }
}

After run command : php bin/magento setup:di:compile