Magento 2 – How to Set Data for Field in Form UI

data-providermagento2uicomponent

Is there a right way pass data (request value) to form ui-component?
reproduce-image
I have try to setData from DataProvider define in form-ui.xml but not work. After form load complete this field must be populated data (initial value)

/**
 * Get data from provider and populate
 * @param void
 * @return array
 */
public function getData()
{

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

    if ($itemId)
        $items = $this->collection->getItems();

    $data = $this->dataPersistor->get('form_item');
    if (!empty($data)) {
       //Do something
    } else {
       //When form init without any data
       // I want to setData for some field here
    }

    return $this->loadedData;
}

My current question:
1 – How to setData in the right way for ui way? If my way not right is there a better way to do setdata for field
2 – Side question: How core-team can make magento find exactly which form to interactive. In old way form , we can define form original with form tag, htmlIdPrefix … etc But in new form i can't find anywhere in uiform.xml to configuration id form, form prefix .. I am curious how it work

It will be great if know about it

Best Answer

In your custom_form.xml file

<field name="your_field_id">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">{Vendorname}\{Modulename}\Model\Classname</item>
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Name label</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="source" xsi:type="string">[source_here]</item>
                    <item name="dataScope" xsi:type="string">your_field_id</item>
                    <item name="caption" xsi:type="string" translate="true">-- Please Select --</item>
                </item>
            </argument>
        </field>

    In above -- Please Select -- is set as default custom message for dropdown.

Where you can set select dynamic option from this class,

{Vendorname}\{Modulename}\Model\Classname using toOptionArray() method.

In DataProvider.php file,

<?php
namespace Yourmodule\Modulename\Model\Path;

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

public function __construct(        
        \Magento\Framework\App\RequestInterface $request        
    ) {
        $this->_request = $request;
    }



public function getData()
{
    if (isset($this->loadedData)) {
        return $this->loadedData;
    }

    $itemId = $this->_request->getParam('page_id');

    if ( !empty($itemId) ) {
    $items = $this->collection->getItems();
    foreach ($items as $item) {
        $this->loadedData[$item->getId()] = $item->getData();
    }

    $data = $this->dataPersistor->get('form_item');

    if (!empty($data)) {
       //Do something
       $this->loadedData[$item->getId()] = $item->getData();
    } 
    return $this->loadedData;
}

}

Another way to get data Using UI component.

If you want get dafault value from another module using UI component

Add this field in your custom_form.xml file

<item name="options" xsi:type="object">{Vendorname}\{Modulename}\Ui\Component\Form\Element\DataProvider</item>

In DataProvider.php file,

<?php
namespace {Vendorname}\{Modulename}\Ui\Component\Form\Element;
class DataProvider extends \Magento\Ui\Component\Form\Element\Input
{
  /**
     * Prepare component configuration
     *
     * @return void
     */
    public function prepare()
    {
      parent::prepare();

      $customValue = {Get Custom value};

      $config = $this->getData('config');

      if(isset($config['dataScope']) && $config['dataScope']=='your_field_id'){
        $config['default']= $customValue;
        $this->setData('config', (array)$config);
      }
    }
}