7
7
namespace Magento \Catalog \Block \Product ;
8
8
9
9
use Magento \Catalog \Api \CategoryRepositoryInterface ;
10
+ use Magento \Catalog \Block \Product \Context ;
10
11
use Magento \Catalog \Block \Product \ProductList \Toolbar ;
12
+ use Magento \Catalog \Helper \Product \ProductList ;
11
13
use Magento \Catalog \Model \Category ;
14
+ use Magento \Catalog \Model \Layer \Resolver ;
12
15
use Magento \Catalog \Model \Product ;
13
16
use Magento \Catalog \Model \ResourceModel \Product \Collection ;
14
17
use Magento \Eav \Model \Entity \Collection \AbstractCollection ;
18
+ use Magento \Framework \Data \Helper \PostHelper ;
15
19
use Magento \Framework \DataObject \IdentityInterface ;
16
20
use Magento \Framework \Exception \NoSuchEntityException ;
21
+ use Magento \Framework \Url \Helper \Data ;
17
22
18
23
/**
19
24
* Product list
@@ -33,7 +38,7 @@ class ListProduct extends AbstractProduct implements IdentityInterface
33
38
/**
34
39
* Product Collection
35
40
*
36
- * @var AbstractCollection
41
+ * @var \Magento\Eav\Model\Entity\Collection\ AbstractCollection
37
42
*/
38
43
protected $ _productCollection ;
39
44
@@ -55,30 +60,39 @@ class ListProduct extends AbstractProduct implements IdentityInterface
55
60
protected $ urlHelper ;
56
61
57
62
/**
58
- * @var CategoryRepositoryInterface
63
+ * @var \Magento\Catalog\Api\ CategoryRepositoryInterface
59
64
*/
60
65
protected $ categoryRepository ;
61
66
62
67
/**
63
- * @param Context $context
68
+ * @var \Magento\Catalog\Helper\Product\ProductList
69
+ */
70
+ protected $ productListHelper ;
71
+
72
+ /**
73
+ *
74
+ * @param \Magento\Catalog\Helper\Product\ProductList $productListHelper
75
+ * @param \Magento\Catalog\Block\Product\Context $context
64
76
* @param \Magento\Framework\Data\Helper\PostHelper $postDataHelper
65
77
* @param \Magento\Catalog\Model\Layer\Resolver $layerResolver
66
- * @param CategoryRepositoryInterface $categoryRepository
78
+ * @param \Magento\Catalog\Api\ CategoryRepositoryInterface $categoryRepository
67
79
* @param \Magento\Framework\Url\Helper\Data $urlHelper
68
80
* @param array $data
69
81
*/
70
82
public function __construct (
71
- \Magento \Catalog \Block \Product \Context $ context ,
72
- \Magento \Framework \Data \Helper \PostHelper $ postDataHelper ,
73
- \Magento \Catalog \Model \Layer \Resolver $ layerResolver ,
83
+ ProductList $ productListHelper ,
84
+ Context $ context ,
85
+ PostHelper $ postDataHelper ,
86
+ Resolver $ layerResolver ,
74
87
CategoryRepositoryInterface $ categoryRepository ,
75
- \ Magento \ Framework \ Url \ Helper \ Data $ urlHelper ,
88
+ Data $ urlHelper ,
76
89
array $ data = []
77
90
) {
78
91
$ this ->_catalogLayer = $ layerResolver ->get ();
79
92
$ this ->_postDataHelper = $ postDataHelper ;
80
93
$ this ->categoryRepository = $ categoryRepository ;
81
94
$ this ->urlHelper = $ urlHelper ;
95
+ $ this ->productListHelper = $ productListHelper ;
82
96
parent ::__construct (
83
97
$ context ,
84
98
$ data
@@ -137,7 +151,12 @@ public function getLoadedProductCollection()
137
151
*/
138
152
public function getMode ()
139
153
{
140
- return $ this ->getChildBlock ('toolbar ' )->getCurrentMode ();
154
+ if ($ this ->getChildBlock ('toolbar ' )) {
155
+ return $ this ->getChildBlock ('toolbar ' )->getCurrentMode ();
156
+ }
157
+ // if toolbar is removed from layout, use the general configuration for product list mode
158
+ // - config path catalog/frontend/list_mode
159
+ return $ this ->productListHelper ->getDefaultViewMode ($ this ->getModes ());
141
160
}
142
161
143
162
/**
@@ -148,27 +167,43 @@ public function getMode()
148
167
protected function _beforeToHtml ()
149
168
{
150
169
$ collection = $ this ->_getProductCollection ();
151
- $ this ->configureToolbar ($ this ->getToolbarBlock (), $ collection );
170
+
171
+ $ this ->addToobarBlock ($ collection );
172
+
152
173
$ collection ->load ();
153
174
154
175
return parent ::_beforeToHtml ();
155
176
}
156
177
178
+ /**
179
+ * Add toolbar block to product listing
180
+ *
181
+ * @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
182
+ */
183
+ private function addToobarBlock (Collection $ collection )
184
+ {
185
+ $ toolbar = $ this ->getToolbarBlock ();
186
+ if ($ toolbar ) {
187
+ $ this ->configureToolbar ($ toolbar , $ collection );
188
+ }
189
+ }
190
+
157
191
/**
158
192
* Retrieve Toolbar block
159
193
*
160
- * @return Toolbar
194
+ * @return Toolbar|false
161
195
*/
162
196
public function getToolbarBlock ()
163
197
{
198
+ $ block = false ;
199
+
164
200
$ blockName = $ this ->getToolbarBlockName ();
165
- if ($ blockName ) {
166
- $ block = $ this ->getLayout ()->getBlock ($ blockName );
167
- if ($ block ) {
168
- return $ block ;
169
- }
201
+ if (!$ blockName ) {
202
+ return $ block ;
170
203
}
171
- $ block = $ this ->getLayout ()->createBlock ($ this ->_defaultToolbarBlock , uniqid (microtime ()));
204
+
205
+ $ block = $ this ->getLayout ()->getBlock ($ blockName );
206
+
172
207
return $ block ;
173
208
}
174
209
@@ -386,9 +421,8 @@ private function initializeProductCollection()
386
421
if ($ origCategory ) {
387
422
$ layer ->setCurrentCategory ($ origCategory );
388
423
}
389
-
390
- $ toolbar = $ this ->getToolbarBlock ();
391
- $ this ->configureToolbar ($ toolbar , $ collection );
424
+
425
+ $ this ->addToobarBlock ($ collection );
392
426
393
427
$ this ->_eventManager ->dispatch (
394
428
'catalog_block_product_list_collection ' ,
0 commit comments