0

Is there a way to get data from a DataProvider to my UI Form component, without having a collection or a key?

My DataProvider currently only has a "getData" function and the form is rendered, but I can't get any values to my form.

My objective is to connect to an API to get the values to be used in the form. This is my DataProvider:

<?php

namespace Invoicing\Moloni\Model\Settings;

class DataProvider extends \Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider
{
    public $loadedData;

    public function getData()
    {
        return [];
    }

}

And this is my UI Component:

<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">moloni_settings_edit.settings_data_source</item>
        <item name="deps" xsi:type="string">moloni_settings_edit.settings_data_source</item>
    </item>
    <item name="label" xsi:type="string" translate="true">Settings</item>
    <item name="layout" xsi:type="array">
        <item name="type" xsi:type="string">tabs</item>
    </item>

    <item name="buttons" xsi:type="array">
        <item name="save" xsi:type="string">Invoicing\Moloni\Block\Adminhtml\Settings\Buttons\SaveButton</item>
    </item>

</argument>

<dataSource name="settings_data_source">
    <argument name="dataProvider" xsi:type="configurableObject">
        <argument name="class" xsi:type="string">Invoicing\Moloni\Model\Settings\DataProvider</argument>
        <argument name="name" xsi:type="string">settings_data_source</argument>
        <argument name="primaryFieldName" xsi:type="string">id</argument>
        <argument name="requestFieldName" xsi:type="string">id</argument>
    </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>
</dataSource>

<fieldset name="general">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">General settings</item>
        </item>
    </argument>

    <field name="document_set_id">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Document set</item>
                <item name="dataType" xsi:type="string">text</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="source" xsi:type="string">document_set_id</item>
            </item>
        </argument>
    </field>
</fieldset>

After exploring the Magento code, I got to the form.php that has the following:

$id = $this->getContext()->getRequestParam($this->getContext()->getDataProvider()->getRequestFieldName(), null);

This $id in my case is null because I don't need to have any id.

Then I end up with a empty $dataSource

if (isset($data[$id])) {
        $dataSource = [
            'data' => $data[$id]
        ];
    } elseif (isset($data['items'])) {
        foreach ($data['items'] as $item) {
            if ($item[$item['id_field_name']] == $id) {
                $dataSource = ['data' => ['general' => $item]];
            }
        }
    }

In my form I will need to have data from a external API (options) and selected values (those are stored in a database table).

What's the best way to achieve this? Thank you in advance.

2 Answers 2

0

What I ended up doing for now, was adding a "id" to the url with the value of "1", and in the getData, I returned it like this:

public function getData()
{
    $values = ['1' => ['document_set_id' => 991]]
    return $values;
}

And I manage to get the values.

But after searching for a better solution in Magento core modules, I took for example the Users module.

That module extends \Magento\Backend\Block\Widget\Form\Generic Is there any problem using that class even if it says its "Deprecated"?

I can't really seem to get my UI Components to work without this "hack".

0

You must create dataprovider like that app/code/[Namespace]/[Module]/Model/DataProvider with code

    <?php
namespace [Namespace]\[Module]\Model;
 
class DataProvider extends \Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider
{    
    public function getData()
    {
        return [];
    }
}

and use it in your ui_component layout app/code/[Namespace]/[Module]/view/adminhtml/ui_component/test_test_form.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">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">namespace_module_form.namespace_module_form_data_source</item>
            <item name="deps" xsi:type="string">namespace_module_form.namespace_module_form_data_source</item>
        </item>
        <item name="label" xsi:type="string" translate="true">Rfq Information</item>
        <item name="config" xsi:type="array">
            <item name="dataScope" xsi:type="string">data</item>
            <item name="namespace" xsi:type="string">namespace_module_form</item>
        </item>
        <item name="buttons" xsi:type="array">
            <item name="back" xsi:type="array">
                <item name="name" xsi:type="string">Back</item>
                <item name="label" xsi:type="string" translate="true">Back</item>
                <item name="class" xsi:type="string">back</item>
                <item name="url" xsi:type="string">*/*/index</item>
            </item>
            <item name="save" xsi:type="array">
                <item name="name" xsi:type="string">Save</item>
                <item name="label" xsi:type="string" translate="true">Save</item>
                <item name="class" xsi:type="string">save primary</item>
                <item name="url" xsi:type="string">*/*/save</item>
            </item>
        </item>
        <item name="template" xsi:type="string">templates/form/collapsible</item>
    </argument>
    <dataSource name="namespace_module_form_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">[Namespace]\[Module]\Model\DataProvider</argument>
            <argument name="name" xsi:type="string">namespace_module_form_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">id</argument>
            <argument name="requestFieldName" xsi:type="string">id</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="submit_url" xsi:type="url" path="test/test/save/"/>
                </item>
            </argument>
        </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>
    </dataSource>
    <fieldset name="category_duplicate_details">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="collapsible" xsi:type="boolean">false</item>
                <item name="label" xsi:type="string" translate="true">Test Options</item>
                <item name="sortOrder" xsi:type="number">10</item>
            </item>
        </argument>
        <field name="test" formElement="select">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="source" xsi:type="string">Test Field</item>
                    <item name="default" xsi:type="string">0</item>
                </item>
            </argument>
            <settings>
                <dataType>boolean</dataType>
                <label translate="true">Test Field</label>
                <dataScope>test</dataScope>
            </settings>
            <formElements>
                <select>
                    <settings>
                        <options class="Magento\Config\Model\Config\Source\Yesno"/>
                    </settings>
                </select>
            </formElements>
        </field>            
    </fieldset>
</form>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.