Skip to content

Tie dynamic fields from custom phtml block to Product Edit Save request

I have created a custom block on my Product Edit admin page by adding this to product_form.xml:

<fieldset name="my_custom_fields">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">My Custom Fields</item>
            <item name="sortOrder" xsi:type="number">30</item>
            <item name="collapsible" xsi:type="boolean">true</item>
            <item name="opened" xsi:type="boolean">false</item>
            <item name="ns" xsi:type="string">my_catalog</item>
        </item>
    </argument>
    <container name="my_custom_fields_container">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="sortOrder" xsi:type="number">102</item>
            </item>
        </argument>
        <htmlContent name="html_content">
            <argument name="block" xsi:type="object">MyCatalogBlockAdminhtmlProductMyCustomFields</argument>
        </htmlContent>
    </container>
</fieldset>

Template:

<?php

namespace MyCatalogBlockAdminhtmlProduct;

use MagentoBackendBlockTemplate;

class MyCustomFields extends Template
{
    protected $_template = 'catalog/product/my-custom-fields.phtml';
}

And then my phtml file:

<?php
/** @var UseCaseFilters $block */

use MyCatalogBlockAdminhtmlProductMyCustomFields;

?>

<input type="text" name="my_test_input" />

The problem is that when I click the Save button on the Product Edit page in Magento, the data in my_test_input does not make it to the post request.

How can I bind the data fields from my .phtml file so that Magento knows to include them in the request?

The reason I am using a template is because in reality I have several dynamic fields based on other data I fetch in my custom block controller. So I have several fields that I need to include in my save request, but I can’t even get a simple static field to be included in the Save request.