1

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?

1 Answers1

-1

Issue might be with how you're setting the data in the result array.

In your plugin, you're setting the 'special_ability' attribute on the 'product' object, but the UI component might be expecting it on the 'product_form' object.

Try to change:

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

to this:

$result[$productId]['product_form']['special_ability'] = "forehead beam";
Mr.Devops
  • 301
  • 2
  • 12