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?

SagarPPanchal
3821 gold badge3 silver badges19 bronze badges
asked Mar 29, 2021 at 14:53
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/… Commented 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) Commented 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. Commented 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 Commented Mar 31, 2021 at 7:42

1 Answer 1

0

You can set a default value like this:

$this->_loadedData['']['general']['shipment_id'] = $shipment->getEntityId();

$this->_loadedData is indexed by the entity's id. A new entity doesn't have yet an id, so whatever is set in $this->_loadedData[''] it will be loaded for new entities.

answered Jan 23, 2024 at 14:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.