DataProvider.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cms\Model\Page;
  7. use Magento\Cms\Model\ResourceModel\Page\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\Page\Collection
  17. */
  18. protected $collection;
  19. /**
  20. * @var DataPersistorInterface
  21. */
  22. protected $dataPersistor;
  23. /**
  24. * @var array
  25. */
  26. protected $loadedData;
  27. /**
  28. * @param string $name
  29. * @param string $primaryFieldName
  30. * @param string $requestFieldName
  31. * @param CollectionFactory $pageCollectionFactory
  32. * @param DataPersistorInterface $dataPersistor
  33. * @param array $meta
  34. * @param array $data
  35. * @param PoolInterface|null $pool
  36. */
  37. public function __construct(
  38. $name,
  39. $primaryFieldName,
  40. $requestFieldName,
  41. CollectionFactory $pageCollectionFactory,
  42. DataPersistorInterface $dataPersistor,
  43. array $meta = [],
  44. array $data = [],
  45. PoolInterface $pool = null
  46. ) {
  47. $this->collection = $pageCollectionFactory->create();
  48. $this->dataPersistor = $dataPersistor;
  49. parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data, $pool);
  50. $this->meta = $this->prepareMeta($this->meta);
  51. }
  52. /**
  53. * Prepares Meta
  54. *
  55. * @param array $meta
  56. * @return array
  57. */
  58. public function prepareMeta(array $meta)
  59. {
  60. return $meta;
  61. }
  62. /**
  63. * Get data
  64. *
  65. * @return array
  66. */
  67. public function getData()
  68. {
  69. if (isset($this->loadedData)) {
  70. return $this->loadedData;
  71. }
  72. $items = $this->collection->getItems();
  73. /** @var $page \Magento\Cms\Model\Page */
  74. foreach ($items as $page) {
  75. $this->loadedData[$page->getId()] = $page->getData();
  76. }
  77. $data = $this->dataPersistor->get('cms_page');
  78. if (!empty($data)) {
  79. $page = $this->collection->getNewEmptyItem();
  80. $page->setData($data);
  81. $this->loadedData[$page->getId()] = $page->getData();
  82. $this->dataPersistor->clear('cms_page');
  83. }
  84. return $this->loadedData;
  85. }
  86. }