0

I've added a dynamic rows element which is populated from a custom table. This is working fine but the datasource for this ui component overrides the default product data source and the default product form fields are not being populated. I've added my datasource like this:

class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{
/**
 * @param string $name
 * @param string $primaryFieldName
 * @param string $requestFieldName
 * @param CollectionFactory $customerGroupsPricesCollectionFactory
 * @param array $meta
 * @param array $data
 */
public function __construct(
    $name,
    $primaryFieldName,
    $requestFieldName,
    CollectionFactory $customerGroupsPricesCollectionFactory,
    array $meta = [],
    array $data = []
) {
    $this->collection = $customerGroupsPricesCollectionFactory->create();
    parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
}

public function getData()
{
    if (isset($this->loadedData)) {
        return $this->loadedData;
    }

    $items = $this->collection->getItems();
    $this->loadedData = array();

    /** @var CustomerGroupsPrice $customerGroupsPrice */
    foreach ($items as $customerGroupsPrice) {
        $this->loadedData[$customerGroupsPrice->getProductId()]['customer_groups_price_container'][] = $customerGroupsPrice->getData();
    }
    //var_dump($this->loadedData);exit;
    return $this->loadedData;

} 
}

and my product_form.xml

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<argument name="data" xsi:type="array">
    <item name="js_config" xsi:type="array">
        <item name="provider" xsi:type="string">product_form.customer_groups_prices_data_source</item>
        <item name="deps" xsi:type="string">product_form.customer_groups_prices_data_source</item>
    </item>
    <item name="config" xsi:type="array">
        <item name="dataScope" xsi:type="string">data</item>
        <item name="namespace" xsi:type="string">product_form</item>
    </item>
</argument>

<settings> 
    <namespace>product_form</namespace>
    <dataScope>data</dataScope>
    <deps>
        <dep>product_form.customer_groups_prices_data_source</dep>
    </deps>
</settings>

<dataSource name="customer_groups_prices_data_source">
    <argument name="dataProvider" xsi:type="configurableObject">
        <argument name="class" xsi:type="string">Vendor\Module\Model\DataProvider</argument>
        <argument name="name" xsi:type="string">customer_groups_prices_data_source</argument>
        <argument name="primaryFieldName" xsi:type="string">product_id</argument>
        <argument name="requestFieldName" xsi:type="string">id</argument>
        <argument name="data" xsi:type="array">
            <item name="js_config" xsi:type="array">
                <item name="component" xsi:type="string">Magento_Ui/js/form/provider</item>
            </item>
        </argument>
    </argument>
</dataSource>

<fieldset name="customer_groups_price_set">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Customer Group Special Prices</item>
            <item name="sortOrder" xsi:type="number">10</item>
        </item>
     </argument>
     <container name="customer_groups_price_container">
        <argument name="data" xsi:type="array">
           <item name="config" xsi:type="array">
               <item name="component" xsi:type="string">Magento_Ui/js/dynamic-rows/dynamic-rows</item>
               <item name="template" xsi:type="string">ui/dynamic-rows/templates/default</item>
               <item name="componentType" xsi:type="string">dynamicRows</item>
               <item name="recordTemplate" xsi:type="string">record</item>
               <item name="addButtonLabel" xsi:type="string">Add Row</item>
               <item name="deleteProperty" xsi:type="boolean">false</item>
           </item>
       </argument>

        <container name="record">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string" translate="true">Group Special Price</item>
                    <item name="component" xsi:type="string" translate="true">Magento_Ui/js/dynamic-rows/record</item>
                    <item name="isTemplate" xsi:type="boolean">true</item>
                    <item name="is_collection" xsi:type="boolean">true</item>
                    <item name="showFallbackReset" xsi:type="boolean">false</item>
                    <item name="source" xsi:type="string">customer_groups_price_container</item>
                </item>
            </argument>
            <field name="entity_id">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="visible" xsi:type="boolean">false</item>
                        <item name="dataType" xsi:type="string">text</item>
                        <item name="formElement" xsi:type="string">input</item>
                        <item name="source" xsi:type="string">customer_groups_price_container</item>
                        <item name="dataScope" xsi:type="string">entity_id</item>
                        <item name="required" xsi:type="boolean">true</item>
                    </item>
                </argument>
            </field>

             <field name="product_id">
                 <argument name="data" xsi:type="array">
                     <item name="config" xsi:type="array">
                         <item name="visible" xsi:type="boolean">false</item>
                         <item name="dataType" xsi:type="string">text</item>
                         <item name="formElement" xsi:type="string">input</item>
                         <item name="source" xsi:type="string">customer_groups_price_container</item>
                         <item name="dataScope" xsi:type="string">product_id</item>
                         <item name="required" xsi:type="boolean">true</item>
                     </item>
                 </argument>
             </field>

             <field name="website_id">
                 <argument name="data" xsi:type="array">
                     <item name="config" xsi:type="array">
                         <item name="label" xsi:type="string">Website ID</item>
                         <item name="visible" xsi:type="boolean">true</item>
                         <item name="dataType" xsi:type="string">text</item>
                         <item name="formElement" xsi:type="string">input</item>
                         <item name="source" xsi:type="string">customer_groups_price_container</item>
                         <item name="dataScope" xsi:type="string">website_id</item>
                         <item name="required" xsi:type="boolean">true</item>
                     </item>
                 </argument>
             </field>

             <field name="customer_group_id">
                 <argument name="data" xsi:type="array">
                     <item name="config" xsi:type="array">
                         <item name="label" xsi:type="string">Customer Group</item>
                         <item name="visible" xsi:type="boolean">true</item>
                         <item name="dataType" xsi:type="string">text</item>
                         <item name="formElement" xsi:type="string">select</item>
                         <item name="source" xsi:type="string">customer_groups_price_container</item>
                         <item name="dataScope" xsi:type="string">customer_group_id</item>
                         <item name="required" xsi:type="boolean">true</item>
                     </item>
                     <item name="options" xsi:type="object">Magento\CatalogRule\Model\Rule\CustomerGroupsOptionsProvider</item>
                 </argument>
             </field>

             <field name="price">
                 <argument name="data" xsi:type="array">
                     <item name="config" xsi:type="array">
                         <item name="label" xsi:type="string">Special Price</item>
                         <item name="visible" xsi:type="boolean">true</item>
                         <item name="dataType" xsi:type="string">text</item>
                         <item name="formElement" xsi:type="string">input</item>
                         <item name="source" xsi:type="string">customer_groups_price_container</item>
                         <item name="dataScope" xsi:type="string">price</item>
                         <item name="required" xsi:type="boolean">true</item>
                     </item>
                 </argument>
             </field>
        </container>
    </container>
</fieldset>

The custom form fieldset renders as expected: enter image description here

but the rest of the product data is not loaded. I can see from inspecting using DevTools in Chrome that the data model for the product is loaded and all product data is there: enter image description here it just isn't populating in the form fields when I add my custom datasource. Can anyone help me add the custom datasource without losing the rest of the product data please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.