5
5
*/
6
6
namespace Magento \Swatches \Block \LayeredNavigation ;
7
7
8
- use Magento \Eav \Model \Entity \Attribute ;
8
+ use Magento \Catalog \Model \Layer \Filter \AbstractFilter ;
9
+ use Magento \Catalog \Model \Layer \Filter \Item as FilterItem ;
9
10
use Magento \Catalog \Model \ResourceModel \Layer \Filter \AttributeFactory ;
10
- use Magento \Framework \ View \ Element \ Template ;
11
+ use Magento \Eav \ Model \ Entity \ Attribute ;
11
12
use Magento \Eav \Model \Entity \Attribute \Option ;
12
- use Magento \Catalog \Model \Layer \Filter \Item as FilterItem ;
13
+ use Magento \Framework \App \ObjectManager ;
14
+ use Magento \Framework \View \Element \Template ;
15
+ use Magento \Framework \View \Element \Template \Context ;
16
+ use Magento \Swatches \Helper \Data ;
17
+ use Magento \Swatches \Helper \Media ;
18
+ use Magento \Theme \Block \Html \Pager ;
13
19
14
20
/**
15
21
* Class RenderLayered Render Swatches at Layered Navigation
@@ -37,7 +43,7 @@ class RenderLayered extends Template
37
43
protected $ eavAttribute ;
38
44
39
45
/**
40
- * @var \Magento\Catalog\Model\Layer\Filter\ AbstractFilter
46
+ * @var AbstractFilter
41
47
*/
42
48
protected $ filter ;
43
49
@@ -47,41 +53,52 @@ class RenderLayered extends Template
47
53
protected $ layerAttribute ;
48
54
49
55
/**
50
- * @var \Magento\Swatches\Helper\ Data
56
+ * @var Data
51
57
*/
52
58
protected $ swatchHelper ;
53
59
54
60
/**
55
- * @var \Magento\Swatches\Helper\ Media
61
+ * @var Media
56
62
*/
57
63
protected $ mediaHelper ;
58
64
59
65
/**
60
- * @param Template\Context $context
66
+ * @var Pager
67
+ */
68
+ private $ htmlPagerBlock ;
69
+
70
+ /**
71
+ * @param Context $context
61
72
* @param Attribute $eavAttribute
62
73
* @param AttributeFactory $layerAttribute
63
- * @param \Magento\Swatches\Helper\ Data $swatchHelper
64
- * @param \Magento\Swatches\Helper\ Media $mediaHelper
74
+ * @param Data $swatchHelper
75
+ * @param Media $mediaHelper
65
76
* @param array $data
77
+ * @param Pager|null $htmlPagerBlock
66
78
*/
67
79
public function __construct (
68
- \ Magento \ Framework \ View \ Element \ Template \ Context $ context ,
80
+ Context $ context ,
69
81
Attribute $ eavAttribute ,
70
82
AttributeFactory $ layerAttribute ,
71
- \Magento \Swatches \Helper \Data $ swatchHelper ,
72
- \Magento \Swatches \Helper \Media $ mediaHelper ,
73
- array $ data = []
83
+ Data $ swatchHelper ,
84
+ Media $ mediaHelper ,
85
+ array $ data = [],
86
+ ?Pager $ htmlPagerBlock = null
74
87
) {
75
88
$ this ->eavAttribute = $ eavAttribute ;
76
89
$ this ->layerAttribute = $ layerAttribute ;
77
90
$ this ->swatchHelper = $ swatchHelper ;
78
91
$ this ->mediaHelper = $ mediaHelper ;
92
+ $ this ->htmlPagerBlock = $ htmlPagerBlock ?? ObjectManager::getInstance ()->get (Pager::class);
79
93
80
94
parent ::__construct ($ context , $ data );
81
95
}
82
96
83
97
/**
98
+ * Set filter and attribute objects
99
+ *
84
100
* @param \Magento\Catalog\Model\Layer\Filter\AbstractFilter $filter
101
+ *
85
102
* @return $this
86
103
* @throws \Magento\Framework\Exception\LocalizedException
87
104
*/
@@ -94,6 +111,8 @@ public function setSwatchFilter(\Magento\Catalog\Model\Layer\Filter\AbstractFilt
94
111
}
95
112
96
113
/**
114
+ * Get attribute swatch data
115
+ *
97
116
* @return array
98
117
*/
99
118
public function getSwatchData ()
@@ -114,30 +133,46 @@ public function getSwatchData()
114
133
$ attributeOptionIds = array_keys ($ attributeOptions );
115
134
$ swatches = $ this ->swatchHelper ->getSwatchesByOptionsId ($ attributeOptionIds );
116
135
117
- $ data = [
136
+ return [
118
137
'attribute_id ' => $ this ->eavAttribute ->getId (),
119
138
'attribute_code ' => $ this ->eavAttribute ->getAttributeCode (),
120
139
'attribute_label ' => $ this ->eavAttribute ->getStoreLabel (),
121
140
'options ' => $ attributeOptions ,
122
141
'swatches ' => $ swatches ,
123
142
];
124
-
125
- return $ data ;
126
143
}
127
144
128
145
/**
146
+ * Build filter option url
147
+ *
129
148
* @param string $attributeCode
130
149
* @param int $optionId
150
+ *
131
151
* @return string
132
152
*/
133
153
public function buildUrl ($ attributeCode , $ optionId )
134
154
{
135
- $ query = [$ attributeCode => $ optionId ];
136
- return $ this ->_urlBuilder ->getUrl ('*/*/* ' , ['_current ' => true , '_use_rewrite ' => true , '_query ' => $ query ]);
155
+ $ query = [
156
+ $ attributeCode => $ optionId ,
157
+ // exclude current page from urls
158
+ $ this ->htmlPagerBlock ->getPageVarName () => null
159
+ ];
160
+
161
+ return $ this ->_urlBuilder ->getUrl (
162
+ '*/*/* ' ,
163
+ [
164
+ '_current ' => true ,
165
+ '_use_rewrite ' => true ,
166
+ '_query ' => $ query
167
+ ]
168
+ );
137
169
}
138
170
139
171
/**
172
+ * Get view data for option with no results
173
+ *
140
174
* @param Option $swatchOption
175
+ *
141
176
* @return array
142
177
*/
143
178
protected function getUnusedOption (Option $ swatchOption )
@@ -150,8 +185,11 @@ protected function getUnusedOption(Option $swatchOption)
150
185
}
151
186
152
187
/**
188
+ * Get option data if visible
189
+ *
153
190
* @param FilterItem[] $filterItems
154
191
* @param Option $swatchOption
192
+ *
155
193
* @return array
156
194
*/
157
195
protected function getFilterOption (array $ filterItems , Option $ swatchOption )
@@ -166,8 +204,11 @@ protected function getFilterOption(array $filterItems, Option $swatchOption)
166
204
}
167
205
168
206
/**
207
+ * Get view data for option
208
+ *
169
209
* @param FilterItem $filterItem
170
210
* @param Option $swatchOption
211
+ *
171
212
* @return array
172
213
*/
173
214
protected function getOptionViewData (FilterItem $ filterItem , Option $ swatchOption )
@@ -187,15 +228,20 @@ protected function getOptionViewData(FilterItem $filterItem, Option $swatchOptio
187
228
}
188
229
189
230
/**
231
+ * Check if option should be visible
232
+ *
190
233
* @param FilterItem $filterItem
234
+ *
191
235
* @return bool
192
236
*/
193
237
protected function isOptionVisible (FilterItem $ filterItem )
194
238
{
195
- return $ this ->isOptionDisabled ($ filterItem ) && $ this ->isShowEmptyResults () ? false : true ;
239
+ return !( $ this ->isOptionDisabled ($ filterItem ) && $ this ->isShowEmptyResults ()) ;
196
240
}
197
241
198
242
/**
243
+ * Check if attribute values should be visible with no results
244
+ *
199
245
* @return bool
200
246
*/
201
247
protected function isShowEmptyResults ()
@@ -204,7 +250,10 @@ protected function isShowEmptyResults()
204
250
}
205
251
206
252
/**
253
+ * Check if option should be disabled
254
+ *
207
255
* @param FilterItem $filterItem
256
+ *
208
257
* @return bool
209
258
*/
210
259
protected function isOptionDisabled (FilterItem $ filterItem )
@@ -213,8 +262,11 @@ protected function isOptionDisabled(FilterItem $filterItem)
213
262
}
214
263
215
264
/**
265
+ * Retrieve filter item by id
266
+ *
216
267
* @param FilterItem[] $filterItems
217
268
* @param integer $id
269
+ *
218
270
* @return bool|FilterItem
219
271
*/
220
272
protected function getFilterItemById (array $ filterItems , $ id )
@@ -228,14 +280,15 @@ protected function getFilterItemById(array $filterItems, $id)
228
280
}
229
281
230
282
/**
283
+ * Get swatch image path
284
+ *
231
285
* @param string $type
232
286
* @param string $filename
287
+ *
233
288
* @return string
234
289
*/
235
290
public function getSwatchPath ($ type , $ filename )
236
291
{
237
- $ imagePath = $ this ->mediaHelper ->getSwatchAttributeImage ($ type , $ filename );
238
-
239
- return $ imagePath ;
292
+ return $ this ->mediaHelper ->getSwatchAttributeImage ($ type , $ filename );
240
293
}
241
294
}
0 commit comments