Skip to content

Commit 75c9b64

Browse files
author
Alexander Akimov
authored
Merge pull request #3534 from magento-tsg/2.2-develop-pr63
[TSG] Backporting for 2.2 (pr63) (2.2.8)
2 parents 0d99623 + fb3fa61 commit 75c9b64

File tree

38 files changed

+1825
-70
lines changed

38 files changed

+1825
-70
lines changed

app/code/Magento/Catalog/Block/Product/ProductList/Toolbar.php

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
use Magento\Catalog\Helper\Product\ProductList;
99
use Magento\Catalog\Model\Product\ProductList\Toolbar as ToolbarModel;
10+
use Magento\Catalog\Model\Product\ProductList\ToolbarMemorizer;
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\App\Http\Context;
13+
use Magento\Framework\Data\Form\FormKey;
1014

1115
/**
1216
* Product list toolbar
@@ -77,6 +81,7 @@ class Toolbar extends \Magento\Framework\View\Element\Template
7781

7882
/**
7983
* @var bool $_paramsMemorizeAllowed
84+
* @deprecated
8085
*/
8186
protected $_paramsMemorizeAllowed = true;
8287

@@ -96,6 +101,7 @@ class Toolbar extends \Magento\Framework\View\Element\Template
96101
* Catalog session
97102
*
98103
* @var \Magento\Catalog\Model\Session
104+
* @deprecated
99105
*/
100106
protected $_catalogSession;
101107

@@ -104,6 +110,11 @@ class Toolbar extends \Magento\Framework\View\Element\Template
104110
*/
105111
protected $_toolbarModel;
106112

113+
/**
114+
* @var ToolbarMemorizer
115+
*/
116+
private $toolbarMemorizer;
117+
107118
/**
108119
* @var ProductList
109120
*/
@@ -119,6 +130,16 @@ class Toolbar extends \Magento\Framework\View\Element\Template
119130
*/
120131
protected $_postDataHelper;
121132

133+
/**
134+
* @var Context
135+
*/
136+
private $httpContext;
137+
138+
/**
139+
* @var FormKey
140+
*/
141+
private $formKey;
142+
122143
/**
123144
* @param \Magento\Framework\View\Element\Template\Context $context
124145
* @param \Magento\Catalog\Model\Session $catalogSession
@@ -128,6 +149,11 @@ class Toolbar extends \Magento\Framework\View\Element\Template
128149
* @param ProductList $productListHelper
129150
* @param \Magento\Framework\Data\Helper\PostHelper $postDataHelper
130151
* @param array $data
152+
* @param ToolbarMemorizer|null $toolbarMemorizer
153+
* @param Context|null $httpContext
154+
* @param FormKey|null $formKey
155+
*
156+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
131157
*/
132158
public function __construct(
133159
\Magento\Framework\View\Element\Template\Context $context,
@@ -137,21 +163,34 @@ public function __construct(
137163
\Magento\Framework\Url\EncoderInterface $urlEncoder,
138164
ProductList $productListHelper,
139165
\Magento\Framework\Data\Helper\PostHelper $postDataHelper,
140-
array $data = []
166+
array $data = [],
167+
ToolbarMemorizer $toolbarMemorizer = null,
168+
Context $httpContext = null,
169+
FormKey $formKey = null
141170
) {
142171
$this->_catalogSession = $catalogSession;
143172
$this->_catalogConfig = $catalogConfig;
144173
$this->_toolbarModel = $toolbarModel;
145174
$this->urlEncoder = $urlEncoder;
146175
$this->_productListHelper = $productListHelper;
147176
$this->_postDataHelper = $postDataHelper;
177+
$this->toolbarMemorizer = $toolbarMemorizer ?: ObjectManager::getInstance()->get(
178+
ToolbarMemorizer::class
179+
);
180+
$this->httpContext = $httpContext ?: ObjectManager::getInstance()->get(
181+
Context::class
182+
);
183+
$this->formKey = $formKey ?: ObjectManager::getInstance()->get(
184+
FormKey::class
185+
);
148186
parent::__construct($context, $data);
149187
}
150188

151189
/**
152190
* Disable list state params memorizing
153191
*
154192
* @return $this
193+
* @deprecated
155194
*/
156195
public function disableParamsMemorizing()
157196
{
@@ -165,6 +204,7 @@ public function disableParamsMemorizing()
165204
* @param string $param parameter name
166205
* @param mixed $value parameter value
167206
* @return $this
207+
* @deprecated
168208
*/
169209
protected function _memorizeParam($param, $value)
170210
{
@@ -244,13 +284,13 @@ public function getCurrentOrder()
244284
$defaultOrder = $keys[0];
245285
}
246286

247-
$order = $this->_toolbarModel->getOrder();
287+
$order = $this->toolbarMemorizer->getOrder();
248288
if (!$order || !isset($orders[$order])) {
249289
$order = $defaultOrder;
250290
}
251291

252-
if ($order != $defaultOrder) {
253-
$this->_memorizeParam('sort_order', $order);
292+
if ($this->toolbarMemorizer->isMemorizingAllowed()) {
293+
$this->httpContext->setValue(ToolbarModel::ORDER_PARAM_NAME, $order, $defaultOrder);
254294
}
255295

256296
$this->setData('_current_grid_order', $order);
@@ -270,13 +310,13 @@ public function getCurrentDirection()
270310
}
271311

272312
$directions = ['asc', 'desc'];
273-
$dir = strtolower($this->_toolbarModel->getDirection());
313+
$dir = strtolower($this->toolbarMemorizer->getDirection());
274314
if (!$dir || !in_array($dir, $directions)) {
275315
$dir = $this->_direction;
276316
}
277317

278-
if ($dir != $this->_direction) {
279-
$this->_memorizeParam('sort_direction', $dir);
318+
if ($this->toolbarMemorizer->isMemorizingAllowed()) {
319+
$this->httpContext->setValue(ToolbarModel::DIRECTION_PARAM_NAME, $dir, $this->_direction);
280320
}
281321

282322
$this->setData('_current_grid_direction', $dir);
@@ -392,6 +432,8 @@ public function getPagerUrl($params = [])
392432
}
393433

394434
/**
435+
* Get pager encoded url.
436+
*
395437
* @param array $params
396438
* @return string
397439
*/
@@ -412,11 +454,15 @@ public function getCurrentMode()
412454
return $mode;
413455
}
414456
$defaultMode = $this->_productListHelper->getDefaultViewMode($this->getModes());
415-
$mode = $this->_toolbarModel->getMode();
457+
$mode = $this->toolbarMemorizer->getMode();
416458
if (!$mode || !isset($this->_availableMode[$mode])) {
417459
$mode = $defaultMode;
418460
}
419461

462+
if ($this->toolbarMemorizer->isMemorizingAllowed()) {
463+
$this->httpContext->setValue(ToolbarModel::MODE_PARAM_NAME, $mode, $defaultMode);
464+
}
465+
420466
$this->setData('_current_grid_mode', $mode);
421467
return $mode;
422468
}
@@ -568,20 +614,22 @@ public function getLimit()
568614
$defaultLimit = $keys[0];
569615
}
570616

571-
$limit = $this->_toolbarModel->getLimit();
617+
$limit = $this->toolbarMemorizer->getLimit();
572618
if (!$limit || !isset($limits[$limit])) {
573619
$limit = $defaultLimit;
574620
}
575621

576-
if ($limit != $defaultLimit) {
577-
$this->_memorizeParam('limit_page', $limit);
622+
if ($this->toolbarMemorizer->isMemorizingAllowed()) {
623+
$this->httpContext->setValue(ToolbarModel::LIMIT_PARAM_NAME, $limit, $defaultLimit);
578624
}
579625

580626
$this->setData('_current_limit', $limit);
581627
return $limit;
582628
}
583629

584630
/**
631+
* Check if limit is current used in toolbar.
632+
*
585633
* @param int $limit
586634
* @return bool
587635
*/
@@ -591,6 +639,8 @@ public function isLimitCurrent($limit)
591639
}
592640

593641
/**
642+
* Pager number of items from which products started on current page.
643+
*
594644
* @return int
595645
*/
596646
public function getFirstNum()
@@ -600,6 +650,8 @@ public function getFirstNum()
600650
}
601651

602652
/**
653+
* Pager number of items products finished on current page.
654+
*
603655
* @return int
604656
*/
605657
public function getLastNum()
@@ -609,6 +661,8 @@ public function getLastNum()
609661
}
610662

611663
/**
664+
* Total number of products in current category.
665+
*
612666
* @return int
613667
*/
614668
public function getTotalNum()
@@ -617,6 +671,8 @@ public function getTotalNum()
617671
}
618672

619673
/**
674+
* Check if current page is the first.
675+
*
620676
* @return bool
621677
*/
622678
public function isFirstPage()
@@ -625,6 +681,8 @@ public function isFirstPage()
625681
}
626682

627683
/**
684+
* Return last page number.
685+
*
628686
* @return int
629687
*/
630688
public function getLastPageNum()
@@ -692,6 +750,8 @@ public function getWidgetOptionsJson(array $customOptions = [])
692750
'orderDefault' => $this->getOrderField(),
693751
'limitDefault' => $this->_productListHelper->getDefaultLimitPerPageValue($defaultMode),
694752
'url' => $this->getPagerUrl(),
753+
'formKey' => $this->formKey->getFormKey(),
754+
'post' => $this->toolbarMemorizer->isMemorizingAllowed() ? true : false,
695755
];
696756
$options = array_replace_recursive($options, $customOptions);
697757
return json_encode(['productListToolbarForm' => $options]);

app/code/Magento/Catalog/Controller/Category/View.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88

99
use Magento\Catalog\Api\CategoryRepositoryInterface;
1010
use Magento\Catalog\Model\Layer\Resolver;
11+
use Magento\Catalog\Model\Product\ProductList\ToolbarMemorizer;
1112
use Magento\Framework\Exception\NoSuchEntityException;
1213
use Magento\Framework\View\Result\PageFactory;
14+
use Magento\Framework\App\Action\Action;
1315

1416
/**
17+
* View a category on storefront. Needs to be accessible by POST because of the store switching.
18+
*
1519
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1620
*/
17-
class View extends \Magento\Framework\App\Action\Action
21+
class View extends Action
1822
{
1923
/**
2024
* Core registry
@@ -69,6 +73,11 @@ class View extends \Magento\Framework\App\Action\Action
6973
*/
7074
protected $categoryRepository;
7175

76+
/**
77+
* @var ToolbarMemorizer
78+
*/
79+
private $toolbarMemorizer;
80+
7281
/**
7382
* Constructor
7483
*
@@ -82,6 +91,7 @@ class View extends \Magento\Framework\App\Action\Action
8291
* @param \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory
8392
* @param Resolver $layerResolver
8493
* @param CategoryRepositoryInterface $categoryRepository
94+
* @param ToolbarMemorizer|null $toolbarMemorizer
8595
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
8696
*/
8797
public function __construct(
@@ -94,7 +104,8 @@ public function __construct(
94104
PageFactory $resultPageFactory,
95105
\Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory,
96106
Resolver $layerResolver,
97-
CategoryRepositoryInterface $categoryRepository
107+
CategoryRepositoryInterface $categoryRepository,
108+
ToolbarMemorizer $toolbarMemorizer = null
98109
) {
99110
parent::__construct($context);
100111
$this->_storeManager = $storeManager;
@@ -106,6 +117,7 @@ public function __construct(
106117
$this->resultForwardFactory = $resultForwardFactory;
107118
$this->layerResolver = $layerResolver;
108119
$this->categoryRepository = $categoryRepository;
120+
$this->toolbarMemorizer = $toolbarMemorizer ?: $context->getObjectManager()->get(ToolbarMemorizer::class);
109121
}
110122

111123
/**
@@ -130,6 +142,7 @@ protected function _initCategory()
130142
}
131143
$this->_catalogSession->setLastVisitedCategoryId($category->getId());
132144
$this->_coreRegistry->register('current_category', $category);
145+
$this->toolbarMemorizer->memorizeParams();
133146
try {
134147
$this->_eventManager->dispatch(
135148
'catalog_controller_category_init_after',
@@ -195,7 +208,7 @@ public function execute()
195208
if ($layoutUpdates && is_array($layoutUpdates)) {
196209
foreach ($layoutUpdates as $layoutUpdate) {
197210
$page->addUpdate($layoutUpdate);
198-
$page->addPageLayoutHandles(['layout_update' => md5($layoutUpdate)], null, false);
211+
$page->addPageLayoutHandles(['layout_update' => sha1($layoutUpdate)], null, false);
199212
}
200213
}
201214

0 commit comments

Comments
 (0)