I am actually trying to create a really simple grid (for now) listing files from server.
The collection work well, I take as example Magento_Backup
module but for the grid the module is using the old way with class and not the UI components
.
My collection class \Foo\Bar\Model\Log\Collection
is extending \Magento\Framework\Data\Collection\Filesystem
to construct file collection.
I defined a new type in my di.xml
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
<arguments>
<argument name="collections" xsi:type="array">
<item name="foo_bar_log_listing_data_source" xsi:type="string">Foo\Bar\Model\Log\Collection</item>
</argument>
</arguments>
</type>
Here is my ui_component
named foo_bar_log_listing.xml
<?xml version="1.0"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<argument name="data" xsi:type="array">
<item name="js_config" xsi:type="array">
<item name="provider" xsi:type="string">foo_bar_log_listing.foo_bar_log_listing_data_source</item>
<item name="deps" xsi:type="string">foo_bar_log_listing.foo_bar_log_listing_data_source</item>
</item>
<item name="spinner" xsi:type="string">foo_bar_log_columns</item>
</argument>
<dataSource name="foo_bar_log_listing_data_source">
<argument name="dataProvider" xsi:type="configurableObject">
<argument name="class" xsi:type="string">Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider</argument>
<argument name="name" xsi:type="string">foo_bar_log_listing_data_source</argument>
<argument name="primaryFieldName" xsi:type="string">code</argument>
<argument name="requestFieldName" xsi:type="string">code</argument>
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
<item name="update_url" xsi:type="url" path="mui/index/render"/>
<item name="storageConfig" xsi:type="array">
<item name="indexField" xsi:type="string">code</item>
</item>
</item>
</argument>
</argument>
</dataSource>
<listingToolbar name="listing_top">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="sticky" xsi:type="boolean">true</item>
</item>
</argument>
<bookmark name="bookmarks"/>
<columnsControls name="columns_controls"/>
<filterSearch name="fulltext"/>
<filters name="listing_filters" />
<paging name="listing_paging"/>
</listingToolbar>
<columns name="foo_bar_log_columns">
<column name="code">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Code</item>
</item>
</argument>
</column>
<column name="file">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">>
<item name="filter" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">File</item>
</item>
</argument>
</column>
</columns>
</listing>
Here is my collection class
<?php
namespace Foo\Bar\Model\Log;
use Magento\Framework\Data\Collection\EntityFactoryInterface;
class Collection extends FilesystemCollection
{
/**
* Filenames regex filter
*
* @var string
*/
protected $_allowedFilesMask = '/^[a-z0-9\.\-\_]+[bar]\.log/i';
/**
* @var \Foo\Bar\Helper\Log $logHelper
*/
private $logHelper;
/**
* @var \Foo\Bar\Helper\Adminhtml\Log $adminLogHelper
*/
private $adminLogHelper;
public function __construct(
EntityFactoryInterface $entityFactory,
\Foo\Bar\Helper\Log $logHelper,
\Foo\Bar\Helper\Adminhtml\Log $adminLogHelper
) {
$this->logHelper = $logHelper;
$this->adminLogHelper = $adminLogHelper;
parent::__construct($entityFactory);
/** @var string $logDirectory */
$logDirectory = $this->logHelper->getLogDirectoryPath();
$this->setCollectRecursively(true);
$this->addTargetDir($logDirectory);
}
/**
* @param string $filename
*
* @return array
*/
protected function _generateRow($filename)
{
/** @var array $row */
$row = parent::_generateRow($filename);
$row = $this->adminLogHelper->generateRow($row);
return $row;
}
}
I understand that the dataSource
is refering my \Foo\Bar\Log\Collection
class with his arguments and for basics entities it is a VirtualType
of \Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
So my questions are
- Is it possible to create a file listing grid with UI Component as basic entities ?
- Which classes do I have to use in di.xml
/ foo_bar_log_listing.xml
classes ?
Thank you.
Collection
class ? Which part of the module do you need ?var/log
path had to be returned by$logDirectory = $this->logHelper->getLogDirectoryPath();
or any other function / class in the Collection class