AttributeProvider.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model;
  7. use Magento\Framework\Model\EntitySnapshot\AttributeProviderInterface;
  8. use Magento\Framework\EntityManager\MetadataPool;
  9. use Magento\Eav\Api\AttributeRepositoryInterface;
  10. use Magento\Framework\Api\SearchCriteriaBuilder;
  11. /**
  12. * Class EntitySnapshot
  13. */
  14. class AttributeProvider implements AttributeProviderInterface
  15. {
  16. /**
  17. * @var MetadataPool
  18. */
  19. protected $metadataPool;
  20. /**
  21. * @var AttributeRepositoryInterface
  22. */
  23. protected $attributeRepository;
  24. /**
  25. * @var SearchCriteriaBuilder
  26. */
  27. protected $searchCriteriaBuilder;
  28. /**
  29. * AttributeProvider constructor.
  30. *
  31. * @param MetadataPool $metadataPool
  32. * @param AttributeRepositoryInterface $attributeRepository
  33. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  34. */
  35. public function __construct(
  36. MetadataPool $metadataPool,
  37. AttributeRepositoryInterface $attributeRepository,
  38. SearchCriteriaBuilder $searchCriteriaBuilder
  39. ) {
  40. $this->metadataPool = $metadataPool;
  41. $this->attributeRepository = $attributeRepository;
  42. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  43. }
  44. /**
  45. * Returns array of fields
  46. *
  47. * @param string $entityType
  48. * @return array
  49. * @throws \Exception
  50. */
  51. public function getAttributes($entityType)
  52. {
  53. $metadata = $this->metadataPool->getMetadata($entityType);
  54. $searchResult = $this->attributeRepository->getList(
  55. $metadata->getEavEntityType(),
  56. $this->searchCriteriaBuilder->addFilter('attribute_set_id', null, 'neq')->create()
  57. );
  58. $attributes = [];
  59. foreach ($searchResult->getItems() as $attribute) {
  60. $attributes[] = $attribute->getAttributeCode();
  61. }
  62. return $attributes;
  63. }
  64. }