1

I've custom model same as Core Code. (Vendor)

../Model/Grid.php

class Grid extends \Magento\Framework\Model\AbstractModel implements 
GridInterface
{

const CACHE_TAG = 'customer_entity';


protected $_cacheTag = 'customer_entity';


protected $_eventPrefix = 'customer_entity';


protected function _construct()
{
    $this->_init('{Vendor}\{Module}\Model\ResourceModel\Grid');
}

public function getEntityId()
{
    return $this->getData(self::ENTITY_ID);
}


public function setEntityId($entityId)
{
    return $this->setData(self::ENTITY_ID, $entityId);
}


public function getFirstname()
{
    return $this->getData(self::FIRSTNAME);
}


public function setFirstname($firstName)
{
    return $this->setData(self::FIRSTNAME, $firstName);
}


public function getLastname()
{
    return $this->getData(self::LASTNAME);
}


public function setLastname($lastName)
{
    return $this->setData(self::LASTNAME, $lastName);
}

public function getGroupId()
{
    return $this->getData(self::GROUP_ID);
}


public function setGroupId($groupId)
{
    return $this->setData(self::GROUP_ID, $groupId);
}
}

Below is my interface

interface GridInterface
{
 const ENTITY_ID = 'entity_id';
 const LASTNAME = 'lastname';
 const GROUP_ID = 'group_id';

 public function getEntityId();
 public function setEntityId($entityId);

 public function getLastname();
 public function setLastname($lastName);

 public function getGroupId();
 public function setGroupId($groupId);
}

When i am dumping model data in block form i am getting everything smilar as

\Magento\Customer\Api\CustomerRepositoryInterface

Note : I've added group_id for test purpose. And group_id is rendering to form Not Last Name.

I've cheked DB i think there's Index in customer_entity table for firstname and lastname that's why it can't render to form.

Check this.

customer_entity

What's solution for this ?? Any Help would be really appreciated.

If you need more details than Refer this: Here

1

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);
      }
    }
}
| 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.