7
7
8
8
use Magento \Catalog \Helper \Product \ProductList ;
9
9
use Magento \Catalog \Model \Product \ProductList \Toolbar as ToolbarModel ;
10
+ use Magento \Catalog \Model \Product \ProductList \ToolbarMemorizer ;
11
+ use Magento \Framework \App \ObjectManager ;
10
12
11
13
/**
12
14
* Product list toolbar
@@ -77,6 +79,7 @@ class Toolbar extends \Magento\Framework\View\Element\Template
77
79
78
80
/**
79
81
* @var bool $_paramsMemorizeAllowed
82
+ * @deprecated
80
83
*/
81
84
protected $ _paramsMemorizeAllowed = true ;
82
85
@@ -96,6 +99,7 @@ class Toolbar extends \Magento\Framework\View\Element\Template
96
99
* Catalog session
97
100
*
98
101
* @var \Magento\Catalog\Model\Session
102
+ * @deprecated
99
103
*/
100
104
protected $ _catalogSession ;
101
105
@@ -104,6 +108,11 @@ class Toolbar extends \Magento\Framework\View\Element\Template
104
108
*/
105
109
protected $ _toolbarModel ;
106
110
111
+ /**
112
+ * @var ToolbarMemorizer
113
+ */
114
+ private $ toolbarMemorizer ;
115
+
107
116
/**
108
117
* @var ProductList
109
118
*/
@@ -119,6 +128,16 @@ class Toolbar extends \Magento\Framework\View\Element\Template
119
128
*/
120
129
protected $ _postDataHelper ;
121
130
131
+ /**
132
+ * @var \Magento\Framework\App\Http\Context
133
+ */
134
+ private $ httpContext ;
135
+
136
+ /**
137
+ * @var \Magento\Framework\Data\Form\FormKey
138
+ */
139
+ private $ formKey ;
140
+
122
141
/**
123
142
* @param \Magento\Framework\View\Element\Template\Context $context
124
143
* @param \Magento\Catalog\Model\Session $catalogSession
@@ -128,6 +147,11 @@ class Toolbar extends \Magento\Framework\View\Element\Template
128
147
* @param ProductList $productListHelper
129
148
* @param \Magento\Framework\Data\Helper\PostHelper $postDataHelper
130
149
* @param array $data
150
+ * @param ToolbarMemorizer|null $toolbarMemorizer
151
+ * @param \Magento\Framework\App\Http\Context|null $httpContext
152
+ * @param \Magento\Framework\Data\Form\FormKey|null $formKey
153
+ *
154
+ * @SuppressWarnings(PHPMD.ExcessiveParameterList)
131
155
*/
132
156
public function __construct (
133
157
\Magento \Framework \View \Element \Template \Context $ context ,
@@ -137,21 +161,34 @@ public function __construct(
137
161
\Magento \Framework \Url \EncoderInterface $ urlEncoder ,
138
162
ProductList $ productListHelper ,
139
163
\Magento \Framework \Data \Helper \PostHelper $ postDataHelper ,
140
- array $ data = []
164
+ array $ data = [],
165
+ ToolbarMemorizer $ toolbarMemorizer = null ,
166
+ \Magento \Framework \App \Http \Context $ httpContext = null ,
167
+ \Magento \Framework \Data \Form \FormKey $ formKey = null
141
168
) {
142
169
$ this ->_catalogSession = $ catalogSession ;
143
170
$ this ->_catalogConfig = $ catalogConfig ;
144
171
$ this ->_toolbarModel = $ toolbarModel ;
145
172
$ this ->urlEncoder = $ urlEncoder ;
146
173
$ this ->_productListHelper = $ productListHelper ;
147
174
$ this ->_postDataHelper = $ postDataHelper ;
175
+ $ this ->toolbarMemorizer = $ toolbarMemorizer ?: ObjectManager::getInstance ()->get (
176
+ ToolbarMemorizer::class
177
+ );
178
+ $ this ->httpContext = $ httpContext ?: ObjectManager::getInstance ()->get (
179
+ \Magento \Framework \App \Http \Context::class
180
+ );
181
+ $ this ->formKey = $ formKey ?: ObjectManager::getInstance ()->get (
182
+ \Magento \Framework \Data \Form \FormKey::class
183
+ );
148
184
parent ::__construct ($ context , $ data );
149
185
}
150
186
151
187
/**
152
188
* Disable list state params memorizing
153
189
*
154
190
* @return $this
191
+ * @deprecated
155
192
*/
156
193
public function disableParamsMemorizing ()
157
194
{
@@ -165,6 +202,7 @@ public function disableParamsMemorizing()
165
202
* @param string $param parameter name
166
203
* @param mixed $value parameter value
167
204
* @return $this
205
+ * @deprecated
168
206
*/
169
207
protected function _memorizeParam ($ param , $ value )
170
208
{
@@ -244,13 +282,13 @@ public function getCurrentOrder()
244
282
$ defaultOrder = $ keys [0 ];
245
283
}
246
284
247
- $ order = $ this ->_toolbarModel ->getOrder ();
285
+ $ order = $ this ->toolbarMemorizer ->getOrder ();
248
286
if (!$ order || !isset ($ orders [$ order ])) {
249
287
$ order = $ defaultOrder ;
250
288
}
251
289
252
- if ($ order != $ defaultOrder ) {
253
- $ this ->_memorizeParam ( ' sort_order ' , $ order );
290
+ if ($ this -> toolbarMemorizer -> isMemorizingAllowed () ) {
291
+ $ this ->httpContext -> setValue (ToolbarModel:: ORDER_PARAM_NAME , $ order, $ defaultOrder );
254
292
}
255
293
256
294
$ this ->setData ('_current_grid_order ' , $ order );
@@ -270,13 +308,13 @@ public function getCurrentDirection()
270
308
}
271
309
272
310
$ directions = ['asc ' , 'desc ' ];
273
- $ dir = strtolower ($ this ->_toolbarModel ->getDirection ());
311
+ $ dir = strtolower ($ this ->toolbarMemorizer ->getDirection ());
274
312
if (!$ dir || !in_array ($ dir , $ directions )) {
275
313
$ dir = $ this ->_direction ;
276
314
}
277
315
278
- if ($ dir != $ this ->_direction ) {
279
- $ this ->_memorizeParam ( ' sort_direction ' , $ dir );
316
+ if ($ this ->toolbarMemorizer -> isMemorizingAllowed () ) {
317
+ $ this ->httpContext -> setValue (ToolbarModel:: DIRECTION_PARAM_NAME , $ dir, $ this -> _direction );
280
318
}
281
319
282
320
$ this ->setData ('_current_grid_direction ' , $ dir );
@@ -392,6 +430,8 @@ public function getPagerUrl($params = [])
392
430
}
393
431
394
432
/**
433
+ * Get pager encoded url.
434
+ *
395
435
* @param array $params
396
436
* @return string
397
437
*/
@@ -412,11 +452,15 @@ public function getCurrentMode()
412
452
return $ mode ;
413
453
}
414
454
$ defaultMode = $ this ->_productListHelper ->getDefaultViewMode ($ this ->getModes ());
415
- $ mode = $ this ->_toolbarModel ->getMode ();
455
+ $ mode = $ this ->toolbarMemorizer ->getMode ();
416
456
if (!$ mode || !isset ($ this ->_availableMode [$ mode ])) {
417
457
$ mode = $ defaultMode ;
418
458
}
419
459
460
+ if ($ this ->toolbarMemorizer ->isMemorizingAllowed ()) {
461
+ $ this ->httpContext ->setValue (ToolbarModel::MODE_PARAM_NAME , $ mode , $ defaultMode );
462
+ }
463
+
420
464
$ this ->setData ('_current_grid_mode ' , $ mode );
421
465
return $ mode ;
422
466
}
@@ -568,20 +612,22 @@ public function getLimit()
568
612
$ defaultLimit = $ keys [0 ];
569
613
}
570
614
571
- $ limit = $ this ->_toolbarModel ->getLimit ();
615
+ $ limit = $ this ->toolbarMemorizer ->getLimit ();
572
616
if (!$ limit || !isset ($ limits [$ limit ])) {
573
617
$ limit = $ defaultLimit ;
574
618
}
575
619
576
- if ($ limit != $ defaultLimit ) {
577
- $ this ->_memorizeParam ( ' limit_page ' , $ limit );
620
+ if ($ this -> toolbarMemorizer -> isMemorizingAllowed () ) {
621
+ $ this ->httpContext -> setValue (ToolbarModel:: LIMIT_PARAM_NAME , $ limit, $ defaultLimit );
578
622
}
579
623
580
624
$ this ->setData ('_current_limit ' , $ limit );
581
625
return $ limit ;
582
626
}
583
627
584
628
/**
629
+ * Check if limit is current used in toolbar.
630
+ *
585
631
* @param int $limit
586
632
* @return bool
587
633
*/
@@ -591,6 +637,8 @@ public function isLimitCurrent($limit)
591
637
}
592
638
593
639
/**
640
+ * Pager number of items from which products started on current page.
641
+ *
594
642
* @return int
595
643
*/
596
644
public function getFirstNum ()
@@ -600,6 +648,8 @@ public function getFirstNum()
600
648
}
601
649
602
650
/**
651
+ * Pager number of items products finished on current page.
652
+ *
603
653
* @return int
604
654
*/
605
655
public function getLastNum ()
@@ -609,6 +659,8 @@ public function getLastNum()
609
659
}
610
660
611
661
/**
662
+ * Total number of products in current category.
663
+ *
612
664
* @return int
613
665
*/
614
666
public function getTotalNum ()
@@ -617,6 +669,8 @@ public function getTotalNum()
617
669
}
618
670
619
671
/**
672
+ * Check if current page is the first.
673
+ *
620
674
* @return bool
621
675
*/
622
676
public function isFirstPage ()
@@ -625,6 +679,8 @@ public function isFirstPage()
625
679
}
626
680
627
681
/**
682
+ * Return last page number.
683
+ *
628
684
* @return int
629
685
*/
630
686
public function getLastPageNum ()
@@ -692,6 +748,8 @@ public function getWidgetOptionsJson(array $customOptions = [])
692
748
'orderDefault ' => $ this ->getOrderField (),
693
749
'limitDefault ' => $ this ->_productListHelper ->getDefaultLimitPerPageValue ($ defaultMode ),
694
750
'url ' => $ this ->getPagerUrl (),
751
+ 'formKey ' => $ this ->formKey ->getFormKey (),
752
+ 'post ' => $ this ->toolbarMemorizer ->isMemorizingAllowed () ? true : false
695
753
];
696
754
$ options = array_replace_recursive ($ options , $ customOptions );
697
755
return json_encode (['productListToolbarForm ' => $ options ]);
0 commit comments