michielenzo
michielenzo

Reputation: 33

Magento 2 set the value of a custom form element in the adminhtml > catalog > products > edit form

In Magento 2 i have created a custom fieldset with one field in Vendor\Module\view\adminhtml\ui_component\productform.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="custom_fieldset">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Custom Fields</item>
                <item name="sortOrder" xsi:type="number">10</item>
                <item name="collapsible" xsi:type="boolean">true</item>
            </item>
        </argument>
        <field name="special_ability">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Special Ability</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">product</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

This results in this fieldset:

enter image description here

I want the value of my field to be filled in.

I have tried making a after plugin on the getData() function of the \Magento\Catalog\Ui\DataProvider\Product\Form\ProductDataProvider class.


declare(strict_types=1);

namespace Vendor\Module\Plugin;

class ProductDataProviderPlugin
{
    public function afterGetData($subject, $result)
    {
        $productId = current(array_keys($result));

        $result[$productId]['product']['special_ability'] = "forehead beam";

        return $result;
    }
}

This does not work. The product object does get an extra attribute in the code but does not match this automatically with my custom field. What extra steps do i need to take in order to fill in my field?

Upvotes: 1

Views: 445

Answers (0)

Related Questions