Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Attribute.php
Go to the documentation of this file.
1 <?php
7 
9 
14 {
18  private $tagFilter;
19 
28  public function __construct(
29  \Magento\Catalog\Model\Layer\Filter\ItemFactory $filterItemFactory,
31  \Magento\Catalog\Model\Layer $layer,
32  \Magento\Catalog\Model\Layer\Filter\Item\DataBuilder $itemDataBuilder,
33  \Magento\Framework\Filter\StripTags $tagFilter,
34  array $data = []
35  ) {
36  parent::__construct(
37  $filterItemFactory,
39  $layer,
41  $data
42  );
43  $this->tagFilter = $tagFilter;
44  }
45 
53  public function apply(\Magento\Framework\App\RequestInterface $request)
54  {
55  $attributeValue = $request->getParam($this->_requestVar);
56  if (empty($attributeValue) && !is_numeric($attributeValue)) {
57  return $this;
58  }
59  $attribute = $this->getAttributeModel();
61  $productCollection = $this->getLayer()
62  ->getProductCollection();
63  $productCollection->addFieldToFilter($attribute->getAttributeCode(), $attributeValue);
64  $label = $this->getOptionText($attributeValue);
65  $this->getLayer()
66  ->getState()
67  ->addFilter($this->_createItem($label, $attributeValue));
68 
69  $this->setItems([]); // set items to disable show filtering
70  return $this;
71  }
72 
79  protected function _getItemsData()
80  {
81  $attribute = $this->getAttributeModel();
83  $productCollection = $this->getLayer()
84  ->getProductCollection();
85  $optionsFacetedData = $productCollection->getFacetedData($attribute->getAttributeCode());
86 
87  $isAttributeFilterable =
88  $this->getAttributeIsFilterable($attribute) === static::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS;
89 
90  if (count($optionsFacetedData) === 0 && !$isAttributeFilterable) {
91  return $this->itemDataBuilder->build();
92  }
93 
94  $productSize = $productCollection->getSize();
95 
96  $options = $attribute->getFrontend()
97  ->getSelectOptions();
98  foreach ($options as $option) {
99  $this->buildOptionData($option, $isAttributeFilterable, $optionsFacetedData, $productSize);
100  }
101 
102  return $this->itemDataBuilder->build();
103  }
104 
114  private function buildOptionData($option, $isAttributeFilterable, $optionsFacetedData, $productSize)
115  {
116  $value = $this->getOptionValue($option);
117  if ($value === false) {
118  return;
119  }
120  $count = $this->getOptionCount($value, $optionsFacetedData);
121  if ($isAttributeFilterable && (!$this->isOptionReducesResults($count, $productSize) || $count === 0)) {
122  return;
123  }
124 
125  $this->itemDataBuilder->addItemData(
126  $this->tagFilter->filter($option['label']),
127  $value,
128  $count
129  );
130  }
131 
138  private function getOptionValue($option)
139  {
140  if (empty($option['value']) && !is_numeric($option['value'])) {
141  return false;
142  }
143  return $option['value'];
144  }
145 
153  private function getOptionCount($value, $optionsFacetedData)
154  {
155  return isset($optionsFacetedData[$value]['count'])
156  ? (int)$optionsFacetedData[$value]['count']
157  : 0;
158  }
159 }
__construct(\Magento\Catalog\Model\Layer\Filter\ItemFactory $filterItemFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Model\Layer $layer, \Magento\Catalog\Model\Layer\Filter\Item\DataBuilder $itemDataBuilder, \Magento\Framework\Filter\StripTags $tagFilter, array $data=[])
Definition: Attribute.php:28
apply(\Magento\Framework\App\RequestInterface $request)
$count
Definition: recent.phtml:13
$storeManager
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16