Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductFieldset.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory;
12 
20 {
24  private $eavConfig;
25 
29  private $collectionFactory;
30 
34  private $searchableAttributes;
35 
40  public function __construct(
41  Config $eavConfig,
42  CollectionFactory $collectionFactory
43  ) {
44  $this->eavConfig = $eavConfig;
45  $this->collectionFactory = $collectionFactory;
46  }
47 
51  public function addDynamicData(array $data)
52  {
53  $searchableAttributes = $this->getSearchableAttributes();
54 
55  $defaultSource = isset($data['source']) ? $data['source'] : null;
56  $additionalFields = $this->convert($searchableAttributes, $defaultSource, null);
57 
58  $data['fields'] = $this->merge($data['fields'], $additionalFields);
59 
60  return $data;
61  }
62 
68  private function getSearchableAttributes()
69  {
70  if ($this->searchableAttributes === null) {
71  $this->searchableAttributes = [];
72 
74  $productAttributes = $this->collectionFactory->create();
75  $productAttributes->addToIndexFilter(true);
76 
78  $attributes = $productAttributes->getItems();
79 
80  $entity = $this->eavConfig->getEntityType(Product::ENTITY)
81  ->getEntity();
82 
83  foreach ($attributes as $attribute) {
84  $attribute->setEntity($entity);
85  }
86 
87  $this->searchableAttributes = $attributes;
88  }
89 
90  return $this->searchableAttributes;
91  }
92 
99  private function convert(array $attributes, $defaultSource, $defaultHandler)
100  {
101  $fields = [];
102  foreach ($attributes as $attribute) {
103  $fields[] = [
104  'name' => $attribute->getName(),
105  'source' => $defaultSource,
106  'handler' => $defaultHandler,
107  'dataType' => $attribute->getBackendType(),
108  'type' => $this->getType($attribute),
109  'filters' => [],
110  ];
111  }
112 
113  return $fields;
114  }
115 
120  private function getType(Attribute $attribute)
121  {
122  $type = '';
123  $isFilterable = $attribute->getData('is_filterable') || $attribute->getData('is_filterable_in_search');
124  $isSearchable = $attribute->getData('is_searchable');
125  if ($isSearchable && $isFilterable) {
126  $type = 'both';
127  } elseif ($isSearchable) {
128  $type = 'searchable';
129  } elseif ($isFilterable) {
130  $type = 'filterable';
131  }
132 
133  return $type;
134  }
135 
141  private function merge(array $dataFields, array $searchableFields)
142  {
143  foreach ($searchableFields as $field) {
144  if (!isset($dataFields[$field['name']])) {
145  $dataFields[$field['name']] = $field;
146  }
147  }
148 
149  return $dataFields;
150  }
151 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$fields
Definition: details.phtml:14
$type
Definition: item.phtml:13
$entity
Definition: element.phtml:22
$attributes
Definition: matrix.phtml:13
__construct(Config $eavConfig, CollectionFactory $collectionFactory)