1

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

5

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

  • Thanks Man :) It's working. Can you please give me one more answer ? How to save that value on save action ? that use_in_custom column available in catalog_eav_attribute table. – Emipro Technologies Pvt. Ltd. Aug 17 '18 at 10:47
  • let me check and know you – Abdul Aug 17 '18 at 10:50
  • Okay sure.... :) – Emipro Technologies Pvt. Ltd. Aug 17 '18 at 10:51
  • have you created custom field " use_in_custom" in catalog_eav_attribute table? – Abdul Aug 17 '18 at 11:04
  • Yes... It's already created. – Emipro Technologies Pvt. Ltd. Aug 17 '18 at 11:04

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.