Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.8 kB
1
Indexable
Never
<?php

namespace Isobar\Bai22\Model\Staff;

use Isobar\Bai22\Model\ResourceModel\Staff\CollectionFactory;
use Magento\Framework\App\Request\DataPersistorInterface;

class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{

    protected $collection;
    protected $dataPersistor;
    protected $loadedData;
    protected $storeManager;

    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $staffCollectionFactory,
        DataPersistorInterface $dataPersistor,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        array $meta = [],
        array $data = []
    )
    {
        $this->collection = $staffCollectionFactory->create();
        $this->dataPersistor = $dataPersistor;
        $this->storeManager = $storeManager;
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
        $this->meta = $this->prepareMeta($this->meta);
    }

    /**
     * Prepares Meta
     */
    public function prepareMeta(array $meta)
    {
        return $meta;
    }

    /**
     * Get data
     */
    public function getData()
    {
        // Get items
        if (isset($this->loadedData)) {
            return $this->loadedData;
        }
        $items = $this->collection->getItems();


        foreach ($items as $staff) {
            $data = $staff->getData();


            $this->loadedData[$staff->getId()] = $data;
        }

        $data = $this->dataPersistor->get('staff');
        if (!empty($data)) {
            $staff = $this->collection->getNewEmptyItem();
            $staff->setData($data);
            $this->loadedData[$staff->getId()] = $staff->getData();
            $this->dataPersistor->clear('staff');
        }

        return $this->loadedData;
    }
}