GroupedProductDataProvider.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\GroupedProduct\Ui\DataProvider\Product;
  7. use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
  8. use Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider;
  9. use Magento\GroupedProduct\Model\Product\Type\Grouped as GroupedProductType;
  10. use Magento\Catalog\Model\ProductTypes\ConfigInterface;
  11. use Magento\Framework\App\RequestInterface;
  12. use Magento\Store\Api\Data\StoreInterface;
  13. use Magento\Store\Api\StoreRepositoryInterface;
  14. class GroupedProductDataProvider extends ProductDataProvider
  15. {
  16. /**
  17. * @var RequestInterface
  18. */
  19. protected $request;
  20. /**
  21. * @var ConfigInterface
  22. */
  23. protected $config;
  24. /**
  25. * @var StoreRepositoryInterface
  26. */
  27. protected $storeRepository;
  28. /**
  29. * Construct
  30. *
  31. * @param string $name
  32. * @param string $primaryFieldName
  33. * @param string $requestFieldName
  34. * @param CollectionFactory $collectionFactory
  35. * @param RequestInterface $request
  36. * @param StoreRepositoryInterface $storeRepository
  37. * @param ConfigInterface $config
  38. * @param \Magento\Ui\DataProvider\AddFieldToCollectionInterface[] $addFieldStrategies
  39. * @param \Magento\Ui\DataProvider\AddFilterToCollectionInterface[] $addFilterStrategies
  40. * @param array $meta
  41. * @param array $data
  42. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  43. */
  44. public function __construct(
  45. $name,
  46. $primaryFieldName,
  47. $requestFieldName,
  48. CollectionFactory $collectionFactory,
  49. RequestInterface $request,
  50. ConfigInterface $config,
  51. StoreRepositoryInterface $storeRepository,
  52. array $meta = [],
  53. array $data = [],
  54. array $addFieldStrategies = [],
  55. array $addFilterStrategies = []
  56. ) {
  57. parent::__construct(
  58. $name,
  59. $primaryFieldName,
  60. $requestFieldName,
  61. $collectionFactory,
  62. $addFieldStrategies,
  63. $addFilterStrategies,
  64. $meta,
  65. $data
  66. );
  67. $this->request = $request;
  68. $this->storeRepository = $storeRepository;
  69. $this->config = $config;
  70. }
  71. /**
  72. * Get data
  73. *
  74. * @return array
  75. */
  76. public function getData()
  77. {
  78. if (!$this->getCollection()->isLoaded()) {
  79. $this->getCollection()->addAttributeToFilter(
  80. 'type_id',
  81. $this->config->getComposableTypes()
  82. );
  83. if ($storeId = $this->request->getParam('current_store_id')) {
  84. /** @var StoreInterface $store */
  85. $store = $this->storeRepository->getById($storeId);
  86. $this->getCollection()->setStore($store);
  87. }
  88. $this->getCollection()->load();
  89. }
  90. $items = $this->getCollection()->toArray();
  91. return [
  92. 'totalRecords' => $this->getCollection()->getSize(),
  93. 'items' => array_values($items),
  94. ];
  95. }
  96. }