AttributeMetadataDataProvider.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model;
  7. use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
  8. /**
  9. * Attribute Metadata data provider class
  10. */
  11. class AttributeMetadataDataProvider
  12. {
  13. /**
  14. * @var \Magento\Eav\Model\Config
  15. */
  16. private $eavConfig;
  17. /**
  18. * @var \Magento\Customer\Model\ResourceModel\Form\Attribute\CollectionFactory
  19. */
  20. private $attrFormCollectionFactory;
  21. /**
  22. * @var \Magento\Store\Model\StoreManager
  23. */
  24. private $storeManager;
  25. /**
  26. * Initialize data provider with data source
  27. *
  28. * @param \Magento\Eav\Model\Config $eavConfig
  29. * @param \Magento\Customer\Model\ResourceModel\Form\Attribute\CollectionFactory $attrFormCollectionFactory
  30. * @param \Magento\Store\Model\StoreManager $storeManager
  31. */
  32. public function __construct(
  33. \Magento\Eav\Model\Config $eavConfig,
  34. \Magento\Customer\Model\ResourceModel\Form\Attribute\CollectionFactory $attrFormCollectionFactory,
  35. \Magento\Store\Model\StoreManager $storeManager
  36. ) {
  37. $this->eavConfig = $eavConfig;
  38. $this->attrFormCollectionFactory = $attrFormCollectionFactory;
  39. $this->storeManager = $storeManager;
  40. }
  41. /**
  42. * Get attribute model for a given entity type and code
  43. *
  44. * @param string $entityType
  45. * @param string $attributeCode
  46. * @return false|AbstractAttribute
  47. */
  48. public function getAttribute($entityType, $attributeCode)
  49. {
  50. return $this->eavConfig->getAttribute($entityType, $attributeCode);
  51. }
  52. /**
  53. * Get all attribute codes for a given entity type and attribute set
  54. *
  55. * @param string $entityType
  56. * @param int $attributeSetId
  57. * @param string|null $storeId
  58. * @return array Attribute codes
  59. */
  60. public function getAllAttributeCodes($entityType, $attributeSetId = 0, $storeId = null)
  61. {
  62. if (null === $storeId) {
  63. $storeId = $this->storeManager->getStore()->getId();
  64. }
  65. $object = new \Magento\Framework\DataObject(
  66. [
  67. 'store_id' => $storeId,
  68. 'attribute_set_id' => $attributeSetId,
  69. ]
  70. );
  71. return $this->eavConfig->getEntityAttributeCodes($entityType, $object);
  72. }
  73. /**
  74. * Load collection with filters applied
  75. *
  76. * @param string $entityType
  77. * @param string $formCode
  78. * @return \Magento\Customer\Model\ResourceModel\Form\Attribute\Collection
  79. */
  80. public function loadAttributesCollection($entityType, $formCode)
  81. {
  82. $attributesFormCollection = $this->attrFormCollectionFactory->create();
  83. $attributesFormCollection->setStore($this->storeManager->getStore())
  84. ->setEntityType($entityType)
  85. ->addFormCodeFilter($formCode)
  86. ->setSortOrder();
  87. return $attributesFormCollection;
  88. }
  89. }