DataProvider.php 2.2 KB

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