Attributes.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ConfigurableProduct\Ui\DataProvider;
  7. class Attributes extends \Magento\Ui\DataProvider\AbstractDataProvider
  8. {
  9. /**
  10. * @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection
  11. */
  12. protected $collection;
  13. /**
  14. * @param string $name
  15. * @param string $primaryFieldName
  16. * @param string $requestFieldName
  17. * @param \Magento\ConfigurableProduct\Model\ConfigurableAttributeHandler $configurableAttributeHandler
  18. * @param array $meta
  19. * @param array $data
  20. */
  21. public function __construct(
  22. $name,
  23. $primaryFieldName,
  24. $requestFieldName,
  25. \Magento\ConfigurableProduct\Model\ConfigurableAttributeHandler $configurableAttributeHandler,
  26. array $meta = [],
  27. array $data = []
  28. ) {
  29. parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
  30. $this->configurableAttributeHandler = $configurableAttributeHandler;
  31. $this->collection = $configurableAttributeHandler->getApplicableAttributes();
  32. }
  33. /**
  34. * @return \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection
  35. */
  36. public function getCollection()
  37. {
  38. return $this->collection;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function getData()
  44. {
  45. $items = [];
  46. $skippedItems = 0;
  47. foreach ($this->getCollection()->getItems() as $attribute) {
  48. if ($this->configurableAttributeHandler->isAttributeApplicable($attribute)) {
  49. $items[] = $attribute->toArray();
  50. } else {
  51. $skippedItems++;
  52. }
  53. }
  54. return [
  55. 'totalRecords' => $this->collection->getSize() - $skippedItems,
  56. 'items' => $items
  57. ];
  58. }
  59. }