Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RenderLayered.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Catalog\Model\ResourceModel\Layer\Filter\AttributeFactory;
13 
20 class RenderLayered extends Template
21 {
26 
32  protected $_template = 'Magento_Swatches::product/layered/renderer.phtml';
33 
37  protected $eavAttribute;
38 
42  protected $filter;
43 
47  protected $layerAttribute;
48 
52  protected $swatchHelper;
53 
57  protected $mediaHelper;
58 
67  public function __construct(
68  \Magento\Framework\View\Element\Template\Context $context,
70  AttributeFactory $layerAttribute,
71  \Magento\Swatches\Helper\Data $swatchHelper,
72  \Magento\Swatches\Helper\Media $mediaHelper,
73  array $data = []
74  ) {
75  $this->eavAttribute = $eavAttribute;
76  $this->layerAttribute = $layerAttribute;
77  $this->swatchHelper = $swatchHelper;
78  $this->mediaHelper = $mediaHelper;
79 
80  parent::__construct($context, $data);
81  }
82 
88  public function setSwatchFilter(\Magento\Catalog\Model\Layer\Filter\AbstractFilter $filter)
89  {
90  $this->filter = $filter;
91  $this->eavAttribute = $filter->getAttributeModel();
92 
93  return $this;
94  }
95 
99  public function getSwatchData()
100  {
101  if (false === $this->eavAttribute instanceof Attribute) {
102  throw new \RuntimeException('Magento_Swatches: RenderLayered: Attribute has not been set.');
103  }
104 
105  $attributeOptions = [];
106  foreach ($this->eavAttribute->getOptions() as $option) {
107  if ($currentOption = $this->getFilterOption($this->filter->getItems(), $option)) {
108  $attributeOptions[$option->getValue()] = $currentOption;
109  } elseif ($this->isShowEmptyResults()) {
110  $attributeOptions[$option->getValue()] = $this->getUnusedOption($option);
111  }
112  }
113 
114  $attributeOptionIds = array_keys($attributeOptions);
115  $swatches = $this->swatchHelper->getSwatchesByOptionsId($attributeOptionIds);
116 
117  $data = [
118  'attribute_id' => $this->eavAttribute->getId(),
119  'attribute_code' => $this->eavAttribute->getAttributeCode(),
120  'attribute_label' => $this->eavAttribute->getStoreLabel(),
121  'options' => $attributeOptions,
122  'swatches' => $swatches,
123  ];
124 
125  return $data;
126  }
127 
134  {
136  return $this->_urlBuilder->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true, '_query' => $query]);
137  }
138 
143  protected function getUnusedOption(Option $swatchOption)
144  {
145  return [
146  'label' => $swatchOption->getLabel(),
147  'link' => 'javascript:void();',
148  'custom_style' => 'disabled'
149  ];
150  }
151 
157  protected function getFilterOption(array $filterItems, Option $swatchOption)
158  {
159  $resultOption = false;
160  $filterItem = $this->getFilterItemById($filterItems, $swatchOption->getValue());
161  if ($filterItem && $this->isOptionVisible($filterItem)) {
162  $resultOption = $this->getOptionViewData($filterItem, $swatchOption);
163  }
164 
165  return $resultOption;
166  }
167 
173  protected function getOptionViewData(FilterItem $filterItem, Option $swatchOption)
174  {
175  $customStyle = '';
176  $linkToOption = $this->buildUrl($this->eavAttribute->getAttributeCode(), $filterItem->getValue());
177  if ($this->isOptionDisabled($filterItem)) {
178  $customStyle = 'disabled';
179  $linkToOption = 'javascript:void();';
180  }
181 
182  return [
183  'label' => $swatchOption->getLabel(),
184  'link' => $linkToOption,
185  'custom_style' => $customStyle
186  ];
187  }
188 
193  protected function isOptionVisible(FilterItem $filterItem)
194  {
195  return $this->isOptionDisabled($filterItem) && $this->isShowEmptyResults() ? false : true;
196  }
197 
201  protected function isShowEmptyResults()
202  {
203  return $this->eavAttribute->getIsFilterable() != self::FILTERABLE_WITH_RESULTS;
204  }
205 
210  protected function isOptionDisabled(FilterItem $filterItem)
211  {
212  return !$filterItem->getCount();
213  }
214 
220  protected function getFilterItemById(array $filterItems, $id)
221  {
222  foreach ($filterItems as $item) {
223  if ($item->getValue() == $id) {
224  return $item;
225  }
226  }
227  return false;
228  }
229 
235  public function getSwatchPath($type, $filename)
236  {
237  $imagePath = $this->mediaHelper->getSwatchAttributeImage($type, $filename);
238 
239  return $imagePath;
240  }
241 }
__construct(\Magento\Framework\View\Element\Template\Context $context, Attribute $eavAttribute, AttributeFactory $layerAttribute, \Magento\Swatches\Helper\Data $swatchHelper, \Magento\Swatches\Helper\Media $mediaHelper, array $data=[])
getOptionViewData(FilterItem $filterItem, Option $swatchOption)
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
return false
Definition: gallery.phtml:36
$id
Definition: fieldset.phtml:14
$type
Definition: item.phtml:13
$attributeCode
Definition: extend.phtml:12
setSwatchFilter(\Magento\Catalog\Model\Layer\Filter\AbstractFilter $filter)
getFilterOption(array $filterItems, Option $swatchOption)