Skip to content

Commit 9dbe444

Browse files
committed
Remove the product list helper from constructor for backward compatibility. Use the default toolbar to get current listing mode. Refactor the code.
1 parent 42072dd commit 9dbe444

File tree

1 file changed

+47
-27
lines changed

1 file changed

+47
-27
lines changed

app/code/Magento/Catalog/Block/Product/ListProduct.php

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Magento\Catalog\Api\CategoryRepositoryInterface;
1010
use Magento\Catalog\Block\Product\Context;
1111
use Magento\Catalog\Block\Product\ProductList\Toolbar;
12-
use Magento\Catalog\Helper\Product\ProductList;
1312
use Magento\Catalog\Model\Category;
1413
use Magento\Catalog\Model\Layer\Resolver;
1514
use Magento\Catalog\Model\Product;
@@ -65,13 +64,6 @@ class ListProduct extends AbstractProduct implements IdentityInterface
6564
protected $categoryRepository;
6665

6766
/**
68-
* @var \Magento\Catalog\Helper\Product\ProductList
69-
*/
70-
protected $productListHelper;
71-
72-
/**
73-
*
74-
* @param \Magento\Catalog\Helper\Product\ProductList $productListHelper
7567
* @param \Magento\Catalog\Block\Product\Context $context
7668
* @param \Magento\Framework\Data\Helper\PostHelper $postDataHelper
7769
* @param \Magento\Catalog\Model\Layer\Resolver $layerResolver
@@ -80,7 +72,6 @@ class ListProduct extends AbstractProduct implements IdentityInterface
8072
* @param array $data
8173
*/
8274
public function __construct(
83-
ProductList $productListHelper,
8475
Context $context,
8576
PostHelper $postDataHelper,
8677
Resolver $layerResolver,
@@ -92,7 +83,6 @@ public function __construct(
9283
$this->_postDataHelper = $postDataHelper;
9384
$this->categoryRepository = $categoryRepository;
9485
$this->urlHelper = $urlHelper;
95-
$this->productListHelper = $productListHelper;
9686
parent::__construct(
9787
$context,
9888
$data
@@ -154,9 +144,31 @@ public function getMode()
154144
if ($this->getChildBlock('toolbar')) {
155145
return $this->getChildBlock('toolbar')->getCurrentMode();
156146
}
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());
147+
148+
return $this->getDefaultListingMode();
149+
}
150+
151+
/**
152+
* Get listing mode for products if toolbar is removed from layout.
153+
* Use the general configuration for product list mode from config path catalog/frontend/list_mode as default value
154+
// or mode data from block declaration from layout.
155+
*
156+
* @return string
157+
*/
158+
private function getDefaultListingMode()
159+
{
160+
// default Toolbar when the toolbar layout is not used
161+
$defaultToolbar = $this->getToolbarBlock();
162+
$availableModes = $defaultToolbar->getModes();
163+
164+
// layout config mode
165+
$mode = $this->getData('mode');
166+
if (!$mode && !isset($availableModes[$mode])) {
167+
// default config mode
168+
$mode = $defaultToolbar->getCurrentMode();
169+
}
170+
171+
return $mode;
160172
}
161173

162174
/**
@@ -168,41 +180,49 @@ protected function _beforeToHtml()
168180
{
169181
$collection = $this->_getProductCollection();
170182

171-
$this->addToobarBlock($collection);
183+
$this->addToolbarBlock($collection);
172184

173185
$collection->load();
174186

175187
return parent::_beforeToHtml();
176188
}
177189

178190
/**
179-
* Add toolbar block to product listing
191+
* Add toolbar block from product listing layout
180192
*
181193
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
182194
*/
183-
private function addToobarBlock(Collection $collection)
195+
private function addToolbarBlock(Collection $collection)
184196
{
185-
$toolbar = $this->getToolbarBlock();
186-
if ($toolbar) {
187-
$this->configureToolbar($toolbar, $collection);
197+
$toolbarLayout = false;
198+
199+
$blockName = $this->getToolbarBlockName();
200+
201+
if ($blockName) {
202+
$toolbarLayout = $this->getLayout()->getBlock($blockName);
203+
}
204+
205+
if ($toolbarLayout) {
206+
$this->configureToolbar($toolbarLayout, $collection);
188207
}
189208
}
190209

191210
/**
192-
* Retrieve Toolbar block
211+
* Retrieve Toolbar block from layout or a default Toolbar
193212
*
194-
* @return Toolbar|false
213+
* @return Toolbar
195214
*/
196215
public function getToolbarBlock()
197216
{
198-
$block = false;
199-
200217
$blockName = $this->getToolbarBlockName();
201-
if (!$blockName) {
202-
return $block;
218+
219+
if ($blockName) {
220+
$block = $this->getLayout()->getBlock($blockName);
203221
}
204222

205-
$block = $this->getLayout()->getBlock($blockName);
223+
if (!$block) {
224+
$block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, uniqid(microtime()));
225+
}
206226

207227
return $block;
208228
}
@@ -422,7 +442,7 @@ private function initializeProductCollection()
422442
$layer->setCurrentCategory($origCategory);
423443
}
424444

425-
$this->addToobarBlock($collection);
445+
$this->addToolbarBlock($collection);
426446

427447
$this->_eventManager->dispatch(
428448
'catalog_block_product_list_collection',

0 commit comments

Comments
 (0)