Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for remove 'product_list_toolbar' block from layout in XML #9413 #11473

Merged
merged 12 commits into from
Nov 25, 2017
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 83 additions & 20 deletions app/code/Magento/Catalog/Block/Product/ListProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
namespace Magento\Catalog\Block\Product;

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Block\Product\Context;
ihor-sviziev marked this conversation as resolved.
Show resolved Hide resolved
use Magento\Catalog\Block\Product\ProductList\Toolbar;
use Magento\Catalog\Model\Category;
ihor-sviziev marked this conversation as resolved.
Show resolved Hide resolved
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
use Magento\Framework\Data\Helper\PostHelper;
use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Url\Helper\Data;

/**
* Product list
Expand All @@ -33,7 +37,7 @@ class ListProduct extends AbstractProduct implements IdentityInterface
/**
* Product Collection
*
* @var AbstractCollection
* @var \Magento\Eav\Model\Entity\Collection\AbstractCollection
ihor-sviziev marked this conversation as resolved.
Show resolved Hide resolved
*/
protected $_productCollection;

Expand All @@ -55,24 +59,24 @@ class ListProduct extends AbstractProduct implements IdentityInterface
protected $urlHelper;

/**
* @var CategoryRepositoryInterface
* @var \Magento\Catalog\Api\CategoryRepositoryInterface
*/
protected $categoryRepository;

/**
* @param Context $context
* @param \Magento\Catalog\Block\Product\Context $context
* @param \Magento\Framework\Data\Helper\PostHelper $postDataHelper
* @param \Magento\Catalog\Model\Layer\Resolver $layerResolver
* @param CategoryRepositoryInterface $categoryRepository
* @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
* @param \Magento\Framework\Url\Helper\Data $urlHelper
* @param array $data
*/
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Framework\Data\Helper\PostHelper $postDataHelper,
\Magento\Catalog\Model\Layer\Resolver $layerResolver,
Context $context,
PostHelper $postDataHelper,
Resolver $layerResolver,
CategoryRepositoryInterface $categoryRepository,
\Magento\Framework\Url\Helper\Data $urlHelper,
Data $urlHelper,
array $data = []
) {
$this->_catalogLayer = $layerResolver->get();
Expand Down Expand Up @@ -137,7 +141,35 @@ public function getLoadedProductCollection()
*/
public function getMode()
{
return $this->getChildBlock('toolbar')->getCurrentMode();
if ($this->getChildBlock('toolbar')) {
return $this->getChildBlock('toolbar')->getCurrentMode();
}

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 @@ -148,28 +180,60 @@ public function getMode()
protected function _beforeToHtml()
{
$collection = $this->_getProductCollection();
$this->configureToolbar($this->getToolbarBlock(), $collection);

$this->addToolbarBlock($collection);

$collection->load();

return parent::_beforeToHtml();
}

/**
* Retrieve Toolbar block
* Add toolbar block from product listing layout
*
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
*/
private function addToolbarBlock(Collection $collection)
{
$toolbarLayout = $this->getToolbarFromLayout();

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

/**
* Retrieve Toolbar block from layout or a default Toolbar
*
* @return Toolbar
*/
public function getToolbarBlock()
{
$block = $this->getToolbarFromLayout();

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

return $block;
}

/**
* Get toolbar block from layout
*
* @return bool|Toolbar
*/
private function getToolbarFromLayout()
{
$blockName = $this->getToolbarBlockName();

$toolbarLayout = false;

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

return $toolbarLayout;
}

/**
Expand Down Expand Up @@ -386,9 +450,8 @@ private function initializeProductCollection()
if ($origCategory) {
$layer->setCurrentCategory($origCategory);
}

$toolbar = $this->getToolbarBlock();
$this->configureToolbar($toolbar, $collection);

$this->addToolbarBlock($collection);

$this->_eventManager->dispatch(
'catalog_block_product_list_collection',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public function testToolbarCoverage()
$parent = $this->_getLayout()->createBlock(\Magento\Catalog\Block\Product\ListProduct::class, 'parent');

/* Prepare toolbar block */
$this->_getLayout()
->createBlock(\Magento\Catalog\Block\Product\ProductList\Toolbar::class, 'product_list_toolbar');
$parent->setToolbarBlockName('product_list_toolbar');
ihor-sviziev marked this conversation as resolved.
Show resolved Hide resolved

$toolbar = $parent->getToolbarBlock();
$this->assertInstanceOf(\Magento\Catalog\Block\Product\ProductList\Toolbar::class, $toolbar, 'Default Toolbar');

Expand Down