AttributeProvider.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\Indexer;
  7. use Magento\Customer\Model\Customer;
  8. use Magento\Framework\Indexer\FieldsetInterface;
  9. use Magento\Eav\Model\Config;
  10. use Magento\Customer\Model\Attribute;
  11. class AttributeProvider implements FieldsetInterface
  12. {
  13. /**
  14. * EAV entity
  15. */
  16. const ENTITY = Customer::ENTITY;
  17. /**
  18. * @var Attribute[]
  19. */
  20. protected $attributes;
  21. /**
  22. * @var Config
  23. */
  24. protected $eavConfig;
  25. /**
  26. * @param Config $eavConfig
  27. */
  28. public function __construct(
  29. Config $eavConfig
  30. ) {
  31. $this->eavConfig = $eavConfig;
  32. }
  33. /**
  34. * Add EAV attribute fields to fieldset
  35. *
  36. * @param array $data
  37. * @return array
  38. */
  39. public function addDynamicData(array $data)
  40. {
  41. $additionalFields = $this->convert($this->getAttributes(), $data);
  42. $data['fields'] = $this->merge($data['fields'], $additionalFields);
  43. return $data;
  44. }
  45. /**
  46. * Retrieve all attributes
  47. *
  48. * @return Attribute[]
  49. */
  50. private function getAttributes()
  51. {
  52. if ($this->attributes === null) {
  53. $this->attributes = [];
  54. $entityType = $this->eavConfig->getEntityType(static::ENTITY);
  55. /** @var \Magento\Customer\Model\Attribute[] $attributes */
  56. $attributes = $entityType->getAttributeCollection()->getItems();
  57. /** @var \Magento\Customer\Model\ResourceModel\Customer $entity */
  58. $entity = $entityType->getEntity();
  59. foreach ($attributes as $attribute) {
  60. $attribute->setEntity($entity);
  61. }
  62. $this->attributes = $attributes;
  63. }
  64. return $this->attributes;
  65. }
  66. /**
  67. * Convert attributes to fields
  68. *
  69. * @param Attribute[] $attributes
  70. * @param array $fieldset
  71. * @return array
  72. */
  73. protected function convert(array $attributes, array $fieldset)
  74. {
  75. $fields = [];
  76. foreach ($attributes as $attribute) {
  77. if (!$attribute->isStatic()) {
  78. if ($attribute->getData('is_used_in_grid')) {
  79. $fields[$attribute->getName()] = [
  80. 'name' => $attribute->getName(),
  81. 'handler' => \Magento\Framework\Indexer\Handler\AttributeHandler::class,
  82. 'origin' => $attribute->getName(),
  83. 'type' => $this->getType($attribute),
  84. 'dataType' => $attribute->getBackendType(),
  85. 'filters' => [],
  86. 'entity' => static::ENTITY,
  87. 'bind' => isset($fieldset['references']['customer']['to'])
  88. ? $fieldset['references']['customer']['to']
  89. : null,
  90. ];
  91. }
  92. } else {
  93. $fields[$attribute->getName()] = [
  94. 'type' => $this->getType($attribute),
  95. ];
  96. }
  97. }
  98. return $fields;
  99. }
  100. /**
  101. * Get field type for attribute
  102. *
  103. * @param Attribute $attribute
  104. * @return string
  105. */
  106. protected function getType(Attribute $attribute)
  107. {
  108. if ($attribute->canBeSearchableInGrid()) {
  109. $type = 'searchable';
  110. } elseif ($attribute->canBeFilterableInGrid()) {
  111. $type = 'filterable';
  112. } else {
  113. $type = 'virtual';
  114. }
  115. return $type;
  116. }
  117. /**
  118. * Merge fields with attribute fields
  119. *
  120. * @param array $dataFields
  121. * @param array $searchableFields
  122. * @return array
  123. */
  124. protected function merge(array $dataFields, array $searchableFields)
  125. {
  126. foreach ($searchableFields as $name => $field) {
  127. if (!isset($field['name']) && !isset($dataFields[$name])) {
  128. continue;
  129. }
  130. if (!isset($dataFields[$name])) {
  131. $dataFields[$name] = [];
  132. }
  133. foreach ($field as $key => $value) {
  134. $dataFields[$name][$key] = $value;
  135. }
  136. }
  137. return $dataFields;
  138. }
  139. }