PostDataProvider.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\Related;
  9. use \Magento\Ui\DataProvider\AbstractDataProvider;
  10. use Magefan\Blog\Model\ResourceModel\Post\Collection;
  11. use Magefan\Blog\Model\ResourceModel\Post\CollectionFactory;
  12. use Magento\Framework\App\RequestInterface;
  13. /**
  14. * Class PostDataProvider
  15. */
  16. class PostDataProvider extends AbstractDataProvider
  17. {
  18. /**
  19. * @var RequestInterface
  20. */
  21. protected $request;
  22. /**
  23. * @var post
  24. */
  25. private $post;
  26. /**
  27. * Construct
  28. *
  29. * @param string $name
  30. * @param string $primaryFieldName
  31. * @param string $requestFieldName
  32. * @param CollectionFactory $collectionFactory
  33. * @param RequestInterface $request
  34. * @param array $meta
  35. * @param array $data
  36. */
  37. public function __construct(
  38. $name,
  39. $primaryFieldName,
  40. $requestFieldName,
  41. CollectionFactory $collectionFactory,
  42. RequestInterface $request,
  43. array $meta = [],
  44. array $data = []
  45. ) {
  46. parent::__construct(
  47. $name,
  48. $primaryFieldName,
  49. $requestFieldName,
  50. $meta,
  51. $data
  52. );
  53. $this->collection = $collectionFactory->create();
  54. $this->request = $request;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function getCollection()
  60. {
  61. /** @var Collection $collection */
  62. $collection = parent::getCollection();
  63. if (!$this->getPost()) {
  64. return $collection;
  65. }
  66. $collection->addFieldToFilter(
  67. $collection->getIdFieldName(),
  68. ['nin' => [$this->getPost()->getId()]]
  69. );
  70. return $this->addCollectionFilters($collection);
  71. }
  72. /**
  73. * Retrieve post
  74. *
  75. * @return PostInterface|null
  76. */
  77. protected function getPost()
  78. {
  79. if (null !== $this->post) {
  80. return $this->post;
  81. }
  82. if (!($id = $this->request->getParam('current_post_id'))) {
  83. return null;
  84. }
  85. return $this->post = $this->postRepository->getById($id);
  86. }
  87. }