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
{
@@ -286,38 +355,38 @@ public function getIdentities()
286
355
/**
287
356
* Get post parameters
288
357
*
289
- * @param \Magento\Catalog\Model\ Product $product
358
+ * @param Product $product
290
359
* @return string
291
360
*/
292
- public function getAddToCartPostParams (\ Magento \ Catalog \ Model \ Product $ product )
361
+ public function getAddToCartPostParams (Product $ product )
293
362
{
294
363
$ url = $ this ->getAddToCartUrl ($ product );
295
364
return [
296
365
'action ' => $ url ,
297
366
'data ' => [
298
367
'product ' => $ product ->getEntityId (),
299
- \ Magento \ Framework \ App \ ActionInterface::PARAM_NAME_URL_ENCODED => $ this ->urlHelper ->getEncodedUrl ($ url ),
368
+ ActionInterface::PARAM_NAME_URL_ENCODED => $ this ->urlHelper ->getEncodedUrl ($ url ),
300
369
]
301
370
];
302
371
}
303
372
304
373
/**
305
- * @param \Magento\Catalog\Model\ Product $product
374
+ * @param Product $product
306
375
* @return string
307
376
*/
308
- public function getProductPrice (\ Magento \ Catalog \ Model \ Product $ product )
377
+ public function getProductPrice (Product $ product )
309
378
{
310
379
$ priceRender = $ this ->getPriceRender ();
311
380
312
381
$ price = '' ;
313
382
if ($ priceRender ) {
314
383
$ price = $ priceRender ->render (
315
- \ Magento \ Catalog \ Pricing \ Price \ FinalPrice::PRICE_CODE ,
384
+ FinalPrice::PRICE_CODE ,
316
385
$ product ,
317
386
[
318
387
'include_container ' => true ,
319
388
'display_minimal_price ' => true ,
320
- 'zone ' => \ Magento \ Framework \ Pricing \ Render::ZONE_ITEM_LIST ,
389
+ 'zone ' => Render::ZONE_ITEM_LIST ,
321
390
'list_category_page ' => true
322
391
]
323
392
);
@@ -330,7 +399,7 @@ public function getProductPrice(\Magento\Catalog\Model\Product $product)
330
399
* Specifies that price rendering should be done for the list of products
331
400
* i.e. rendering happens in the scope of product list, but not single product
332
401
*
333
- * @return \Magento\Framework\Pricing\ Render
402
+ * @return Render
334
403
*/
335
404
protected function getPriceRender ()
336
405
{
@@ -356,7 +425,7 @@ protected function getPriceRender()
356
425
private function initializeProductCollection ()
357
426
{
358
427
$ layer = $ this ->getLayer ();
359
- /* @var $layer \Magento\Catalog\Model\ Layer */
428
+ /* @var $layer Layer */
360
429
if ($ this ->getShowRootCategory ()) {
361
430
$ this ->setCategoryId ($ this ->_storeManager ->getStore ()->getRootCategoryId ());
362
431
}
@@ -395,8 +464,7 @@ private function initializeProductCollection()
395
464
$ layer ->setCurrentCategory ($ origCategory );
396
465
}
397
466
398
- $ toolbar = $ this ->getToolbarBlock ();
399
- $ this ->configureToolbar ($ toolbar , $ collection );
467
+ $ this ->addToolbarBlock ($ collection );
400
468
401
469
$ this ->_eventManager ->dispatch (
402
470
'catalog_block_product_list_collection ' ,
0 commit comments