Skip to content

Commit

Permalink
Remove the product list helper from constructor for backward compatib…
Browse files Browse the repository at this point in the history
…ility. Use the default toolbar to get current listing mode. Refactor the code.
  • Loading branch information
mariuscris committed Oct 16, 2017
1 parent 42072dd commit 9dbe444
Showing 1 changed file with 47 additions and 27 deletions.
74 changes: 47 additions & 27 deletions app/code/Magento/Catalog/Block/Product/ListProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Block\Product\Context;
use Magento\Catalog\Block\Product\ProductList\Toolbar;
use Magento\Catalog\Helper\Product\ProductList;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Catalog\Model\Product;
Expand Down Expand Up @@ -65,13 +64,6 @@ class ListProduct extends AbstractProduct implements IdentityInterface
protected $categoryRepository;

/**
* @var \Magento\Catalog\Helper\Product\ProductList
*/
protected $productListHelper;

/**
*
* @param \Magento\Catalog\Helper\Product\ProductList $productListHelper
* @param \Magento\Catalog\Block\Product\Context $context
* @param \Magento\Framework\Data\Helper\PostHelper $postDataHelper
* @param \Magento\Catalog\Model\Layer\Resolver $layerResolver
Expand All @@ -80,7 +72,6 @@ class ListProduct extends AbstractProduct implements IdentityInterface
* @param array $data
*/
public function __construct(
ProductList $productListHelper,
Context $context,
PostHelper $postDataHelper,
Resolver $layerResolver,
Expand All @@ -92,7 +83,6 @@ public function __construct(
$this->_postDataHelper = $postDataHelper;
$this->categoryRepository = $categoryRepository;
$this->urlHelper = $urlHelper;
$this->productListHelper = $productListHelper;
parent::__construct(
$context,
$data
Expand Down Expand Up @@ -154,9 +144,31 @@ public function getMode()
if ($this->getChildBlock('toolbar')) {
return $this->getChildBlock('toolbar')->getCurrentMode();
}
// if toolbar is removed from layout, use the general configuration for product list mode
// - config path catalog/frontend/list_mode
return $this->productListHelper->getDefaultViewMode($this->getModes());

return $this->getDefaultListingMode();
}

/**
* Get listing mode for products if toolbar is removed from layout.
* Use the general configuration for product list mode from config path catalog/frontend/list_mode as default value
// or mode data from block declaration from layout.
*
* @return string
*/
private function getDefaultListingMode()
{
// default Toolbar when the toolbar layout is not used
$defaultToolbar = $this->getToolbarBlock();
$availableModes = $defaultToolbar->getModes();

// layout config mode
$mode = $this->getData('mode');
if (!$mode && !isset($availableModes[$mode])) {
// default config mode
$mode = $defaultToolbar->getCurrentMode();
}

return $mode;
}

/**
Expand All @@ -168,41 +180,49 @@ protected function _beforeToHtml()
{
$collection = $this->_getProductCollection();

$this->addToobarBlock($collection);
$this->addToolbarBlock($collection);

$collection->load();

return parent::_beforeToHtml();
}

/**
* Add toolbar block to product listing
* Add toolbar block from product listing layout
*
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
*/
private function addToobarBlock(Collection $collection)
private function addToolbarBlock(Collection $collection)
{
$toolbar = $this->getToolbarBlock();
if ($toolbar) {
$this->configureToolbar($toolbar, $collection);
$toolbarLayout = false;

$blockName = $this->getToolbarBlockName();

if ($blockName) {
$toolbarLayout = $this->getLayout()->getBlock($blockName);
}

if ($toolbarLayout) {
$this->configureToolbar($toolbarLayout, $collection);
}
}

/**
* Retrieve Toolbar block
* Retrieve Toolbar block from layout or a default Toolbar
*
* @return Toolbar|false
* @return Toolbar
*/
public function getToolbarBlock()
{
$block = false;

$blockName = $this->getToolbarBlockName();
if (!$blockName) {
return $block;

if ($blockName) {
$block = $this->getLayout()->getBlock($blockName);
}

$block = $this->getLayout()->getBlock($blockName);
if (!$block) {
$block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, uniqid(microtime()));
}

return $block;
}
Expand Down Expand Up @@ -422,7 +442,7 @@ private function initializeProductCollection()
$layer->setCurrentCategory($origCategory);
}

$this->addToobarBlock($collection);
$this->addToolbarBlock($collection);

$this->_eventManager->dispatch(
'catalog_block_product_list_collection',
Expand Down

0 comments on commit 9dbe444

Please sign in to comment.