55 */
66namespace Magento \Swatches \Block \LayeredNavigation ;
77
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 ;
910use Magento \Catalog \Model \ResourceModel \Layer \Filter \AttributeFactory ;
10- use Magento \Framework \ View \ Element \ Template ;
11+ use Magento \Eav \ Model \ Entity \ Attribute ;
1112use 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 ;
1319
1420/**
1521 * Class RenderLayered Render Swatches at Layered Navigation
@@ -37,7 +43,7 @@ class RenderLayered extends Template
3743 protected $ eavAttribute ;
3844
3945 /**
40- * @var \Magento\Catalog\Model\Layer\Filter\ AbstractFilter
46+ * @var AbstractFilter
4147 */
4248 protected $ filter ;
4349
@@ -47,41 +53,52 @@ class RenderLayered extends Template
4753 protected $ layerAttribute ;
4854
4955 /**
50- * @var \Magento\Swatches\Helper\ Data
56+ * @var Data
5157 */
5258 protected $ swatchHelper ;
5359
5460 /**
55- * @var \Magento\Swatches\Helper\ Media
61+ * @var Media
5662 */
5763 protected $ mediaHelper ;
5864
5965 /**
60- * @param Template\Context $context
66+ * @var Pager
67+ */
68+ private $ htmlPagerBlock ;
69+
70+ /**
71+ * @param Context $context
6172 * @param Attribute $eavAttribute
6273 * @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
6576 * @param array $data
77+ * @param Pager|null $htmlPagerBlock
6678 */
6779 public function __construct (
68- \ Magento \ Framework \ View \ Element \ Template \ Context $ context ,
80+ Context $ context ,
6981 Attribute $ eavAttribute ,
7082 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
7487 ) {
7588 $ this ->eavAttribute = $ eavAttribute ;
7689 $ this ->layerAttribute = $ layerAttribute ;
7790 $ this ->swatchHelper = $ swatchHelper ;
7891 $ this ->mediaHelper = $ mediaHelper ;
92+ $ this ->htmlPagerBlock = $ htmlPagerBlock ?? ObjectManager::getInstance ()->get (Pager::class);
7993
8094 parent ::__construct ($ context , $ data );
8195 }
8296
8397 /**
98+ * Set filter and attribute objects
99+ *
84100 * @param \Magento\Catalog\Model\Layer\Filter\AbstractFilter $filter
101+ *
85102 * @return $this
86103 * @throws \Magento\Framework\Exception\LocalizedException
87104 */
@@ -94,6 +111,8 @@ public function setSwatchFilter(\Magento\Catalog\Model\Layer\Filter\AbstractFilt
94111 }
95112
96113 /**
114+ * Get attribute swatch data
115+ *
97116 * @return array
98117 */
99118 public function getSwatchData ()
@@ -114,30 +133,46 @@ public function getSwatchData()
114133 $ attributeOptionIds = array_keys ($ attributeOptions );
115134 $ swatches = $ this ->swatchHelper ->getSwatchesByOptionsId ($ attributeOptionIds );
116135
117- $ data = [
136+ return [
118137 'attribute_id ' => $ this ->eavAttribute ->getId (),
119138 'attribute_code ' => $ this ->eavAttribute ->getAttributeCode (),
120139 'attribute_label ' => $ this ->eavAttribute ->getStoreLabel (),
121140 'options ' => $ attributeOptions ,
122141 'swatches ' => $ swatches ,
123142 ];
124-
125- return $ data ;
126143 }
127144
128145 /**
146+ * Build filter option url
147+ *
129148 * @param string $attributeCode
130149 * @param int $optionId
150+ *
131151 * @return string
132152 */
133153 public function buildUrl ($ attributeCode , $ optionId )
134154 {
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+ );
137169 }
138170
139171 /**
172+ * Get view data for option with no results
173+ *
140174 * @param Option $swatchOption
175+ *
141176 * @return array
142177 */
143178 protected function getUnusedOption (Option $ swatchOption )
@@ -150,8 +185,11 @@ protected function getUnusedOption(Option $swatchOption)
150185 }
151186
152187 /**
188+ * Get option data if visible
189+ *
153190 * @param FilterItem[] $filterItems
154191 * @param Option $swatchOption
192+ *
155193 * @return array
156194 */
157195 protected function getFilterOption (array $ filterItems , Option $ swatchOption )
@@ -166,8 +204,11 @@ protected function getFilterOption(array $filterItems, Option $swatchOption)
166204 }
167205
168206 /**
207+ * Get view data for option
208+ *
169209 * @param FilterItem $filterItem
170210 * @param Option $swatchOption
211+ *
171212 * @return array
172213 */
173214 protected function getOptionViewData (FilterItem $ filterItem , Option $ swatchOption )
@@ -187,15 +228,20 @@ protected function getOptionViewData(FilterItem $filterItem, Option $swatchOptio
187228 }
188229
189230 /**
231+ * Check if option should be visible
232+ *
190233 * @param FilterItem $filterItem
234+ *
191235 * @return bool
192236 */
193237 protected function isOptionVisible (FilterItem $ filterItem )
194238 {
195- return $ this ->isOptionDisabled ($ filterItem ) && $ this ->isShowEmptyResults () ? false : true ;
239+ return !( $ this ->isOptionDisabled ($ filterItem ) && $ this ->isShowEmptyResults ()) ;
196240 }
197241
198242 /**
243+ * Check if attribute values should be visible with no results
244+ *
199245 * @return bool
200246 */
201247 protected function isShowEmptyResults ()
@@ -204,7 +250,10 @@ protected function isShowEmptyResults()
204250 }
205251
206252 /**
253+ * Check if option should be disabled
254+ *
207255 * @param FilterItem $filterItem
256+ *
208257 * @return bool
209258 */
210259 protected function isOptionDisabled (FilterItem $ filterItem )
@@ -213,8 +262,11 @@ protected function isOptionDisabled(FilterItem $filterItem)
213262 }
214263
215264 /**
265+ * Retrieve filter item by id
266+ *
216267 * @param FilterItem[] $filterItems
217268 * @param integer $id
269+ *
218270 * @return bool|FilterItem
219271 */
220272 protected function getFilterItemById (array $ filterItems , $ id )
@@ -228,14 +280,15 @@ protected function getFilterItemById(array $filterItems, $id)
228280 }
229281
230282 /**
283+ * Get swatch image path
284+ *
231285 * @param string $type
232286 * @param string $filename
287+ *
233288 * @return string
234289 */
235290 public function getSwatchPath ($ type , $ filename )
236291 {
237- $ imagePath = $ this ->mediaHelper ->getSwatchAttributeImage ($ type , $ filename );
238-
239- return $ imagePath ;
292+ return $ this ->mediaHelper ->getSwatchAttributeImage ($ type , $ filename );
240293 }
241294}
0 commit comments