DataProvider.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogRule\Model\Rule;
  7. use Magento\CatalogRule\Model\ResourceModel\Rule\Collection;
  8. use Magento\CatalogRule\Model\ResourceModel\Rule\CollectionFactory;
  9. use Magento\CatalogRule\Model\Rule;
  10. use Magento\Framework\App\Request\DataPersistorInterface;
  11. /**
  12. * Class DataProvider
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
  16. {
  17. /**
  18. * @var Collection
  19. */
  20. protected $collection;
  21. /**
  22. * @var array
  23. */
  24. protected $loadedData;
  25. /**
  26. * @var DataPersistorInterface
  27. */
  28. protected $dataPersistor;
  29. /**
  30. * @param string $name
  31. * @param string $primaryFieldName
  32. * @param string $requestFieldName
  33. * @param CollectionFactory $collectionFactory
  34. * @param DataPersistorInterface $dataPersistor
  35. * @param array $meta
  36. * @param array $data
  37. */
  38. public function __construct(
  39. $name,
  40. $primaryFieldName,
  41. $requestFieldName,
  42. CollectionFactory $collectionFactory,
  43. DataPersistorInterface $dataPersistor,
  44. array $meta = [],
  45. array $data = []
  46. ) {
  47. $this->collection = $collectionFactory->create();
  48. $this->dataPersistor = $dataPersistor;
  49. parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
  50. }
  51. /**
  52. * @return array
  53. */
  54. public function getData()
  55. {
  56. if (isset($this->loadedData)) {
  57. return $this->loadedData;
  58. }
  59. $items = $this->collection->getItems();
  60. /** @var Rule $rule */
  61. foreach ($items as $rule) {
  62. $rule->load($rule->getId());
  63. $this->loadedData[$rule->getId()] = $rule->getData();
  64. }
  65. $data = $this->dataPersistor->get('catalog_rule');
  66. if (!empty($data)) {
  67. $rule = $this->collection->getNewEmptyItem();
  68. $rule->setData($data);
  69. $this->loadedData[$rule->getId()] = $rule->getData();
  70. $this->dataPersistor->clear('catalog_rule');
  71. }
  72. return $this->loadedData;
  73. }
  74. }