6
6
*/
7
7
namespace Magento \Catalog \Controller \Category ;
8
8
9
- use Magento \Framework \App \Action \HttpPostActionInterface ;
10
- use Magento \Framework \App \Action \HttpGetActionInterface ;
11
9
use Magento \Catalog \Api \CategoryRepositoryInterface ;
10
+ use Magento \Catalog \Model \Category ;
11
+ use Magento \Catalog \Model \Design ;
12
12
use Magento \Catalog \Model \Layer \Resolver ;
13
13
use Magento \Catalog \Model \Product \ProductList \ToolbarMemorizer ;
14
+ use Magento \Catalog \Model \Session ;
15
+ use Magento \CatalogUrlRewrite \Model \CategoryUrlPathGenerator ;
16
+ use Magento \Framework \App \Action \Action ;
17
+ use Magento \Framework \App \Action \Context ;
18
+ use Magento \Framework \App \Action \HttpGetActionInterface ;
19
+ use Magento \Framework \App \Action \HttpPostActionInterface ;
20
+ use Magento \Framework \App \ActionInterface ;
21
+ use Magento \Framework \Controller \Result \ForwardFactory ;
22
+ use Magento \Framework \Controller \ResultInterface ;
23
+ use Magento \Framework \DataObject ;
24
+ use Magento \Framework \Exception \LocalizedException ;
14
25
use Magento \Framework \Exception \NoSuchEntityException ;
26
+ use Magento \Framework \Registry ;
27
+ use Magento \Framework \View \Result \Page ;
15
28
use Magento \Framework \View \Result \PageFactory ;
16
- use Magento \Framework \App \Action \Action ;
29
+ use Magento \Store \Model \StoreManagerInterface ;
30
+ use Psr \Log \LoggerInterface ;
17
31
18
32
/**
19
33
* View a category on storefront. Needs to be accessible by POST because of the store switching.
@@ -25,41 +39,41 @@ class View extends Action implements HttpGetActionInterface, HttpPostActionInter
25
39
/**
26
40
* Core registry
27
41
*
28
- * @var \Magento\Framework\ Registry
42
+ * @var Registry
29
43
*/
30
44
protected $ _coreRegistry = null ;
31
45
32
46
/**
33
47
* Catalog session
34
48
*
35
- * @var \Magento\Catalog\Model\ Session
49
+ * @var Session
36
50
*/
37
51
protected $ _catalogSession ;
38
52
39
53
/**
40
54
* Catalog design
41
55
*
42
- * @var \Magento\Catalog\Model\ Design
56
+ * @var Design
43
57
*/
44
58
protected $ _catalogDesign ;
45
59
46
60
/**
47
- * @var \Magento\Store\Model\ StoreManagerInterface
61
+ * @var StoreManagerInterface
48
62
*/
49
63
protected $ _storeManager ;
50
64
51
65
/**
52
- * @var \Magento\CatalogUrlRewrite\Model\ CategoryUrlPathGenerator
66
+ * @var CategoryUrlPathGenerator
53
67
*/
54
68
protected $ categoryUrlPathGenerator ;
55
69
56
70
/**
57
- * @var \Magento\Framework\View\Result\ PageFactory
71
+ * @var PageFactory
58
72
*/
59
73
protected $ resultPageFactory ;
60
74
61
75
/**
62
- * @var \Magento\Framework\Controller\Result\ ForwardFactory
76
+ * @var ForwardFactory
63
77
*/
64
78
protected $ resultForwardFactory ;
65
79
@@ -83,28 +97,28 @@ class View extends Action implements HttpGetActionInterface, HttpPostActionInter
83
97
/**
84
98
* Constructor
85
99
*
86
- * @param \Magento\Framework\App\Action\ Context $context
87
- * @param \Magento\Catalog\Model\ Design $catalogDesign
88
- * @param \Magento\Catalog\Model\ Session $catalogSession
89
- * @param \Magento\Framework\ Registry $coreRegistry
90
- * @param \Magento\Store\Model\ StoreManagerInterface $storeManager
91
- * @param \Magento\CatalogUrlRewrite\Model\ CategoryUrlPathGenerator $categoryUrlPathGenerator
92
- * @param \Magento\Framework\View\Result\ PageFactory $resultPageFactory
93
- * @param \Magento\Framework\Controller\Result\ ForwardFactory $resultForwardFactory
100
+ * @param Context $context
101
+ * @param Design $catalogDesign
102
+ * @param Session $catalogSession
103
+ * @param Registry $coreRegistry
104
+ * @param StoreManagerInterface $storeManager
105
+ * @param CategoryUrlPathGenerator $categoryUrlPathGenerator
106
+ * @param PageFactory $resultPageFactory
107
+ * @param ForwardFactory $resultForwardFactory
94
108
* @param Resolver $layerResolver
95
109
* @param CategoryRepositoryInterface $categoryRepository
96
110
* @param ToolbarMemorizer|null $toolbarMemorizer
97
111
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
98
112
*/
99
113
public function __construct (
100
- \ Magento \ Framework \ App \ Action \ Context $ context ,
101
- \ Magento \ Catalog \ Model \ Design $ catalogDesign ,
102
- \ Magento \ Catalog \ Model \ Session $ catalogSession ,
103
- \ Magento \ Framework \ Registry $ coreRegistry ,
104
- \ Magento \ Store \ Model \ StoreManagerInterface $ storeManager ,
105
- \ Magento \ CatalogUrlRewrite \ Model \ CategoryUrlPathGenerator $ categoryUrlPathGenerator ,
114
+ Context $ context ,
115
+ Design $ catalogDesign ,
116
+ Session $ catalogSession ,
117
+ Registry $ coreRegistry ,
118
+ StoreManagerInterface $ storeManager ,
119
+ CategoryUrlPathGenerator $ categoryUrlPathGenerator ,
106
120
PageFactory $ resultPageFactory ,
107
- \ Magento \ Framework \ Controller \ Result \ ForwardFactory $ resultForwardFactory ,
121
+ ForwardFactory $ resultForwardFactory ,
108
122
Resolver $ layerResolver ,
109
123
CategoryRepositoryInterface $ categoryRepository ,
110
124
ToolbarMemorizer $ toolbarMemorizer = null
@@ -125,7 +139,7 @@ public function __construct(
125
139
/**
126
140
* Initialize requested category object
127
141
*
128
- * @return \Magento\Catalog\Model\ Category|bool
142
+ * @return Category|bool
129
143
*/
130
144
protected function _initCategory ()
131
145
{
@@ -150,8 +164,8 @@ protected function _initCategory()
150
164
'catalog_controller_category_init_after ' ,
151
165
['category ' => $ category , 'controller_action ' => $ this ]
152
166
);
153
- } catch (\ Magento \ Framework \ Exception \ LocalizedException $ e ) {
154
- $ this ->_objectManager ->get (\ Psr \ Log \ LoggerInterface::class)->critical ($ e );
167
+ } catch (LocalizedException $ e ) {
168
+ $ this ->_objectManager ->get (LoggerInterface::class)->critical ($ e );
155
169
return false ;
156
170
}
157
171
@@ -161,13 +175,12 @@ protected function _initCategory()
161
175
/**
162
176
* Category view action
163
177
*
164
- * @return \Magento\Framework\Controller\ResultInterface
165
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
166
- * @SuppressWarnings(PHPMD.NPathComplexity)
178
+ * @return ResultInterface
179
+ * @throws NoSuchEntityException
167
180
*/
168
181
public function execute ()
169
182
{
170
- if ($ this ->_request ->getParam (\ Magento \ Framework \ App \ ActionInterface::PARAM_NAME_URL_ENCODED )) {
183
+ if ($ this ->_request ->getParam (ActionInterface::PARAM_NAME_URL_ENCODED )) {
171
184
return $ this ->resultRedirectFactory ->create ()->setUrl ($ this ->_redirect ->getRedirectUrl ());
172
185
}
173
186
$ category = $ this ->_initCategory ();
@@ -188,29 +201,18 @@ public function execute()
188
201
$ page ->getConfig ()->setPageLayout ($ settings ->getPageLayout ());
189
202
}
190
203
191
- $ hasChildren = $ category ->hasChildren ();
192
- if ($ category ->getIsAnchor ()) {
193
- $ type = $ hasChildren ? 'layered ' : 'layered_without_children ' ;
194
- } else {
195
- $ type = $ hasChildren ? 'default ' : 'default_without_children ' ;
196
- }
204
+ $ pageType = $ this ->getPageType ($ category );
197
205
198
- if (!$ hasChildren ) {
206
+ if (!$ category -> hasChildren () ) {
199
207
// Two levels removed from parent. Need to add default page type.
200
- $ parentType = strtok ($ type , '_ ' );
201
- $ page ->addPageLayoutHandles (['type ' => $ parentType ], null , false );
208
+ $ parentPageType = strtok ($ pageType , '_ ' );
209
+ $ page ->addPageLayoutHandles (['type ' => $ parentPageType ], null , false );
202
210
}
203
- $ page ->addPageLayoutHandles (['type ' => $ type ], null , false );
211
+ $ page ->addPageLayoutHandles (['type ' => $ pageType ], null , false );
204
212
$ page ->addPageLayoutHandles (['id ' => $ category ->getId ()]);
205
213
206
214
// apply custom layout update once layout is loaded
207
- $ layoutUpdates = $ settings ->getLayoutUpdates ();
208
- if ($ layoutUpdates && is_array ($ layoutUpdates )) {
209
- foreach ($ layoutUpdates as $ layoutUpdate ) {
210
- $ page ->addUpdate ($ layoutUpdate );
211
- $ page ->addPageLayoutHandles (['layout_update ' => sha1 ($ layoutUpdate )], null , false );
212
- }
213
- }
215
+ $ this ->applyLayoutUpdates ($ page , $ settings );
214
216
215
217
$ page ->getConfig ()->addBodyClass ('page-products ' )
216
218
->addBodyClass ('categorypath- ' . $ this ->categoryUrlPathGenerator ->getUrlPath ($ category ))
@@ -221,4 +223,40 @@ public function execute()
221
223
return $ this ->resultForwardFactory ->create ()->forward ('noroute ' );
222
224
}
223
225
}
226
+
227
+ /**
228
+ * Get page type based on category
229
+ *
230
+ * @param Category $category
231
+ * @return string
232
+ */
233
+ private function getPageType (Category $ category ) : string
234
+ {
235
+ $ hasChildren = $ category ->hasChildren ();
236
+ if ($ category ->getIsAnchor ()) {
237
+ return $ hasChildren ? 'layered ' : 'layered_without_children ' ;
238
+ }
239
+
240
+ return $ hasChildren ? 'default ' : 'default_without_children ' ;
241
+ }
242
+
243
+ /**
244
+ * Apply custom layout updates
245
+ *
246
+ * @param Page $page
247
+ * @param DataObject $settings
248
+ * @return void
249
+ */
250
+ private function applyLayoutUpdates (
251
+ Page $ page ,
252
+ DataObject $ settings
253
+ ) {
254
+ $ layoutUpdates = $ settings ->getLayoutUpdates ();
255
+ if ($ layoutUpdates && is_array ($ layoutUpdates )) {
256
+ foreach ($ layoutUpdates as $ layoutUpdate ) {
257
+ $ page ->addUpdate ($ layoutUpdate );
258
+ $ page ->addPageLayoutHandles (['layout_update ' => sha1 ($ layoutUpdate )], null , false );
259
+ }
260
+ }
261
+ }
224
262
}
0 commit comments