I'm trying to understand uiForm Component. I'm stucked in My module settings page. I created database table with settings in one row. And now I must to get default data and show them in Form. My main questions:
- How get data from table and show it in e.g. input field?
- What structure should have DataProvider::getData() return array?
- When I even use die() in getData() function nothings happened. It isn't executed? why?
- In documentation is
js_config -> config -> provider - specifies the name of the component data.
but it isn't working . I gets error in console.log. It's (working?) for js_config -> provider like is now in my file.
Table name is: correctemail_settings
Below are my example files: settings_form.xml
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/Ui/etc/ui_configuration.xsd">
<argument name="data" xsi:type="array">
<item name="js_config" xsi:type="array">
<item name="provider" xsi:type="string">settings_form.settings_form_data_source</item>
<item name="deps" xsi:type="string">settings_form.settings_form_data_source</item>
</item>
<item name="label" xsi:type="string" translate="true">Sample Form</item>
<item name="layout" xsi:type="array">
<item name="type" xsi:type="string">tabs</item>
</item>
</argument>
<dataSource name="settings_form_data_source">
<argument name="dataProvider" xsi:type="configurableObject">
<argument name="class" xsi:type="string">iDesign\Correctemail\Model\Settings\DataProvider</argument>
<argument name="name" xsi:type="string">settings_form_data_source</argument>
<argument name="primaryFieldName" xsi:type="string">settings_id</argument>
<argument name="requestFieldName" xsi:type="string">settings_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="settings">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Change Settings</item>
</item>
</argument>
<!-- This field represents form id and is hidden -->
<field name="settings_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="dataScope" xsi:type="string">settings_id</item>
<item name="source" xsi:type="string">correctemail_settings</item>
</item>
</argument>
</field>
<!-- This field has data type 'text' and standard 'input' form element and looks like input -->
<field name="formId">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string">Some text</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">correctemail_settings</item>
<item name="dataScope" xsi:type="string">formId</item>
</item>
</argument>
</field>
</fieldset>
DataProvider.php
namespace iDesign\Correctemail\Model\Settings;
use \Magento\Ui\DataProvider\AbstractDataProvider;
use \iDesign\Correctemail\Model\ResourceModel\Settings\Collection;
class DataProvider extends AbstractDataProvider
{
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
Collection $settingsCollection,
array $meta = [],
array $data = []
) {
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
$this->collection = $settingsCollection;
}
public function getData()
{
/*if (!$this->getCollection()->isLoaded()) {
$this->getCollection()->load(1);
}
$items = $this->getCollection()->getData();*/
return ['settings' =>[
'settings_id' => 1,
'formId' => 'exampleValue']];
}
}
As you see I even tried to return static data to understand how it should be defined.
-
1Try looking at a core example such as CMS blocks and copy from that.Aaron Allen– Aaron Allen2016年09月22日 08:10:03 +00:00Commented Sep 22, 2016 at 8:10
-
In magento2/sample_form there aren't any data provided into form. Others like product form in magento uses also grid/list (don't remember exactly) it is a bit to complex for my needs. I'm looking here for help because I doesn't find any solution. in core/github/stack/documentation. My main problem is to understand dependencies between uiForm / xml / DataProvider / and js template creating. If you have any specific materials it will be helpfullRadek Radzik– Radek Radzik2016年09月22日 08:54:48 +00:00Commented Sep 22, 2016 at 8:54
1 Answer 1
My main problem that DataProvider::getData() function weren't executed was because it didn't know which row to populate. When I added /settings_id/1/ into an url then this function was called.
And return value should be set like this:
return [1 => ['settings' => $item]]
where
[$selectedId => [$fieldsetName => $ArrayOfitemValues]]
I hope it helps somebody.
-
but how do you know the id of the settings to return in the getData() function. I saw examples iterating over all entries which doesnt make sense like here webkul.com/blog/create-ui-form-magento2-part-2Yehia A.Salam– Yehia A.Salam2017年09月29日 16:34:47 +00:00Commented Sep 29, 2017 at 16:34
-
The collection of the
DataProvidergets filtered by id from url on github.com/magento/magento2/blob/… .Quisse– Quisse2018年12月04日 11:31:14 +00:00Commented Dec 4, 2018 at 11:31
Explore related questions
See similar questions with these tags.