5
5
*/
6
6
namespace Magento \Theme \Block \Html ;
7
7
8
+ use Magento \Backend \Model \Menu ;
8
9
use Magento \Framework \Data \Tree \Node ;
10
+ use Magento \Framework \Data \Tree \Node \Collection ;
9
11
use Magento \Framework \Data \Tree \NodeFactory ;
10
12
use Magento \Framework \Data \TreeFactory ;
13
+ use Magento \Framework \DataObject ;
11
14
use Magento \Framework \DataObject \IdentityInterface ;
12
15
use Magento \Framework \View \Element \Template ;
13
16
@@ -29,7 +32,7 @@ class Topmenu extends Template implements IdentityInterface
29
32
/**
30
33
* Top menu data tree
31
34
*
32
- * @var \Magento\Framework\Data\Tree\ Node
35
+ * @var Node
33
36
*/
34
37
protected $ _menu ;
35
38
@@ -89,28 +92,35 @@ public function getHtml($outermostClass = '', $childrenWrapClass = '', $limit =
89
92
$ this ->getMenu ()->setOutermostClass ($ outermostClass );
90
93
$ this ->getMenu ()->setChildrenWrapClass ($ childrenWrapClass );
91
94
92
- $ html = $ this ->_getHtml ($ this ->getMenu (), $ childrenWrapClass , $ limit );
95
+ $ transportObject = new DataObject (
96
+ [
97
+ 'html ' => $ this ->_getHtml (
98
+ $ this ->getMenu (),
99
+ $ childrenWrapClass ,
100
+ $ limit
101
+ )
102
+ ]
103
+ );
93
104
94
- $ transportObject = new \Magento \Framework \DataObject (['html ' => $ html ]);
95
105
$ this ->_eventManager ->dispatch (
96
106
'page_block_html_topmenu_gethtml_after ' ,
97
107
['menu ' => $ this ->getMenu (), 'transportObject ' => $ transportObject ]
98
108
);
99
- $ html = $ transportObject -> getHtml ();
100
- return $ html ;
109
+
110
+ return $ transportObject -> getHtml () ;
101
111
}
102
112
103
113
/**
104
114
* Count All Subnavigation Items
105
115
*
106
- * @param \Magento\Backend\Model\ Menu $items
116
+ * @param Menu $items
107
117
* @return int
108
118
*/
109
119
protected function _countItems ($ items )
110
120
{
111
121
$ total = $ items ->count ();
112
122
foreach ($ items as $ item ) {
113
- /** @var $item \Magento\Backend\Model\ Menu\Item */
123
+ /** @var $item Menu\Item */
114
124
if ($ item ->hasChildren ()) {
115
125
$ total += $ this ->_countItems ($ item ->getChildren ());
116
126
}
@@ -121,7 +131,7 @@ protected function _countItems($items)
121
131
/**
122
132
* Building Array with Column Brake Stops
123
133
*
124
- * @param \Magento\Backend\Model\ Menu $items
134
+ * @param Menu $items
125
135
* @param int $limit
126
136
* @return array|void
127
137
*
@@ -164,7 +174,7 @@ protected function _columnBrake($items, $limit)
164
174
/**
165
175
* Add sub menu HTML code for current menu item
166
176
*
167
- * @param \Magento\Framework\Data\Tree\ Node $child
177
+ * @param Node $child
168
178
* @param string $childLevel
169
179
* @param string $childrenWrapClass
170
180
* @param int $limit
@@ -192,59 +202,46 @@ protected function _addSubMenu($child, $childLevel, $childrenWrapClass, $limit)
192
202
/**
193
203
* Recursively generates top menu html from data that is specified in $menuTree
194
204
*
195
- * @param \Magento\Framework\Data\Tree\ Node $menuTree
205
+ * @param Node $menuTree
196
206
* @param string $childrenWrapClass
197
207
* @param int $limit
198
208
* @param array $colBrakes
199
209
* @return string
200
- *
201
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
202
- * @SuppressWarnings(PHPMD.NPathComplexity)
203
210
*/
204
211
protected function _getHtml (
205
- \ Magento \ Framework \ Data \ Tree \ Node $ menuTree ,
212
+ Node $ menuTree ,
206
213
$ childrenWrapClass ,
207
214
$ limit ,
208
215
array $ colBrakes = []
209
216
) {
210
217
$ html = '' ;
211
218
212
219
$ children = $ menuTree ->getChildren ();
213
- $ parentLevel = $ menuTree ->getLevel ();
214
- $ childLevel = $ parentLevel === null ? 0 : $ parentLevel + 1 ;
220
+ $ childLevel = $ this -> getChildLevel ( $ menuTree ->getLevel () );
221
+ $ this -> removeChildrenWithoutActiveParent ( $ children , $ childLevel ) ;
215
222
216
223
$ counter = 1 ;
217
- $ itemPosition = 1 ;
218
224
$ childrenCount = $ children ->count ();
219
225
220
226
$ parentPositionClass = $ menuTree ->getPositionClass ();
221
227
$ itemPositionClassPrefix = $ parentPositionClass ? $ parentPositionClass . '- ' : 'nav- ' ;
222
228
223
- /** @var \Magento\Framework\Data\Tree\ Node $child */
229
+ /** @var Node $child */
224
230
foreach ($ children as $ child ) {
225
- if ($ childLevel === 0 && $ child ->getData ('is_parent_active ' ) === false ) {
226
- continue ;
227
- }
228
231
$ child ->setLevel ($ childLevel );
229
- $ child ->setIsFirst ($ counter == 1 );
230
- $ child ->setIsLast ($ counter == $ childrenCount );
232
+ $ child ->setIsFirst ($ counter === 1 );
233
+ $ child ->setIsLast ($ counter === $ childrenCount );
231
234
$ child ->setPositionClass ($ itemPositionClassPrefix . $ counter );
232
235
233
236
$ outermostClassCode = '' ;
234
237
$ outermostClass = $ menuTree ->getOutermostClass ();
235
238
236
- if ($ childLevel == 0 && $ outermostClass ) {
239
+ if ($ childLevel === 0 && $ outermostClass ) {
237
240
$ outermostClassCode = ' class=" ' . $ outermostClass . '" ' ;
238
- $ currentClass = $ child ->getClass ();
239
-
240
- if (empty ($ currentClass )) {
241
- $ child ->setClass ($ outermostClass );
242
- } else {
243
- $ child ->setClass ($ currentClass . ' ' . $ outermostClass );
244
- }
241
+ $ this ->setCurrentClass ($ child , $ outermostClass );
245
242
}
246
243
247
- if (is_array ( $ colBrakes ) && count ($ colBrakes) && $ colBrakes [ $ counter][ ' colbrake ' ] ) {
244
+ if ($ this -> shouldAddNewColumn ($ colBrakes, $ counter) ) {
248
245
$ html .= '</ul></li><li class="column"><ul> ' ;
249
246
}
250
247
@@ -257,11 +254,10 @@ protected function _getHtml(
257
254
$ childrenWrapClass ,
258
255
$ limit
259
256
) . '</li> ' ;
260
- $ itemPosition ++;
261
257
$ counter ++;
262
258
}
263
259
264
- if (is_array ($ colBrakes ) && count ($ colBrakes ) && $ limit ) {
260
+ if (is_array ($ colBrakes ) && ! empty ($ colBrakes ) && $ limit ) {
265
261
$ html = '<li class="column"><ul> ' . $ html . '</ul></li> ' ;
266
262
}
267
263
@@ -271,14 +267,13 @@ protected function _getHtml(
271
267
/**
272
268
* Generates string with all attributes that should be present in menu item element
273
269
*
274
- * @param \Magento\Framework\Data\Tree\ Node $item
270
+ * @param Node $item
275
271
* @return string
276
272
*/
277
- protected function _getRenderedMenuItemAttributes (\ Magento \ Framework \ Data \ Tree \ Node $ item )
273
+ protected function _getRenderedMenuItemAttributes (Node $ item )
278
274
{
279
275
$ html = '' ;
280
- $ attributes = $ this ->_getMenuItemAttributes ($ item );
281
- foreach ($ attributes as $ attributeName => $ attributeValue ) {
276
+ foreach ($ this ->_getMenuItemAttributes ($ item ) as $ attributeName => $ attributeValue ) {
282
277
$ html .= ' ' . $ attributeName . '=" ' . str_replace ('" ' , '\" ' , $ attributeValue ) . '" ' ;
283
278
}
284
279
return $ html ;
@@ -287,27 +282,26 @@ protected function _getRenderedMenuItemAttributes(\Magento\Framework\Data\Tree\N
287
282
/**
288
283
* Returns array of menu item's attributes
289
284
*
290
- * @param \Magento\Framework\Data\Tree\ Node $item
285
+ * @param Node $item
291
286
* @return array
292
287
*/
293
- protected function _getMenuItemAttributes (\ Magento \ Framework \ Data \ Tree \ Node $ item )
288
+ protected function _getMenuItemAttributes (Node $ item )
294
289
{
295
- $ menuItemClasses = $ this ->_getMenuItemClasses ($ item );
296
- return ['class ' => implode (' ' , $ menuItemClasses )];
290
+ return ['class ' => implode (' ' , $ this ->_getMenuItemClasses ($ item ))];
297
291
}
298
292
299
293
/**
300
294
* Returns array of menu item's classes
301
295
*
302
- * @param \Magento\Framework\Data\Tree\ Node $item
296
+ * @param Node $item
303
297
* @return array
304
298
*/
305
- protected function _getMenuItemClasses (\ Magento \ Framework \ Data \ Tree \ Node $ item )
299
+ protected function _getMenuItemClasses (Node $ item )
306
300
{
307
- $ classes = [];
308
-
309
- $ classes [] = ' level ' . $ item ->getLevel ();
310
- $ classes [] = $ item -> getPositionClass () ;
301
+ $ classes = [
302
+ ' level ' . $ item -> getLevel (),
303
+ $ item ->getPositionClass (),
304
+ ] ;
311
305
312
306
if ($ item ->getIsCategory ()) {
313
307
$ classes [] = 'category-item ' ;
@@ -375,7 +369,7 @@ protected function getCacheTags()
375
369
/**
376
370
* Get menu object.
377
371
*
378
- * Creates \Magento\Framework\Data\ Tree\Node root node object.
372
+ * Creates Tree root node object.
379
373
* The creation logic was moved from class constructor into separate method.
380
374
*
381
375
* @return Node
@@ -394,4 +388,61 @@ public function getMenu()
394
388
}
395
389
return $ this ->_menu ;
396
390
}
391
+
392
+ /**
393
+ * Remove children from collection when the parent is not active
394
+ *
395
+ * @param Collection $children
396
+ * @param int $childLevel
397
+ * @return void
398
+ */
399
+ private function removeChildrenWithoutActiveParent (Collection $ children , int $ childLevel ): void
400
+ {
401
+ /** @var Node $child */
402
+ foreach ($ children as $ child ) {
403
+ if ($ childLevel === 0 && $ child ->getData ('is_parent_active ' ) === false ) {
404
+ $ children ->delete ($ child );
405
+ }
406
+ }
407
+ }
408
+
409
+ /**
410
+ * Retrieve child level based on parent level
411
+ *
412
+ * @param int $parentLevel
413
+ *
414
+ * @return int
415
+ */
416
+ private function getChildLevel ($ parentLevel ): int
417
+ {
418
+ return $ parentLevel === null ? 0 : $ parentLevel + 1 ;
419
+ }
420
+
421
+ /**
422
+ * Check if new column should be added.
423
+ *
424
+ * @param array $colBrakes
425
+ * @param int $counter
426
+ * @return bool
427
+ */
428
+ private function shouldAddNewColumn (array $ colBrakes , int $ counter ): bool
429
+ {
430
+ return count ($ colBrakes ) && $ colBrakes [$ counter ]['colbrake ' ];
431
+ }
432
+
433
+ /**
434
+ * Set current class.
435
+ *
436
+ * @param Node $child
437
+ * @param string $outermostClass
438
+ */
439
+ private function setCurrentClass (Node $ child , string $ outermostClass ): void
440
+ {
441
+ $ currentClass = $ child ->getClass ();
442
+ if (empty ($ currentClass )) {
443
+ $ child ->setClass ($ outermostClass );
444
+ } else {
445
+ $ child ->setClass ($ currentClass . ' ' . $ outermostClass );
446
+ }
447
+ }
397
448
}
0 commit comments