1

I added a custom field inside my product attributes and it's render and save OK, but when I reload the attribute form, the value of my custom field is empty. If I enter to the form data inside the _prepareForm in Front.php, I see my custom field value that is 5 right now

enter image description here

I create my plugin with this code:

/app/code/Vendor/CustomModule/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="vendor_custommodule_attribute_edit_form" type="Vendor\CustomModule\Plugin\Block\Adminhtml\Product\Attribute\Edit\Tab\Front" sortOrder="1"/>
    </type>
</config>

/app/code/Vendor/CustomModule/Plugin/Block/Adminhtml/Product/Attribute/Edit/Tab/Front.php

namespace Vendor\CustomModule\Plugin\Block\Adminhtml\Product\Attribute\Edit\Tab;

class Front
{

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

        $form = $subject->getForm();
        if (is_object($form)) {
            $fieldset = $form->addFieldset(
                'custom_fieldset',
                ['legend' => __('Custom options')]
            );

            $fieldset->addField(
                'my_custom_field',
                'text',
                [   
                    'name'      => 'my_custom_field',
                    'label'     => __('My custom field'),
                    'title'     => __('My custom field'),
                ]
            );
        }
        return $proceed();
    }
}

/app/code/Vendor/CustomModule/view/adminhtml/ui_component/product_attribute_add_form.xml

<?xml version="1.0"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="custom_fieldset">
        <field name="my_custom_field">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">130</item>
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="label" xsi:type="string" translate="true">My custom field</item>
                    <item name="default" xsi:type="number">0</item>
                </item>
            </argument>
        </field>
    </fieldset>    
</form>

The result is this:

enter image description here

But in the database I have the correct value that is currently 5

Somebody could help me please?

0

protected $_coreRegistry;


public function __construct(
    \Magento\Framework\Registry $registry
) {
    $this->_coreRegistry = $registry;
}

/**
 * Get form HTML
 *
 * @return string
 */
public function aroundGetFormHtml(
    \Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab\Front $subject,
    \Closure $proceed
)
{
    $attributeObject = $this->_coreRegistry->registry('entity_attribute');
    $yesnoSource = $this->_yesNo->toOptionArray();
    $form = $subject->getForm();
    if (is_object($form)) {
        $fieldset = $form->addFieldset(
            'custom_fieldset',
            ['legend' => __('Custom options')]
        );

        $fieldset->addField(
            'my_custom_field',
            'text',
            [   
                'name'      => 'my_custom_field',
                'label'     => __('My custom field'),
                'title'     => __('My custom field'),
            ]
        );
    }
    $form->setValues($attributeObject->getData());
    return $proceed();
}

you forgot to setValues for the form Try these codes and run again

  • np, i'm glad that helped u :) – Quang Huy Nov 26 '18 at 8:35
  • Hello Saul, does your extension works now??? If yes, would you share this extension? I also nee to add some fields to the attribute table! – Dr. Christian Kusche Feb 27 at 5: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.