3
3
* Copyright 2014 Adobe
4
4
* All Rights Reserved.
5
5
*/
6
+
7
+ declare (strict_types=1 );
8
+
6
9
namespace Magento \Catalog \Controller \Category ;
7
10
8
11
use Magento \Catalog \Api \CategoryRepositoryInterface ;
11
14
use Magento \Catalog \Model \Category \Attribute \LayoutUpdateManager ;
12
15
use Magento \Catalog \Model \Design ;
13
16
use Magento \Catalog \Model \Layer \Resolver ;
14
- use Magento \Catalog \Model \Product \ProductList \ToolbarMemorizer ;
15
17
use Magento \Catalog \Model \Product \ProductList \Toolbar ;
18
+ use Magento \Catalog \Model \Product \ProductList \ToolbarMemorizer ;
16
19
use Magento \Catalog \Model \Session ;
17
20
use Magento \CatalogUrlRewrite \Model \CategoryUrlPathGenerator ;
18
21
use Magento \Framework \App \Action \Action ;
21
24
use Magento \Framework \App \Action \HttpPostActionInterface ;
22
25
use Magento \Framework \App \ActionInterface ;
23
26
use Magento \Framework \App \ObjectManager ;
27
+ use Magento \Framework \Controller \Result \Forward ;
24
28
use Magento \Framework \Controller \Result \ForwardFactory ;
25
- use Magento \Framework \Controller \ResultFactory ;
29
+ use Magento \Framework \Controller \Result \Redirect ;
30
+ use Magento \Framework \Controller \ResultInterface ;
26
31
use Magento \Framework \DataObject ;
27
32
use Magento \Framework \Exception \LocalizedException ;
28
33
use Magento \Framework \Exception \NoSuchEntityException ;
@@ -107,8 +112,6 @@ class View extends Action implements HttpGetActionInterface, HttpPostActionInter
107
112
private $ logger ;
108
113
109
114
/**
110
- * Constructor
111
- *
112
115
* @param Context $context
113
116
* @param Design $catalogDesign
114
117
* @param Session $catalogSession
@@ -121,8 +124,8 @@ class View extends Action implements HttpGetActionInterface, HttpPostActionInter
121
124
* @param CategoryRepositoryInterface $categoryRepository
122
125
* @param ToolbarMemorizer|null $toolbarMemorizer
123
126
* @param LayoutUpdateManager|null $layoutUpdateManager
124
- * @param CategoryHelper $categoryHelper
125
- * @param LoggerInterface $logger
127
+ * @param CategoryHelper|null $categoryHelper
128
+ * @param LoggerInterface|null $logger
126
129
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
127
130
*/
128
131
public function __construct (
@@ -161,9 +164,7 @@ public function __construct(
161
164
}
162
165
163
166
/**
164
- * Initialize requested category object
165
- *
166
- * @return Category|bool
167
+ * @return Category|false
167
168
*/
168
169
protected function _initCategory ()
169
170
{
@@ -189,109 +190,132 @@ protected function _initCategory()
189
190
['category ' => $ category , 'controller_action ' => $ this ]
190
191
);
191
192
} catch (LocalizedException $ e ) {
192
- $ this ->logger ->critical ($ e );
193
+ $ this ->logger ->critical (( string ) $ e );
193
194
return false ;
194
195
}
195
196
196
197
return $ category ;
197
198
}
198
199
199
200
/**
200
- * Category view action
201
- *
201
+ * @return ResultInterface
202
202
* @throws NoSuchEntityException
203
203
*/
204
204
public function execute ()
205
205
{
206
- $ result = null ;
207
-
208
206
if ($ this ->_request ->getParam (ActionInterface::PARAM_NAME_URL_ENCODED )) {
209
207
//phpcs:ignore Magento2.Legacy.ObsoleteResponse
210
208
return $ this ->resultRedirectFactory ->create ()->setUrl ($ this ->_redirect ->getRedirectUrl ());
211
209
}
212
210
213
211
$ category = $ this ->_initCategory ();
214
- if ($ category ) {
215
- // Negative ?p= value is not supported, redirect to a base version of category page.
216
- if ($ this ->_request ->getParam (Toolbar::PAGE_PARM_NAME ) < 0 ) {
217
- return $ this ->resultRedirectFactory ->create ()
218
- ->setHttpResponseCode (301 )
219
- ->setUrl ($ category ->getUrl ());
220
- }
212
+ if (!$ category ) {
213
+ return $ this ->resultForwardFactory ->create ()->forward ('noroute ' );
214
+ }
221
215
222
- $ this ->layerResolver ->create (Resolver::CATALOG_LAYER_CATEGORY );
223
- $ settings = $ this ->_catalogDesign ->getDesignSettings ($ category );
216
+ $ pageRedirect = $ this ->handlePageRedirect ($ category );
217
+ if ($ pageRedirect ) {
218
+ return $ pageRedirect ;
219
+ }
224
220
225
- // apply custom design
226
- if ($ settings ->getCustomDesign ()) {
227
- $ this ->_catalogDesign ->applyCustomDesign ($ settings ->getCustomDesign ());
228
- }
221
+ $ page = $ this ->preparePage ($ category );
229
222
230
- $ this ->_catalogSession ->setLastViewedCategoryId ($ category ->getId ());
223
+ if ($ this ->shouldRedirectOnToolbarAction ()) {
224
+ $ this ->getResponse ()->setRedirect ($ this ->_redirect ->getRedirectUrl ());
225
+ }
231
226
232
- $ page = $ this ->resultPageFactory ->create ();
233
- // apply custom layout (page) template once the blocks are generated
234
- if ($ settings ->getPageLayout ()) {
235
- $ page ->getConfig ()->setPageLayout ($ settings ->getPageLayout ());
236
- }
227
+ return $ page ;
228
+ }
237
229
238
- $ pageType = $ this ->getPageType ($ category );
230
+ /**
231
+ * @param Category $category
232
+ * @return Redirect|null
233
+ */
234
+ private function handlePageRedirect (Category $ category ): ?Redirect
235
+ {
236
+ if ($ this ->_request ->getParam (Toolbar::PAGE_PARM_NAME ) < 0 ) {
237
+ return $ this ->resultRedirectFactory ->create ()
238
+ ->setHttpResponseCode (301 )
239
+ ->setUrl ($ category ->getUrl ());
240
+ }
239
241
240
- if (!$ category ->hasChildren ()) {
241
- // Two levels removed from parent. Need to add default page type.
242
- $ parentPageType = strtok ($ pageType , '_ ' );
243
- $ page ->addPageLayoutHandles (['type ' => $ parentPageType ], null , false );
244
- }
245
- $ page ->addPageLayoutHandles (['type ' => $ pageType ], null , false );
246
- $ categoryDisplayMode = is_string ($ category ->getDisplayMode ()) ?
247
- strtolower ($ category ->getDisplayMode ()) : '' ;
248
- $ page ->addPageLayoutHandles (['displaymode ' => $ categoryDisplayMode ], null , false );
249
- $ page ->addPageLayoutHandles (['id ' => $ category ->getId ()]);
242
+ return null ;
243
+ }
250
244
251
- // apply custom layout update once layout is loaded
252
- $ this ->applyLayoutUpdates ($ page , $ settings );
245
+ /**
246
+ * @param Category $category
247
+ * @return Page
248
+ */
249
+ private function preparePage (Category $ category ): Page
250
+ {
251
+ $ this ->layerResolver ->create (Resolver::CATALOG_LAYER_CATEGORY );
252
+ $ settings = $ this ->_catalogDesign ->getDesignSettings ($ category );
253
+
254
+ if ($ settings ->getCustomDesign ()) {
255
+ $ this ->_catalogDesign ->applyCustomDesign ($ settings ->getCustomDesign ());
256
+ }
253
257
254
- $ page ->getConfig ()->addBodyClass ('page-products ' )
255
- ->addBodyClass ('categorypath- ' . $ this ->categoryUrlPathGenerator ->getUrlPath ($ category ))
256
- ->addBodyClass ('category- ' . $ category ->getUrlKey ());
258
+ $ this ->_catalogSession ->setLastViewedCategoryId ($ category ->getId ());
257
259
258
- if ($ this ->shouldRedirectOnToolbarAction ()) {
259
- $ this ->getResponse ()->setRedirect ($ this ->_redirect ->getRedirectUrl ());
260
- }
261
- return $ page ;
262
- } elseif (!$ this ->getResponse ()->isRedirect ()) {
263
- $ result = $ this ->resultForwardFactory ->create ()->forward ('noroute ' );
260
+ $ page = $ this ->resultPageFactory ->create ();
261
+ if ($ settings ->getPageLayout ()) {
262
+ $ page ->getConfig ()->setPageLayout ($ settings ->getPageLayout ());
264
263
}
265
- return $ result ;
264
+
265
+ $ this ->addPageLayoutHandles ($ page , $ category );
266
+ $ this ->applyLayoutUpdates ($ page , $ settings );
267
+
268
+ $ page ->getConfig ()->addBodyClass ('page-products ' )
269
+ ->addBodyClass ('categorypath- ' . $ this ->categoryUrlPathGenerator ->getUrlPath ($ category ))
270
+ ->addBodyClass ('category- ' . $ category ->getUrlKey ());
271
+
272
+ return $ page ;
273
+ }
274
+
275
+ /**
276
+ * @param Page $page
277
+ * @param Category $category
278
+ * @return void
279
+ */
280
+ private function addPageLayoutHandles (Page $ page , Category $ category ): void
281
+ {
282
+ $ pageType = $ this ->getPageType ($ category );
283
+
284
+ if (!$ category ->hasChildren ()) {
285
+ $ parentPageType = strtok ($ pageType , '_ ' );
286
+ $ page ->addPageLayoutHandles (['type ' => $ parentPageType ], null , false );
287
+ }
288
+ $ page ->addPageLayoutHandles (['type ' => $ pageType ], null , false );
289
+
290
+ $ categoryDisplayMode = is_string ($ category ->getDisplayMode ()) ?
291
+ strtolower ($ category ->getDisplayMode ()) : '' ;
292
+ $ page ->addPageLayoutHandles (['displaymode ' => $ categoryDisplayMode ], null , false );
293
+ $ page ->addPageLayoutHandles (['id ' => $ category ->getId ()]);
266
294
}
267
295
268
296
/**
269
- * Get page type based on category
270
- *
271
297
* @param Category $category
272
298
* @return string
273
299
*/
274
- private function getPageType (Category $ category ) : string
300
+ private function getPageType (Category $ category ): string
275
301
{
276
302
$ hasChildren = $ category ->hasChildren ();
277
303
if ($ category ->getIsAnchor ()) {
278
- return $ hasChildren ? 'layered ' : 'layered_without_children ' ;
304
+ return $ hasChildren ? 'layered ' : 'layered_without_children ' ;
279
305
}
280
306
281
307
return $ hasChildren ? 'default ' : 'default_without_children ' ;
282
308
}
283
309
284
310
/**
285
- * Apply custom layout updates
286
- *
287
311
* @param Page $page
288
312
* @param DataObject $settings
289
313
* @return void
290
314
*/
291
315
private function applyLayoutUpdates (
292
316
Page $ page ,
293
317
DataObject $ settings
294
- ) {
318
+ ): void {
295
319
$ layoutUpdates = $ settings ->getLayoutUpdates ();
296
320
if ($ layoutUpdates && is_array ($ layoutUpdates )) {
297
321
foreach ($ layoutUpdates as $ layoutUpdate ) {
@@ -300,26 +324,23 @@ private function applyLayoutUpdates(
300
324
}
301
325
}
302
326
303
- //Selected files
304
327
if ($ settings ->getPageLayoutHandles ()) {
305
328
$ page ->addPageLayoutHandles ($ settings ->getPageLayoutHandles ());
306
329
}
307
330
}
308
331
309
332
/**
310
- * Checks for toolbar actions
311
- *
312
333
* @return bool
313
334
*/
314
335
private function shouldRedirectOnToolbarAction (): bool
315
336
{
316
337
$ params = $ this ->getRequest ()->getParams ();
317
338
318
- return $ this ->toolbarMemorizer ->isMemorizingAllowed () && empty (array_intersect ([
339
+ return $ this ->toolbarMemorizer ->isMemorizingAllowed () && ! empty (array_intersect ([
319
340
Toolbar::ORDER_PARAM_NAME ,
320
341
Toolbar::DIRECTION_PARAM_NAME ,
321
342
Toolbar::MODE_PARAM_NAME ,
322
343
Toolbar::LIMIT_PARAM_NAME
323
- ], array_keys ($ params ))) === false ;
344
+ ], array_keys ($ params )));
324
345
}
325
346
}
0 commit comments