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.

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".

Your Answer

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

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