EntityAttributeList.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\GraphQl\Model;
  8. use Magento\Eav\Api\AttributeManagementInterface;
  9. use Magento\Eav\Api\AttributeSetRepositoryInterface;
  10. use Magento\Eav\Api\Data\AttributeInterface;
  11. use Magento\Framework\Api\FilterBuilder;
  12. use Magento\Framework\Api\MetadataServiceInterface;
  13. use Magento\Framework\Api\SearchCriteriaBuilder;
  14. use Magento\Framework\Exception\NoSuchEntityException;
  15. use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
  16. /**
  17. * Iterate through all attribute sets to retrieve attributes for any given entity type
  18. */
  19. class EntityAttributeList
  20. {
  21. /**
  22. * @var AttributeManagementInterface
  23. */
  24. private $attributeManagement;
  25. /**
  26. * @var AttributeSetRepositoryInterface
  27. */
  28. private $attributeSetRepository;
  29. /**
  30. * @var SearchCriteriaBuilder
  31. */
  32. private $searchCriteriaBuilder;
  33. /**
  34. * @var FilterBuilder
  35. */
  36. private $filterBuilder;
  37. /**
  38. * @param AttributeManagementInterface $attributeManagement
  39. * @param AttributeSetRepositoryInterface $attributeSetRepository
  40. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  41. * @param FilterBuilder $filterBuilder
  42. */
  43. public function __construct(
  44. AttributeManagementInterface $attributeManagement,
  45. AttributeSetRepositoryInterface $attributeSetRepository,
  46. SearchCriteriaBuilder $searchCriteriaBuilder,
  47. FilterBuilder $filterBuilder
  48. ) {
  49. $this->attributeManagement = $attributeManagement;
  50. $this->attributeSetRepository = $attributeSetRepository;
  51. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  52. $this->filterBuilder = $filterBuilder;
  53. }
  54. /**
  55. * Retrieve all EAV and custom attribute codes from all attribute sets for given entity code.
  56. *
  57. * Returned in the format [$attributeCode => $isSortable] with $isSortable being a boolean value where an attribute
  58. * can be sorted with in a search criteria expression. The metadata service parameter is only required if type has
  59. * custom attributes.
  60. *
  61. * @param string $entityCode
  62. * @param MetadataServiceInterface $metadataService
  63. * @return boolean[]
  64. * @throws GraphQlNoSuchEntityException
  65. */
  66. public function getDefaultEntityAttributes(
  67. string $entityCode,
  68. MetadataServiceInterface $metadataService = null
  69. ) : array {
  70. $this->searchCriteriaBuilder->addFilters(
  71. [
  72. $this->filterBuilder
  73. ->setField('entity_type_code')
  74. ->setValue($entityCode)
  75. ->setConditionType('eq')
  76. ->create(),
  77. ]
  78. );
  79. $attributeSetList = $this->attributeSetRepository->getList($this->searchCriteriaBuilder->create())->getItems();
  80. $attributes = [];
  81. foreach ($attributeSetList as $attributeSet) {
  82. try {
  83. $attributes = array_merge(
  84. $attributes,
  85. $this->attributeManagement->getAttributes($entityCode, $attributeSet->getAttributeSetId())
  86. );
  87. } catch (NoSuchEntityException $exception) {
  88. throw new GraphQlNoSuchEntityException(__('Entity code %1 does not exist.', [$entityCode]));
  89. }
  90. }
  91. $attributeCodes = [];
  92. $metadata = $metadataService ? $metadataService->getCustomAttributesMetadata() : [];
  93. foreach ($metadata as $customAttribute) {
  94. if (!array_key_exists($customAttribute->getAttributeCode(), $attributeCodes)) {
  95. $attributeCodes[$customAttribute->getAttributeCode()] = false;
  96. }
  97. }
  98. /** @var AttributeInterface $attribute */
  99. foreach ($attributes as $attribute) {
  100. if (!array_key_exists($attribute->getAttributeCode(), $attributeCodes)) {
  101. $attributeCodes[$attribute->getAttributeCode()]
  102. = ((! $attribute->getIsUserDefined()) && !is_array($attribute));
  103. }
  104. }
  105. return $attributeCodes;
  106. }
  107. }