I'm trying to show the selected options in a multiselect in a custom admin module form. The data for the multi select field is saved in the database.
What i have as field:
<field name="position_options">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">{Vendor}\{Module}\Model\Form\Source\OptionsMultiselect</item>
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">text</item>
<item name="label" translate="true" xsi:type="string">Positions</item>
<item name="formElement" xsi:type="string">multiselect</item>
</item>
</argument>
</field>
If i add:
<item name="default" xsi:type="string">0,1,2</item>
Between it will hightlight fields with corresponding id's.
But how do i get the data from the database in there? Or do i need to do it in another way?
Thanks in advance!
<argument name="class" xsi:type="string">{Vendor}\{Module}\Model\DataProvider</argument>
class DataProvider extends AbstractDataProvider
{
/**
* @var ResourceModel\Data\Collection
*/
protected $collection;
/**
* @var DataPersistorInterface
*/
protected $dataPersistor;
/**
* @var array
*/
protected $loadedData;
/**
* @param string $name
* @param string $primaryFieldName
* @param string $requestFieldName
* @param CollectionFactory $pageCollectionFactory
* @param DataPersistorInterface $dataPersistor
* @param array $meta
* @param array $data
*/
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
CollectionFactory $pageCollectionFactory,
DataPersistorInterface $dataPersistor,
array $meta = [],
array $data = []
) {
$this->collection = $pageCollectionFactory->create();
$this->dataPersistor = $dataPersistor;
$this->meta = $this->prepareMeta($this->meta);
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
}
/**
* Prepares Meta
*
* @param array $meta
* @return array
*/
public function prepareMeta(array $meta)
{
return $meta;
}
/**
* Get data
*
* @return array
*/
public function getData()
{
if (isset($this->loadedData)) {
return $this->loadedData;
}
$items = $this->collection->getItems();
foreach ($items as $page) {
$this->loadedData[$page->getId()] = $page->getData();
}
$data = $this->dataPersistor->get('module_messages');
if (!empty($data)) {
$page = $this->collection->getNewEmptyItem();
$page->setData($data);
$this->loadedData[$page->getId()] = $page->getData();
$this->dataPersistor->clear('module_messages');
}
return $this->loadedData;
}
}
Didn't change it in here. Added
protected function _getLoadSelect($field, $value, $object)
{
$select = parent::_getLoadSelect($field, $value, $object);
$select
->joinLeft(
[
'rel' => $this->getTable('shirtsbedrukken_overprinttable_data_options_rel'),
],
'shirtsbedrukken_overprinttable_data.sod_id = rel.sod_id',
['rel.soo_id']
)
->joinLeft(
[
'soo' => $this->getTable('shirtsbedrukken_overprinttable_options'),
],
'rel.soo_id = soo.soo_id',
['soo_id']
);
return $select;
}
Inside \Model\ResourceModel
Was is in the wrong place?