9
9
use Magento \Catalog \Api \CategoryRepositoryInterface ;
10
10
use Magento \Catalog \Block \Product \ProductList \Toolbar ;
11
11
use Magento \Catalog \Model \Category ;
12
+ use Magento \Catalog \Model \Config ;
13
+ use Magento \Catalog \Model \Layer ;
14
+ use Magento \Catalog \Model \Layer \Resolver ;
12
15
use Magento \Catalog \Model \Product ;
13
16
use Magento \Catalog \Model \ResourceModel \Product \Collection ;
17
+ use Magento \Catalog \Pricing \Price \FinalPrice ;
14
18
use Magento \Eav \Model \Entity \Collection \AbstractCollection ;
19
+ use Magento \Framework \App \ActionInterface ;
20
+ use Magento \Framework \App \Config \Element ;
21
+ use Magento \Framework \Data \Helper \PostHelper ;
15
22
use Magento \Framework \DataObject \IdentityInterface ;
16
23
use Magento \Framework \Exception \NoSuchEntityException ;
24
+ use Magento \Framework \Pricing \Render ;
25
+ use Magento \Framework \Url \Helper \Data ;
17
26
18
27
/**
19
28
* Product list
@@ -40,17 +49,17 @@ class ListProduct extends AbstractProduct implements IdentityInterface
40
49
/**
41
50
* Catalog layer
42
51
*
43
- * @var \Magento\Catalog\Model\ Layer
52
+ * @var Layer
44
53
*/
45
54
protected $ _catalogLayer ;
46
55
47
56
/**
48
- * @var \Magento\Framework\Data\Helper\ PostHelper
57
+ * @var PostHelper
49
58
*/
50
59
protected $ _postDataHelper ;
51
60
52
61
/**
53
- * @var \Magento\Framework\Url\Helper\ Data
62
+ * @var Data
54
63
*/
55
64
protected $ urlHelper ;
56
65
@@ -61,18 +70,18 @@ class ListProduct extends AbstractProduct implements IdentityInterface
61
70
62
71
/**
63
72
* @param Context $context
64
- * @param \Magento\Framework\Data\Helper\ PostHelper $postDataHelper
65
- * @param \Magento\Catalog\Model\Layer\ Resolver $layerResolver
73
+ * @param PostHelper $postDataHelper
74
+ * @param Resolver $layerResolver
66
75
* @param CategoryRepositoryInterface $categoryRepository
67
- * @param \Magento\Framework\Url\Helper\ Data $urlHelper
76
+ * @param Data $urlHelper
68
77
* @param array $data
69
78
*/
70
79
public function __construct (
71
- \ Magento \ Catalog \ Block \ Product \ Context $ context ,
72
- \ Magento \ Framework \ Data \ Helper \ PostHelper $ postDataHelper ,
73
- \ Magento \ Catalog \ Model \ Layer \ Resolver $ layerResolver ,
80
+ Context $ context ,
81
+ PostHelper $ postDataHelper ,
82
+ Resolver $ layerResolver ,
74
83
CategoryRepositoryInterface $ categoryRepository ,
75
- \ Magento \ Framework \ Url \ Helper \ Data $ urlHelper ,
84
+ Data $ urlHelper ,
76
85
array $ data = []
77
86
) {
78
87
$ this ->_catalogLayer = $ layerResolver ->get ();
@@ -113,7 +122,7 @@ protected function _getProductCollection()
113
122
/**
114
123
* Get catalog layer model
115
124
*
116
- * @return \Magento\Catalog\Model\ Layer
125
+ * @return Layer
117
126
*/
118
127
public function getLayer ()
119
128
{
@@ -137,7 +146,35 @@ public function getLoadedProductCollection()
137
146
*/
138
147
public function getMode ()
139
148
{
140
- return $ this ->getChildBlock ('toolbar ' )->getCurrentMode ();
149
+ if ($ this ->getChildBlock ('toolbar ' )) {
150
+ return $ this ->getChildBlock ('toolbar ' )->getCurrentMode ();
151
+ }
152
+
153
+ return $ this ->getDefaultListingMode ();
154
+ }
155
+
156
+ /**
157
+ * Get listing mode for products if toolbar is removed from layout.
158
+ * Use the general configuration for product list mode from config path catalog/frontend/list_mode as default value
159
+ * or mode data from block declaration from layout.
160
+ *
161
+ * @return string
162
+ */
163
+ private function getDefaultListingMode ()
164
+ {
165
+ // default Toolbar when the toolbar layout is not used
166
+ $ defaultToolbar = $ this ->getToolbarBlock ();
167
+ $ availableModes = $ defaultToolbar ->getModes ();
168
+
169
+ // layout config mode
170
+ $ mode = $ this ->getData ('mode ' );
171
+
172
+ if (!$ mode || !isset ($ availableModes [$ mode ])) {
173
+ // default config mode
174
+ $ mode = $ defaultToolbar ->getCurrentMode ();
175
+ }
176
+
177
+ return $ mode ;
141
178
}
142
179
143
180
/**
@@ -148,28 +185,60 @@ public function getMode()
148
185
protected function _beforeToHtml ()
149
186
{
150
187
$ collection = $ this ->_getProductCollection ();
151
- $ this ->configureToolbar ($ this ->getToolbarBlock (), $ collection );
188
+
189
+ $ this ->addToolbarBlock ($ collection );
190
+
152
191
$ collection ->load ();
153
192
154
193
return parent ::_beforeToHtml ();
155
194
}
156
195
157
196
/**
158
- * Retrieve Toolbar block
197
+ * Add toolbar block from product listing layout
198
+ *
199
+ * @param Collection $collection
200
+ */
201
+ private function addToolbarBlock (Collection $ collection )
202
+ {
203
+ $ toolbarLayout = $ this ->getToolbarFromLayout ();
204
+
205
+ if ($ toolbarLayout ) {
206
+ $ this ->configureToolbar ($ toolbarLayout , $ collection );
207
+ }
208
+ }
209
+
210
+ /**
211
+ * Retrieve Toolbar block from layout or a default Toolbar
159
212
*
160
213
* @return Toolbar
161
214
*/
162
215
public function getToolbarBlock ()
216
+ {
217
+ $ block = $ this ->getToolbarFromLayout ();
218
+
219
+ if (!$ block ) {
220
+ $ block = $ this ->getLayout ()->createBlock ($ this ->_defaultToolbarBlock , uniqid (microtime ()));
221
+ }
222
+
223
+ return $ block ;
224
+ }
225
+
226
+ /**
227
+ * Get toolbar block from layout
228
+ *
229
+ * @return bool|Toolbar
230
+ */
231
+ private function getToolbarFromLayout ()
163
232
{
164
233
$ blockName = $ this ->getToolbarBlockName ();
234
+
235
+ $ toolbarLayout = false ;
236
+
165
237
if ($ blockName ) {
166
- $ block = $ this ->getLayout ()->getBlock ($ blockName );
167
- if ($ block ) {
168
- return $ block ;
169
- }
238
+ $ toolbarLayout = $ this ->getLayout ()->getBlock ($ blockName );
170
239
}
171
- $ block = $ this -> getLayout ()-> createBlock ( $ this -> _defaultToolbarBlock , uniqid ( microtime ()));
172
- return $ block ;
240
+
241
+ return $ toolbarLayout ;
173
242
}
174
243
175
244
/**
@@ -203,7 +272,7 @@ public function setCollection($collection)
203
272
}
204
273
205
274
/**
206
- * @param array|string|integer|\Magento\Framework\App\Config\ Element $code
275
+ * @param array|string|integer| Element $code
207
276
* @return $this
208
277
*/
209
278
public function addAttribute ($ code )
@@ -223,7 +292,7 @@ public function getPriceBlockTemplate()
223
292
/**
224
293
* Retrieve Catalog Config object
225
294
*
226
- * @return \Magento\Catalog\Model\ Config
295
+ * @return Config
227
296
*/
228
297
protected function _getConfig ()
229
298
{
@@ -233,8 +302,8 @@ protected function _getConfig()
233
302
/**
234
303
* Prepare Sort By fields from Category Data
235
304
*
236
- * @param \Magento\Catalog\Model\ Category $category
237
- * @return \Magento\Catalog\Block\Product\ListProduct
305
+ * @param Category $category
306
+ * @return $this
238
307
*/
239
308
public function prepareSortableFieldsByCategory ($ category )
240
309
{
@@ -278,38 +347,38 @@ public function getIdentities()
278
347
/**
279
348
* Get post parameters
280
349
*
281
- * @param \Magento\Catalog\Model\ Product $product
350
+ * @param Product $product
282
351
* @return string
283
352
*/
284
- public function getAddToCartPostParams (\ Magento \ Catalog \ Model \ Product $ product )
353
+ public function getAddToCartPostParams (Product $ product )
285
354
{
286
355
$ url = $ this ->getAddToCartUrl ($ product );
287
356
return [
288
357
'action ' => $ url ,
289
358
'data ' => [
290
359
'product ' => $ product ->getEntityId (),
291
- \ Magento \ Framework \ App \ ActionInterface::PARAM_NAME_URL_ENCODED => $ this ->urlHelper ->getEncodedUrl ($ url ),
360
+ ActionInterface::PARAM_NAME_URL_ENCODED => $ this ->urlHelper ->getEncodedUrl ($ url ),
292
361
]
293
362
];
294
363
}
295
364
296
365
/**
297
- * @param \Magento\Catalog\Model\ Product $product
366
+ * @param Product $product
298
367
* @return string
299
368
*/
300
- public function getProductPrice (\ Magento \ Catalog \ Model \ Product $ product )
369
+ public function getProductPrice (Product $ product )
301
370
{
302
371
$ priceRender = $ this ->getPriceRender ();
303
372
304
373
$ price = '' ;
305
374
if ($ priceRender ) {
306
375
$ price = $ priceRender ->render (
307
- \ Magento \ Catalog \ Pricing \ Price \ FinalPrice::PRICE_CODE ,
376
+ FinalPrice::PRICE_CODE ,
308
377
$ product ,
309
378
[
310
379
'include_container ' => true ,
311
380
'display_minimal_price ' => true ,
312
- 'zone ' => \ Magento \ Framework \ Pricing \ Render::ZONE_ITEM_LIST ,
381
+ 'zone ' => Render::ZONE_ITEM_LIST ,
313
382
'list_category_page ' => true
314
383
]
315
384
);
@@ -322,7 +391,7 @@ public function getProductPrice(\Magento\Catalog\Model\Product $product)
322
391
* Specifies that price rendering should be done for the list of products
323
392
* i.e. rendering happens in the scope of product list, but not single product
324
393
*
325
- * @return \Magento\Framework\Pricing\ Render
394
+ * @return Render
326
395
*/
327
396
protected function getPriceRender ()
328
397
{
@@ -348,7 +417,7 @@ protected function getPriceRender()
348
417
private function initializeProductCollection ()
349
418
{
350
419
$ layer = $ this ->getLayer ();
351
- /* @var $layer \Magento\Catalog\Model\ Layer */
420
+ /* @var $layer Layer */
352
421
if ($ this ->getShowRootCategory ()) {
353
422
$ this ->setCategoryId ($ this ->_storeManager ->getStore ()->getRootCategoryId ());
354
423
}
@@ -386,9 +455,8 @@ private function initializeProductCollection()
386
455
if ($ origCategory ) {
387
456
$ layer ->setCurrentCategory ($ origCategory );
388
457
}
389
-
390
- $ toolbar = $ this ->getToolbarBlock ();
391
- $ this ->configureToolbar ($ toolbar , $ collection );
458
+
459
+ $ this ->addToolbarBlock ($ collection );
392
460
393
461
$ this ->_eventManager ->dispatch (
394
462
'catalog_block_product_list_collection ' ,
0 commit comments