0

I want to set a default value for an uiComponent field (shipping_id in this case) from the DataProvider (not from the form's XML, but from PHP). This is because the default value is dynamic (i am creating a new record based on a shipping id).

In my DataProvider:

    public function getData()
    {

        foreach ($this->collection->getItems() as $item) {
            $this->_loadedData[$item->getId()]['general'] = $item->getData();
        }

        if (empty($this->_loadedData)) {
            //set default values

            /** @var ShipmentInterface $shipment */
            $shipment = $this->persistor->get('shipment');
            $this->_loadedData['general']['shipment_id'] = $shipment->getEntityId();
        }

        return $this->_loadedData;
    }

and in my form's XML i am having a fieldset:

    <fieldset name="general">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">General</item>
            </item>
        </argument>
        <field name="shipment_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">number</item>
                    <item name="label" xsi:type="string" translate="true">Shipment ID</item>
                    <item name="formElement" xsi:type="string">hidden</item>
                </item>
            </argument>
        </field>
        ...
    </fieldset>

Anyway, the code above from the DataProvider is working partially, it loads existing data, but when adding, the hidden field has no value. Any idea how to reference a field from a fieldset when adding/editing data?

What should be the structure of $this->_loadedData when adding/editing data?

4
  • I don't really understand your requirement but if you want to get value from another field you can use imports in the XML, you will get dynamic value if the field you want to get is observable devdocs.magento.com/guides/v2.4/ui_comp_guide/concepts/…
    – Jimmy
    Mar 30, 2021 at 1:59
  • Hi Jimmy, i want to set an initial value for a field (ui-component) from PHP. I am blocked at this moment as i don't understand what should be the structure of the _loadedData (the variable that is sent from my DataProvider to the form)
    – Robert
    Mar 31, 2021 at 6:00
  • I don't quite see the "default value" use case. The reason is when opening a grid page, the value will be populated from database, the value is in the database.
    – Jimmy
    Mar 31, 2021 at 7:42
  • So if you want to make a place holder, you can use JS to do it. I'm not sure if XML/PHP can do the job
    – Jimmy
    Mar 31, 2021 at 7:42

0

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.