Model/Template/DataProvider.php
<?php
/**
* Copyright © EAdesign by Eco Active S.R.L.,All rights reserved.
* See LICENSE for license details.
*/
namespace Eadesigndev\Pdfgenerator\Model\Template;
use Eadesigndev\Pdfgenerator\Model\ResourceModel\Pdfgenerator\CollectionFactory as templateCollectionFactory;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Ui\DataProvider\AbstractDataProvider;
/**
* Class DataProvider
*/
class DataProvider extends AbstractDataProvider
{
/**
* @var \Eadesigndev\Pdfgenerator\Model\ResourceModel\Pdfgenerator\Collection
*/
public $collection;
/**
* @var DataPersistorInterface
*/
private $dataPersistor;
/**
* @var array
*/
private $loadedData;
/**
* @param string $name
* @param string $primaryFieldName
* @param string $requestFieldName
* @param templateCollectionFactory $templateCollectionFactory
* @param DataPersistorInterface $dataPersistor
* @param array $meta
* @param array $data
*/
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
templateCollectionFactory $templateCollectionFactory,
DataPersistorInterface $dataPersistor,
array $meta = [],
array $data = []
) {
$this->collection = $templateCollectionFactory->create();
$this->dataPersistor = $dataPersistor;
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
$this->meta = $meta;
}
/**
* Get data
*
* @return array
*/
public function getData()
{
if (isset($this->loadedData)) {
return $this->loadedData;
}
$templates = $this->collection->getItems();
/** @var $template \Eadesigndev\Pdfgenerator\Model\Pdfgenerator */
foreach ($templates as $template) {
$this->loadedData[$template->getId()] = $template->getData();
}
$data = $this->dataPersistor->get('pdfgenerator_template');
if (!empty($data)) {
$template = $this->collection->getNewEmptyItem();
$template->setData($data);
$this->loadedData[$template->getId()] = $template->getData();
$this->dataPersistor->clear('pdfgenerator_template');
}
return $this->loadedData;
}
}