0

I've got an admin ui form with category tree field

https://github.com/DominicWatts/Announce/blob/429e2f6228b5911f261df6f6855d76913c934f9a/view/adminhtml/ui_component/xigen_announce_group_form.xml#L159-L189

        <field name="category" component="Magento_Catalog/js/components/new-category" sortOrder="60" 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">true</item>
                    <item name="showCheckbox" xsi:type="boolean">true</item>
                    <item name="disableLabel" xsi:type="boolean">true</item>
                    <item name="levelsVisibility" xsi:type="number">1</item>
                </item>
            </argument>
            <settings>
                <required>false</required>
                <validation>
                    <rule name="required-entry" xsi:type="boolean">false</rule>
                </validation>
                <elementTmpl>ui/grid/filters/elements/ui-select</elementTmpl>
                <label translate="true">Category</label>
                <dataScope>category</dataScope>
                <componentType>field</componentType>
                <listens>
                    <link name="${ $.namespace }.${ $.namespace }:responseData">setParsed</link>
                </listens>
            </settings>
            <formElements>
                <select>
                    <settings>
                        <options class="Magento\Catalog\Ui\Component\Product\Form\Categories\Options"/>
                    </settings>
                </select>
            </formElements>
        </field>

I've got the field populating and data is saving correctly

The remaining issue is that the field appears blank on load

enter image description here

But sorts itself out when you click

enter image description here

I've missed a step. Any ideas?

  • 1
    The ui_component code seems correct, you should be check in Xigen\Announce\Model\Group\DataProvider file. Please check on this file on getData() method which is render or not your categories data. – Siranjeevi K S May 25 at 2:26
0

Comment on question pointed me in right direction. Worth highlighted that this is because I'm using a multiselect. Single i.e. <item name="multiple" xsi:type="boolean">false</item> no issue.

XML and db data is correct

Had to update Xigen\Announce\Model\Group\DataProvider::getData()

My form is tabbed. So additional layer here. But went from this...

    /**
     * Get data
     * @return array
     */
    public function getData()
    {
        if (isset($this->loadedData)) {
            return $this->loadedData;
        }
        $items = $this->collection->getItems();
        foreach ($items as $model) {
            $this->loadedData[$model->getId()][Data::GROUP_TAB] = $model->getData();
        }
        $data = $this->dataPersistor->get('xigen_announce_group');

        if (!empty($data)) {
            $model = $this->collection->getNewEmptyItem();
            $model->setData($data);
            $this->loadedData[$model->getId()][Data::GROUP_TAB] = $model->getData();
            $this->dataPersistor->clear('xigen_announce_group');
        }

        return $this->loadedData;
    }

To this...

    /**
     * Get data
     * @return array
     */
    public function getData()
    {
        if (isset($this->loadedData)) {
            return $this->loadedData;
        }
        $items = $this->collection->getItems();
        foreach ($items as $model) {
            $form = $this->loadedData[$model->getId()][Data::GROUP_TAB] = $model->getData();
            $form['category'] = explode(',', $form['category']);
        }
        $data = $this->dataPersistor->get('xigen_announce_group');

        if (!empty($data)) {
            $model = $this->collection->getNewEmptyItem();
            $model->setData($data);
            $this->loadedData[$model->getId()][Data::GROUP_TAB] = $model->getData();
            $this->dataPersistor->clear('xigen_announce_group');
        } else {
            if ($items) {
                if ($model->getData('category')) {
                    $group[$model->getId()][Data::GROUP_TAB] = $form;
                    return $group;
                }
            }
        }

        return $this->loadedData;
    }

This answer helped https://magento.stackexchange.com/a/270807/70343

| improve this answer | |

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.