I'm developing a Magento2 extension that has an admin grid which is generated using the UI Listing Component. The grid shows the records (a list of blog items) just fine. The extension allows to save blog items for specific store views which saves the blog_id together with the store_id in a separate database table. Now what I would like to do is show a column in the grid with blog items that shows the store views selected for each blog item.
The whole setup is quite similar to the CMS pages and cms_page_listing.xml. There is a column in my blog_listing.xml for the store view like this:
<column name="store_id" class="Magento\Store\Ui\Component\Listing\Column\Store">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>
<item name="sortable" xsi:type="boolean">false</item>
<item name="label" xsi:type="string" translate="true">Store View</item>
</item>
</argument>
</column>
When loading the grid the following error is shown: "Notice: Undefined index: store_id in ..\vendor\magento\module-store\Ui\Component\Listing\Column\Store.php on line 82"
Obviously there is no store_id in the default collection of the blog items as it's connected through another table with the actual store_id's. But my collection looks like this and it should be in there: app\code\vendor\module\Model\ResourceModel\AbstractCollection.php
protected function performAfterLoadBlog($tableName, $columnName) {
$items = $this->getColumnValues($columnName);
if (count($items)) {
$connection = $this->getConnection();
$select = $connection->select()->from(['blog_entity_store' => $this->getTable($tableName)])
->where('blog_entity_store.' . $columnName . ' IN (?)', $items);
$result = $connection->fetchPairs($select);
if ($result) {
foreach ($this as $item) {
$entityId = $item->getData($columnName);
if (!isset($result[$entityId])) {
continue;
}
if ($result[$entityId] == 0) {
$stores = $this->storeManager->getStores(false, true);
$storeId = current($stores)->getId();
$storeCode = key($stores);
} else {
$storeId = $result[$item->getData($columnName)];
$storeCode = $this->storeManager->getStore($storeId)->getCode();
}
$item->setData('_first_store_id', $storeId);
$item->setData('store_code', $storeCode);
$item->setData('store_id', [$result[$entityId]]);
}
}
}
}
protected function joinStoreRelationTable($tableName, $columnName) {
if ($this->getFilter('store')) {
$this->getSelect()->join(
['store_table' => $this->getTable($tableName)],
'main_table.' . $columnName . ' = store_table.' . $columnName,
[]
)->group(
'main_table.' . $columnName
);
}
parent::_renderFiltersBefore();
}
\app\code\vendor\module\Model\ResourceModel\Blog\Collection.php
protected function _afterLoad() {
$this->performAfterLoadBlog('vendor_module_store', 'blog_id');
$this->_previewFlag = false;
return parent::_afterLoad();
}
protected function _renderFiltersBefore() {
$this->joinStoreRelationTable('vendor_module_store', 'blog_id');
}
So my question is, how do I go from here so the store_id column can be rendered with the correct store views?