I have a form like this. This is used to save a new custom admin. But I have a problem with showing a value in the store_id field while editing.
While showing this form the store field must be selected. The name of this field is store_id but I have to show the name of the store instead. so I need to make a class for getting the store's name. I can get all the names of the stores and use them for the dropdown list. But after seeing the detail when editing, the store_id field is empty and doesn't even show the store_id or the store_name. However, the store_id can be saved to the database.
This is the part for the store_id field at admin_form.xml
<field name="store_id">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">Module\Vendor\Model\Select\Source\Store</item>
<item name="config" xsi:type="array">
<item name="componentType" xsi:type="string">field</item>
<item name="formElement" xsi:type="string">select</item>
<item name="label" xsi:type="string">Store</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="dataType" xsi:type="string">text</item>
<item name="source" xsi:type="string">Customer</item>
<item name="dataScope" xsi:type="string">store_id</item>
</item>
</argument>
</field>
and this is the class for showing the name of the store
<?php
class Store extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
/**
* @var \Vendor\Module\Model\CustomFactory
*/
private $customFactory;
/**
* @param \Magento\Customer\Model\CustomerFactory $customerFactory
*/
public function __construct(\Module\Vendor\Model\ResourceModel\Store\CollectionFactory $customFactory)
{
$this->customFactory = $customFactory;
}
/**
* Get all options
*
* @return array
*/
public function getAllOptions()
{
$customCollection = $this->customFactory->create();
$customCollection->addFieldToFilter('status', 'Active');
$customCollection->setOrder('store_name', 'ASC');
$this->_options = [['label' => 'Please select', 'value' => '']];
foreach ($customCollection as $custom) {
$this->_options[] = ['label' => $custom->getData('store_name'), 'value' => $custom->getData('id')];
}
return $this->_options;
}
}
How can I make this field is selected based on the store_id and the selected field shows the store's name?