Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AttributeProvider.php
Go to the documentation of this file.
1 <?php
7 
12 
14 {
19 
23  protected $attributes;
24 
28  protected $eavConfig;
29 
33  public function __construct(
35  ) {
36  $this->eavConfig = $eavConfig;
37  }
38 
45  public function addDynamicData(array $data)
46  {
47  $additionalFields = $this->convert($this->getAttributes(), $data);
48  $data['fields'] = $this->merge($data['fields'], $additionalFields);
49  return $data;
50  }
51 
57  private function getAttributes()
58  {
59  if ($this->attributes === null) {
60  $this->attributes = [];
61  $entityType = $this->eavConfig->getEntityType(static::ENTITY);
63  $attributes = $entityType->getAttributeCollection()->getItems();
65  $entity = $entityType->getEntity();
66 
67  foreach ($attributes as $attribute) {
68  $attribute->setEntity($entity);
69  }
70  $this->attributes = $attributes;
71  }
72 
73  return $this->attributes;
74  }
75 
83  protected function convert(array $attributes, array $fieldset)
84  {
85  $fields = [];
86  foreach ($attributes as $attribute) {
87  if (!$attribute->isStatic()) {
88  if ($attribute->getData('is_used_in_grid')) {
89  $fields[$attribute->getName()] = [
90  'name' => $attribute->getName(),
91  'handler' => \Magento\Framework\Indexer\Handler\AttributeHandler::class,
92  'origin' => $attribute->getName(),
93  'type' => $this->getType($attribute),
94  'dataType' => $attribute->getBackendType(),
95  'filters' => [],
96  'entity' => static::ENTITY,
97  'bind' => isset($fieldset['references']['customer']['to'])
98  ? $fieldset['references']['customer']['to']
99  : null,
100  ];
101  }
102  } else {
103  $fields[$attribute->getName()] = [
104  'type' => $this->getType($attribute),
105  ];
106  }
107  }
108 
109  return $fields;
110  }
111 
118  protected function getType(Attribute $attribute)
119  {
120  if ($attribute->canBeSearchableInGrid()) {
121  $type = 'searchable';
122  } elseif ($attribute->canBeFilterableInGrid()) {
123  $type = 'filterable';
124  } else {
125  $type = 'virtual';
126  }
127 
128  return $type;
129  }
130 
138  protected function merge(array $dataFields, array $searchableFields)
139  {
140  foreach ($searchableFields as $name => $field) {
141  if (!isset($field['name']) && !isset($dataFields[$name])) {
142  continue;
143  }
144  if (!isset($dataFields[$name])) {
145  $dataFields[$name] = [];
146  }
147  foreach ($field as $key => $value) {
148  $dataFields[$name][$key] = $value;
149  }
150  }
151 
152  return $dataFields;
153  }
154 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$fields
Definition: details.phtml:14
merge(array $dataFields, array $searchableFields)
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
$entity
Definition: element.phtml:22
convert(array $attributes, array $fieldset)
if(!isset($_GET['name'])) $name
Definition: log.php:14