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