1

I need to know how to populate a dynamicRows ui component I have added to the product edit form in Magento 2.

I've got the data coming from a custom table and the data is getting through. I have the dynamic rows component in the correct place in the advanced pricing modal but the dynamicRows records are not being rendered. Here's my code:

DataProvider.php

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_special_prices_form'][] = $customerGroupsPrice->getData();
    }
    //var_dump($this->loadedData);exit;
    return $this->loadedData;

}

public function getMeta()
{
    return $this->meta;
}

customer_groups_prices_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">customer_groups_special_prices_form.customer_groups_special_prices_form_data_source</item>
        <item name="deps" xsi:type="string">customer_groups_special_prices_form.customer_groups_special_prices_form_data_source</item>
        <item name="namespace" xsi:type="string">customer_groups_special_prices_form</item>
    </item>

    <item name="config" xsi:type="array">
        <item name="dataScope" xsi:type="string">data</item>
        <item name="provider" xsi:type="string">customer_groups_special_prices_form.customer_groups_special_prices_form_data_source</item>
        <item name="deps" xsi:type="string">customer_groups_special_prices_form.customer_groups_special_prices_form_data_source</item>
    </item>
</argument>

<dataSource name="customer_groups_special_prices_form_data_source">
    <argument name="dataProvider" xsi:type="configurableObject">
        <argument name="class" xsi:type="string">Icansee\CustomerGroupsPrices\Model\DataProvider</argument>
        <argument name="name" xsi:type="string">customer_groups_special_prices_form_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>

modifyMeta function in my modifier class:

public function modifyMeta(array $meta)
{
    $meta = array_merge_recursive(
        $meta, [
            'advanced_pricing_modal' => [

                'children' => [
                    'customer_groups_prices_form' => [
                        'arguments' => [
                            'data' => [
                                'config' => [
                                    'label' => __('Customer Group Special Prices'),
                                    'componentType' => Fieldset::NAME,
                                    'dataScope' => 'customer_group_special_prices_form',
                                    'collapsible' => false,
                                    'sortOrder' => 50,
                                ],
                            ],
                        ],
                        'children' => [
                            self::CUSTOMER_GROUP_SPECIAL_PRICES_FIELD => [
                                'arguments' => [
                                    'data' => [
                                        'config' => [
                                            'componentType' => 'dynamicRows',
                                            'component' => 'Magento_Ui/js/dynamic-rows/dynamic-rows',
                                            'label' => __('Group Special Prices'),
                                            'renderDefaultRecord' => false,
                                            'ns' => 'customer_group_special_prices_form',
                                            'externalProvider' => 'customer_groups_special_prices_form.customer_groups_special_prices_form_data_source',
                                            'imports' => [
                                                'productId' => '${ $.provider }:data.entity_id',
                                            ],
                                            'exports' => [
                                                'productId' => '${ $.externalProvider }:params.product_id'
                                            ],
                                            'dataScope' => '',
                                            'dndConfig' => [
                                                'enabled' => false,
                                            ],
                                            'deleteProperty' => false,
                                            'disabled' => false,
                                            'required' => false,
                                            'additionalClasses' => 'admin__field-wide',
                                            'sortOrder' => 50,
                                        ],
                                    ],
                                ],
                                'children' =>  [
                                    'record' => [
                                        'arguments' => [
                                            'data' => [
                                                'config' => [
                                                    'componentType' => Container::NAME,
                                                    'isTemplate' => true,
                                                    'is_collection' => true,
                                                    'component' => 'Magento_Ui/js/dynamic-rows/record',
                                                    'dataScope' => 'data.customer_group_special_prices_form',
                                                ],
                                            ],
                                        ],
                                        'children' => [
                                            'entity_id' => [
                                                'arguments' => [
                                                    'data' => [
                                                        'config' => [
                                                            'dataType' => Text::NAME,
                                                            'formElement' => Input::NAME,
                                                            'componentType' => Field::NAME,
                                                            'dataScope' => 'entity_id',
                                                            'visible' => false,
                                                            'disabled' => true,
                                                            'sortOrder' => 10,
                                                        ],
                                                    ],
                                                ],
                                            ],
                                            'product_id' => [
                                                'arguments' => [
                                                    'data' => [
                                                        'config' => [
                                                            'dataType' => Text::NAME,
                                                            'formElement' => Input::NAME,
                                                            'componentType' => Field::NAME,
                                                            'dataScope' => 'product_id',
                                                            'visible' => false,
                                                            'disabled' => true,
                                                            'sortOrder' => 20,
                                                        ],
                                                    ],
                                                ],
                                            ],
                                            'website_id' => [
                                                'arguments' => [
                                                    'data' => [
                                                        'config' => [
                                                            'dataType' => Text::NAME,
                                                            'formElement' => Select::NAME,
                                                            'componentType' => Field::NAME,
                                                            'dataScope' => 'website_id',
                                                            'label' => __('Website'),
                                                            'options' => $this->getWebsites(),
                                                            'visible' => $this->isMultiWebsites(),
                                                            'disabled' => ($this->isShowWebsiteColumn() && !$this->isAllowChangeWebsite()),
                                                            'sortOrder' => 30,
                                                        ],
                                                    ],
                                                ],
                                            ],
                                            'customer_group_id' => [
                                                'arguments' => [
                                                    'data' => [
                                                        'config' => [
                                                            'formElement' => Select::NAME,
                                                            'componentType' => Field::NAME,
                                                            'dataType' => Text::NAME,
                                                            'dataScope' => 'customer_group_id',
                                                            'label' => __('Customer Group'),
                                                            'options' => $this->getCustomerGroups(),
                                                            'sortOrder' => 40,
                                                        ],
                                                    ],
                                                ],
                                            ],
                                            'price' => [
                                                'arguments' => [
                                                    'data' => [
                                                        'config' => [
                                                            'formElement' => Input::NAME,
                                                            'componentType' => Field::NAME,
                                                            'dataType' => Number::NAME,
                                                            'label' => __('Price'),
                                                            'dataScope' => 'price',
                                                            'sortOrder' => 50,
                                                            'validation' => [
                                                                'required-entry' => true,
                                                                'validate-greater-than-zero' => true,
                                                                'validate-digits' => false,
                                                                'validate-number' => true,
                                                            ],
                                                        ],
                                                    ],
                                                ],
                                            ],
                                            'actionDelete' => [
                                                'arguments' => [
                                                    'data' => [
                                                        'config' => [
                                                            'componentType' => 'actionDelete',
                                                            'dataType' => Text::NAME,
                                                            'label' => '',
                                                            'sortOrder' => 60,
                                                        ],
                                                    ],
                                                ],
                                            ],
                                        ],
                                    ],
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ]
    );

    return $meta;
}

di.xml excerpt:

<virtualType name="CustomerGroupsPricesFilterPool" type="Magento\Framework\View\Element\UiComponent\DataProvider\FilterPool">
    <arguments>
        <argument name="appliers" xsi:type="array">
            <item name="regular" xsi:type="object">Magento\Framework\View\Element\UiComponent\DataProvider\RegularFilter</item>
            <item name="fulltext" xsi:type="object">Magento\Framework\View\Element\UiComponent\DataProvider\FulltextFilter</item>
        </argument>
    </arguments>
</virtualType>

<virtualType name="CustomerGroupsPricesDataProvider" type="Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider">
    <arguments>
        <argument name="collection" xsi:type="object" shared="false">Icansee\CustomerGroupsPrices\Model\ResourceModel\CustomerGroup\Collection</argument>
        <argument name="filterPool" xsi:type="object" shared="false">CustomerGroupsPricesFilterPool</argument>
    </arguments>
</virtualType>

<type name="Icansee\CustomerGroupsPrices\Model\ResourceModel\CustomerGroup\Collection">
    <arguments>
        <argument name="mainTable" xsi:type="string">ics_customer_groups_price</argument>
        <argument name="eventPrefix" xsi:type="string">customer_groups_special_prices_form_grid_collection</argument>
        <argument name="eventObject" xsi:type="string">customer_groups_special_prices_grid_collection</argument>
        <argument name="resourceModel" xsi:type="string">Icansee\CustomerGroupsPrices\Model\ResourceModel\CustomerGroupsPrice</argument>
    </arguments>
</type>

<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
    <arguments>
        <argument name="collections" xsi:type="array">
            <item name="customer_groups_special_prices_form_data_source" xsi:type="string">Icansee\CustomerGroupsPrices\Model\ResourceModel\CustomerGroup\Grid\Collection</item>
        </argument>
    </arguments>
</type>

The data from the custom database table comes through when I var_dump it from the data provider class, the issue is that the records of the dynamicRows are not being rendered, so there is something missing to link the datasource/data to the ui component. What am I missing?

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.