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,29 @@ 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
+ 'html ' => $ this ->_getHtml ($ this ->getMenu (), $ childrenWrapClass , $ limit )
97
+ ]);
93
98
94
- $ transportObject = new \Magento \Framework \DataObject (['html ' => $ html ]);
95
99
$ this ->_eventManager ->dispatch (
96
100
'page_block_html_topmenu_gethtml_after ' ,
97
101
['menu ' => $ this ->getMenu (), 'transportObject ' => $ transportObject ]
98
102
);
99
- $ html = $ transportObject -> getHtml ();
100
- return $ html ;
103
+
104
+ return $ transportObject -> getHtml () ;
101
105
}
102
106
103
107
/**
104
108
* Count All Subnavigation Items
105
109
*
106
- * @param \Magento\Backend\Model\ Menu $items
110
+ * @param Menu $items
107
111
* @return int
108
112
*/
109
113
protected function _countItems ($ items )
110
114
{
111
115
$ total = $ items ->count ();
112
116
foreach ($ items as $ item ) {
113
- /** @var $item \Magento\Backend\Model\ Menu\Item */
117
+ /** @var $item Menu\Item */
114
118
if ($ item ->hasChildren ()) {
115
119
$ total += $ this ->_countItems ($ item ->getChildren ());
116
120
}
@@ -121,7 +125,7 @@ protected function _countItems($items)
121
125
/**
122
126
* Building Array with Column Brake Stops
123
127
*
124
- * @param \Magento\Backend\Model\ Menu $items
128
+ * @param Menu $items
125
129
* @param int $limit
126
130
* @return array|void
127
131
*
@@ -164,7 +168,7 @@ protected function _columnBrake($items, $limit)
164
168
/**
165
169
* Add sub menu HTML code for current menu item
166
170
*
167
- * @param \Magento\Framework\Data\Tree\ Node $child
171
+ * @param Node $child
168
172
* @param string $childLevel
169
173
* @param string $childrenWrapClass
170
174
* @param int $limit
@@ -192,48 +196,41 @@ protected function _addSubMenu($child, $childLevel, $childrenWrapClass, $limit)
192
196
/**
193
197
* Recursively generates top menu html from data that is specified in $menuTree
194
198
*
195
- * @param \Magento\Framework\Data\Tree\ Node $menuTree
199
+ * @param Node $menuTree
196
200
* @param string $childrenWrapClass
197
201
* @param int $limit
198
202
* @param array $colBrakes
199
203
* @return string
200
- *
201
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
202
- * @SuppressWarnings(PHPMD.NPathComplexity)
203
204
*/
204
205
protected function _getHtml (
205
- \ Magento \ Framework \ Data \ Tree \ Node $ menuTree ,
206
+ Node $ menuTree ,
206
207
$ childrenWrapClass ,
207
208
$ limit ,
208
209
array $ colBrakes = []
209
210
) {
210
211
$ html = '' ;
211
212
212
213
$ children = $ menuTree ->getChildren ();
213
- $ parentLevel = $ menuTree -> getLevel ( );
214
- $ childLevel = $ parentLevel === null ? 0 : $ parentLevel + 1 ;
214
+ $ this -> removeChildrenWithoutActiveParent ( $ children );
215
+ $ childLevel = $ this -> getChildLevel ( $ menuTree -> getLevel ()) ;
215
216
216
217
$ counter = 1 ;
217
- $ itemPosition = 1 ;
218
218
$ childrenCount = $ children ->count ();
219
219
220
220
$ parentPositionClass = $ menuTree ->getPositionClass ();
221
221
$ itemPositionClassPrefix = $ parentPositionClass ? $ parentPositionClass . '- ' : 'nav- ' ;
222
222
223
- /** @var \Magento\Framework\Data\Tree\ Node $child */
223
+ /** @var Node $child */
224
224
foreach ($ children as $ child ) {
225
- if ($ childLevel === 0 && $ child ->getData ('is_parent_active ' ) === false ) {
226
- continue ;
227
- }
228
225
$ child ->setLevel ($ childLevel );
229
- $ child ->setIsFirst ($ counter == 1 );
230
- $ child ->setIsLast ($ counter == $ childrenCount );
226
+ $ child ->setIsFirst ($ counter === 1 );
227
+ $ child ->setIsLast ($ counter === $ childrenCount );
231
228
$ child ->setPositionClass ($ itemPositionClassPrefix . $ counter );
232
229
233
230
$ outermostClassCode = '' ;
234
231
$ outermostClass = $ menuTree ->getOutermostClass ();
235
232
236
- if ($ childLevel == 0 && $ outermostClass ) {
233
+ if ($ childLevel === 0 && $ outermostClass ) {
237
234
$ outermostClassCode = ' class=" ' . $ outermostClass . '" ' ;
238
235
$ currentClass = $ child ->getClass ();
239
236
@@ -244,7 +241,7 @@ protected function _getHtml(
244
241
}
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,7 +254,6 @@ protected function _getHtml(
257
254
$ childrenWrapClass ,
258
255
$ limit
259
256
) . '</li> ' ;
260
- $ itemPosition ++;
261
257
$ counter ++;
262
258
}
263
259
@@ -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,44 @@ 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
+ *
397
+ * @return void
398
+ */
399
+ private function removeChildrenWithoutActiveParent (Collection $ children )
400
+ {
401
+ /** @var Node $child */
402
+ foreach ($ children as $ child ) {
403
+ if ($ 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 )
417
+ {
418
+ return $ parentLevel === null ? 0 : $ parentLevel + 1 ;
419
+ }
420
+
421
+ /**
422
+ * @param array $colBrakes
423
+ * @param $counter
424
+ *
425
+ * @return bool
426
+ */
427
+ private function shouldAddNewColumn (array $ colBrakes , int $ counter )
428
+ {
429
+ return count ($ colBrakes ) && $ colBrakes [$ counter ]['colbrake ' ];
430
+ }
397
431
}
0 commit comments