1

I am trying to create a admin UI form in my custom module. But, when I am in edit form, I can't see my for elements populated by my module db table. Suppose, there is a 'title' column in my table and it contains data but I can't populate that title textbox from db table entry.

Here is my DataProvider and UI form xml: Please help. Thanks in advance.

Module namespace: CompanyName Module name: MenuManager

DataProvider.php

class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{ 

    protected $_loadedData; 
    /**
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param CollectionFactory $employeeCollectionFactory
     * @param array $meta
     * @param array $data
     */
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $menumanagerCollectionFactory,
        array $meta = [],
        array $data = []
    ) {
        $this->collection = $menumanagerCollectionFactory->create(); 
       parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
    } 

    /*public function addFilter(\Magento\Framework\Api\Filter $filter) {
        return null; 
    } */

    /**
     * Get data  
     *
     * @return array
     */
    public function getData()
    {

        if (isset($this->_loadedData)) {
            return $this->_loadedData;
        }

        $items = $this->collection->getItems();
        $ruleId = '';
        foreach ($items as $page) {
            $this->loadedData[$page->getId()] = $page->getData();
            //$ruleId = $page->getId();
        } 
        return $this->loadedData;
    }
}

companyname_menumanager_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">companyname_menumanager_form.companyname_menumanager_form_data_source</item>
            <item name="deps" xsi:type="string">companyname_menumanager_form.companyname_menumanager_form_data_source</item>
        </item>
        <item name="label" xsi:type="string" translate="true">Employee Information</item>
        <item name="config" xsi:type="array">
            <item name="dataScope" xsi:type="string">data</item>
            <item name="menumanager" xsi:type="string">companyname_menumanager_form</item>
        </item>
        <item name="buttons" xsi:type="array">
            <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">primary</item>
            </item>
        </item>
    </argument>
    <settings>
        <layout>
            <navContainerName>left</navContainerName>
            <type>tabs</type>
        </layout>
        <deps>
            <dep>companyname_menumanager_form.companyname_menumanager_form_data_source</dep>
        </deps>
    </settings>
    <dataSource name="categorytree_form_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">CompanyName\MenuManager\Model\DataProvider</argument>
            <argument name="name" xsi:type="string">companyname_menumanager_form_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 name="submit_url" path="*/*/save" xsi:type="url" />
            </item>
        </argument>
    </dataSource>
    <fieldset name="categorytree_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">Categories</item>
                <item name="sortOrder" xsi:type="number">20</item>
            </item>
        </argument>
        <field name="title" formElement="input">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="source" xsi:type="string">title</item>
                    <item name="dataType" xsi:type="string">text</item>
                </item>
            </argument>
            <settings>
                <dataType>text</dataType>
                <visible>true</visible>
            </settings>
        </field>
        <field name="id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <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">id</item>
                </item>
            </argument>
        </field>
        <field name="categories" component="CompanyName_MenuManager/js/select-category" sortOrder="20" formElement="select">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filterOptions" xsi:type="boolean">true</item>
                    <item name="multiple" xsi:type="boolean">false</item>
                    <item name="showCheckbox" xsi:type="boolean">false</item>
                    <item name="disableLabel" xsi:type="boolean">true</item>
                </item>
            </argument>
            <settings>
                <required>true</required>
                <validation>
                    <rule name="required-entry" xsi:type="boolean">true</rule>
                </validation>
                <elementTmpl>ui/grid/filters/elements/ui-select</elementTmpl>
                <label translate="true">Select Category</label>
                <dataScope>data.categories</dataScope>
                <componentType>field</componentType>
                <listens>
                    <link name="${ $.companyname }.${ $.companyname }:responseData">setParsed</link>
                </listens>
            </settings>
            <formElements>
                <select>
                    <settings>
                        <options class="CompanyName\MenuManager\Ui\Component\Form\Category\Options"/>
                    </settings>
                </select>
            </formElements>
        </field>    


    </fieldset>

</form>
0

In DataProvider.php change below line with my answer:

foreach ($items as $page) {
        $this->loadedData['categorytree_details'][$page->getId()] = $page->getData();
        //$ruleId = $page->getId();
    } 

when your ui form left side tabs you add you have return data with field set

if you remove below code from your companyname_menumanager_form.xml file

      <layout>
        <navContainerName>left</navContainerName>
        <type>tabs</type>
    </layout>

then no need to add filedset in dataprovider.

so just add filedset whenever you require leftside tab in your form.

  • there was problem in form xml file which was related to tag 'dataScope'. I already fixed it. Thanks for your answer though. – Mrinal Chakraborty Oct 22 '19 at 12:26

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.