PostDataProvider.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Copyright © 2016 Ihor Vansach (ihor@magefan.com). All rights reserved.
  4. * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
  5. *
  6. * Glory to Ukraine! Glory to the heroes!
  7. */
  8. namespace Magefan\Blog\Ui\DataProvider\Post\Form;
  9. use Magefan\Blog\Model\ResourceModel\Post\CollectionFactory;
  10. use Magento\Framework\App\Request\DataPersistorInterface;
  11. /**
  12. * Class DataProvider
  13. */
  14. class PostDataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
  15. {
  16. /**
  17. * @var \Magefan\Blog\Model\ResourceModel\Post\Collection
  18. */
  19. protected $collection;
  20. /**
  21. * @var DataPersistorInterface
  22. */
  23. protected $dataPersistor;
  24. /**
  25. * @var array
  26. */
  27. protected $loadedData;
  28. /**
  29. * @param string $name
  30. * @param string $primaryFieldName
  31. * @param string $requestFieldName
  32. * @param CollectionFactory $postCollectionFactory
  33. * @param DataPersistorInterface $dataPersistor
  34. * @param array $meta
  35. * @param array $data
  36. */
  37. public function __construct(
  38. $name,
  39. $primaryFieldName,
  40. $requestFieldName,
  41. CollectionFactory $postCollectionFactory,
  42. DataPersistorInterface $dataPersistor,
  43. array $meta = [],
  44. array $data = []
  45. ) {
  46. $this->collection = $postCollectionFactory->create();
  47. $this->dataPersistor = $dataPersistor;
  48. parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
  49. $this->meta = $this->prepareMeta($this->meta);
  50. }
  51. /**
  52. * Prepares Meta
  53. *
  54. * @param array $meta
  55. * @return array
  56. */
  57. public function prepareMeta(array $meta)
  58. {
  59. return $meta;
  60. }
  61. /**
  62. * Get data
  63. *
  64. * @return array
  65. */
  66. public function getData()
  67. {
  68. if (isset($this->loadedData)) {
  69. return $this->loadedData;
  70. }
  71. $items = $this->collection->getItems();
  72. /** @var $post \Magefan\Blog\Model\Post */
  73. foreach ($items as $post) {
  74. $post = $post->load($post->getId()); //temporary fix
  75. $data = $post->getData();
  76. /* Prepare Featured Image */
  77. $map = [
  78. 'featured_img' => 'getFeaturedImage',
  79. 'og_img' => 'getOgImage'
  80. ];
  81. foreach ($map as $key => $method) {
  82. if (isset($data[$key])) {
  83. $name = $data[$key];
  84. unset($data[$key]);
  85. $data[$key][0] = [
  86. 'name' => $name,
  87. 'url' => $post->$method(),
  88. ];
  89. }
  90. }
  91. $data['data'] = ['links' => []];
  92. /* Prepare related posts */
  93. $collection = $post->getRelatedPosts();
  94. $items = [];
  95. foreach($collection as $item) {
  96. $items[] = [
  97. 'id' => $item->getId(),
  98. 'title' => $item->getTitle(),
  99. ];
  100. }
  101. $data['data']['links']['post'] = $items;
  102. /* Prepare related products */
  103. $collection = $post->getRelatedProducts()->addAttributeToSelect('name');
  104. $items = [];
  105. foreach($collection as $item) {
  106. $items[] = [
  107. 'id' => $item->getId(),
  108. 'name' => $item->getName(),
  109. ];
  110. }
  111. $data['data']['links']['product'] = $items;
  112. /* Set data */
  113. $this->loadedData[$post->getId()] = $data;
  114. }
  115. return $this->loadedData;
  116. }
  117. }