AttributeLoader.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model\ResourceModel;
  7. use Magento\Eav\Api\AttributeRepositoryInterface as AttributeRepository;
  8. use Magento\Framework\Api\SearchCriteriaBuilder;
  9. use Magento\Framework\EntityManager\MetadataPool;
  10. /**
  11. * Loads attributes by attribute set
  12. */
  13. class AttributeLoader
  14. {
  15. /** Name of ATTRIBUTE_SET_ID field */
  16. const ATTRIBUTE_SET_ID = 'attribute_set_id';
  17. /**
  18. * @var AttributeRepository
  19. */
  20. private $attributeRepository;
  21. /**
  22. * @var MetadataPool
  23. */
  24. private $metadataPool;
  25. /**
  26. * @var SearchCriteriaBuilder
  27. */
  28. private $searchCriteriaBuilder;
  29. /**
  30. * Constructor
  31. *
  32. * @param AttributeRepository $attributeRepository
  33. * @param MetadataPool $metadataPool
  34. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  35. */
  36. public function __construct(
  37. AttributeRepository $attributeRepository,
  38. MetadataPool $metadataPool,
  39. SearchCriteriaBuilder $searchCriteriaBuilder
  40. ) {
  41. $this->attributeRepository = $attributeRepository;
  42. $this->metadataPool = $metadataPool;
  43. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  44. }
  45. /**
  46. * Get attributes list from attribute set
  47. *
  48. * @param string $entityType
  49. * @param int|null $attributeSetId
  50. * @return \Magento\Eav\Api\Data\AttributeInterface[]
  51. */
  52. public function getAttributes($entityType, $attributeSetId = null)
  53. {
  54. $metadata = $this->metadataPool->getMetadata($entityType);
  55. if ($attributeSetId === null) {
  56. $criteria = $this->searchCriteriaBuilder->addFilter(self::ATTRIBUTE_SET_ID, null, 'neq')->create();
  57. } else {
  58. $criteria = $this->searchCriteriaBuilder->addFilter(self::ATTRIBUTE_SET_ID, $attributeSetId)->create();
  59. }
  60. $searchResult = $this->attributeRepository->getList(
  61. $metadata->getEavEntityType(),
  62. $criteria
  63. );
  64. $attributes = $searchResult->getItems();
  65. return $attributes;
  66. }
  67. }