3
3
namespace OuterEdge \Menu \Helper ;
4
4
5
5
use Magento \Framework \App \Helper \AbstractHelper ;
6
+ use OuterEdge \Menu \Model \Menu as MenuModel ;
6
7
use OuterEdge \Menu \Model \MenuFactory as MenuFactory ;
8
+ use OuterEdge \Menu \Model \ResourceModel \Item \Collection as ItemCollection ;
7
9
use OuterEdge \Menu \Model \ResourceModel \Item \CollectionFactory as ItemCollectionFactory ;
8
- use OuterEdge \Menu \Model \Menu as MenuModel ;
10
+ use OuterEdge \Menu \Model \Item ;
11
+ use OuterEdge \Menu \Model \ItemFactory ;
9
12
use Magento \Framework \Escaper ;
13
+ use Magento \Store \Model \StoreManagerInterface ;
14
+ use Magento \Catalog \Model \ResourceModel \Category \Collection as CategoryCollection ;
15
+ use Magento \Catalog \Model \ResourceModel \Category \CollectionFactory as CategoryCollectionFactory ;
16
+ use Magento \Framework \Data \Collection ;
10
17
11
18
class Menu extends AbstractHelper
12
19
{
@@ -19,28 +26,60 @@ class Menu extends AbstractHelper
19
26
* @var ItemCollectionFactory
20
27
*/
21
28
private $ itemCollectionFactory ;
29
+
30
+ /**
31
+ * @var ItemFactory
32
+ */
33
+ private $ itemFactory ;
22
34
23
35
/**
24
36
* @var Escaper
25
37
*/
26
38
private $ escaper ;
27
-
39
+
40
+ /**
41
+ * @var StoreManagerInterface
42
+ */
43
+ private $ storeManager ;
44
+
45
+ /**
46
+ * @var CategoryCollectionFactory
47
+ */
48
+ private $ categoryCollectionFactory ;
49
+
28
50
/**
29
51
* @param MenuFactory $menuFactory
30
52
* @param ItemCollectionFactory $itemCollectionFactory
53
+ * @param ItemFactory $itemFactory
31
54
* @param Escaper $escaper
55
+ * @param StoreManagerInterface $storeManager
56
+ * @param CategoryCollectionFactory $categoryCollectionFactory
32
57
* @codeCoverageIgnore
33
58
*/
34
59
public function __construct (
35
60
MenuFactory $ menuFactory ,
36
61
ItemCollectionFactory $ itemCollectionFactory ,
37
- Escaper $ escaper
62
+ ItemFactory $ itemFactory ,
63
+ Escaper $ escaper ,
64
+ StoreManagerInterface $ storeManager ,
65
+ CategoryCollectionFactory $ categoryCollectionFactory
38
66
) {
39
67
$ this ->menuFactory = $ menuFactory ;
40
68
$ this ->itemCollectionFactory = $ itemCollectionFactory ;
69
+ $ this ->itemFactory = $ itemFactory ;
41
70
$ this ->escaper = $ escaper ;
71
+ $ this ->storeManager = $ storeManager ;
72
+ $ this ->categoryCollectionFactory = $ categoryCollectionFactory ;
42
73
}
43
-
74
+
75
+ /**
76
+ * Get menu html public wrapper
77
+ *
78
+ * @param MenuModel|int|string $menu
79
+ * @param boolean $includeWrapper
80
+ * @param boolean $loadByCode
81
+ * @return string
82
+ */
44
83
public function getMenuHtml ($ menu , $ includeWrapper = false , $ loadByCode = false )
45
84
{
46
85
if ($ menu instanceof MenuModel) {
@@ -59,7 +98,14 @@ public function getMenuHtml($menu, $includeWrapper = false, $loadByCode = false)
59
98
60
99
return $ this ->_getMenuHtml ($ menuModel , $ includeWrapper );
61
100
}
62
-
101
+
102
+ /**
103
+ * Get menu html protected
104
+ *
105
+ * @param MenuModel $menu
106
+ * @param boolean $includeWrapper
107
+ * @return string
108
+ */
63
109
protected function _getMenuHtml ($ menu , $ includeWrapper = false )
64
110
{
65
111
$ items = $ this ->itemCollectionFactory ->create ()
@@ -74,13 +120,24 @@ protected function _getMenuHtml($menu, $includeWrapper = false)
74
120
}
75
121
return $ menuHtml ;
76
122
}
77
-
123
+
124
+ /**
125
+ * Add sub menu html to menu
126
+ *
127
+ * @param ItemCollection $items
128
+ * @param int $level
129
+ * @return string
130
+ */
78
131
protected function _addSubMenu ($ items , $ level = 0 )
79
132
{
80
133
$ html = '' ;
81
134
82
135
foreach ($ items as $ item ) {
83
136
$ children = $ this ->_getItemChildren ($ item );
137
+
138
+ if ($ item ->getCategoryId () && $ item ->getUseSubcategories ()) {
139
+ $ this ->_addSubcategoriesToChildren ($ children , $ item ->getCategoryId (), $ level + 1 );
140
+ }
84
141
85
142
$ item ->setLevel ($ level );
86
143
$ item ->setChildren ($ children ->count ());
@@ -111,7 +168,13 @@ protected function _addSubMenu($items, $level = 0)
111
168
112
169
return $ html ;
113
170
}
114
-
171
+
172
+ /**
173
+ * Get item classes for menu item
174
+ *
175
+ * @param Item $item
176
+ * @return string
177
+ */
115
178
protected function _getItemClasses ($ item )
116
179
{
117
180
$ classes = ['level ' . $ item ->getLevel ()];
@@ -126,11 +189,64 @@ protected function _getItemClasses($item)
126
189
}
127
190
return implode (' ' , $ classes );
128
191
}
129
-
192
+
193
+ /**
194
+ * Get menu item children collection
195
+ *
196
+ * @param Item $item
197
+ * @return ItemCollection
198
+ */
130
199
protected function _getItemChildren ($ item )
131
200
{
132
201
return $ this ->itemCollectionFactory ->create ()
133
202
->addFieldToFilter ('parent_id ' , ['eq ' => $ item ->getId ()])
134
203
->setOrder ('sort_order ' , 'ASC ' );
135
204
}
205
+
206
+ /**
207
+ * Add default magento categories to children collection
208
+ *
209
+ * @param ItemCollection $children
210
+ * @param int $categoryId
211
+ * @param int $level
212
+ * @return void
213
+ */
214
+ protected function _addSubcategoriesToChildren (&$ children , $ categoryId , $ level )
215
+ {
216
+ $ storeId = $ this ->storeManager ->getStore ()->getId ();
217
+
218
+ $ subCategories = $ this ->getSubCategories ($ storeId , $ categoryId );
219
+ foreach ($ subCategories as $ subCategory ) {
220
+ $ item = $ this ->itemFactory ->create ();
221
+ $ item ->setData ([
222
+ 'title ' => $ subCategory ->getName (),
223
+ 'category_id ' => $ subCategory ->getId (),
224
+ 'use_subcategories ' => true
225
+ ]);
226
+ $ children ->addItem ($ item );
227
+ }
228
+ }
229
+
230
+ /**
231
+ * Load magento category collection from parent category
232
+ *
233
+ * @param int $storeId
234
+ * @param int $categoryId
235
+ * @return CategoryCollection
236
+ */
237
+ protected function getSubCategories ($ storeId , $ categoryId )
238
+ {
239
+ $ collection = $ this ->categoryCollectionFactory ->create ();
240
+ $ collection ->setStoreId ($ storeId );
241
+ $ collection ->addAttributeToSelect ('name ' );
242
+ $ collection ->addFieldToFilter ('parent_id ' , $ categoryId );
243
+ $ collection ->addAttributeToFilter ('include_in_menu ' , 1 );
244
+ $ collection ->addIsActiveFilter ();
245
+ $ collection ->addUrlRewriteToResult ();
246
+ $ collection ->addOrder ('level ' , Collection::SORT_ORDER_ASC );
247
+ $ collection ->addOrder ('position ' , Collection::SORT_ORDER_ASC );
248
+ $ collection ->addOrder ('parent_id ' , Collection::SORT_ORDER_ASC );
249
+ $ collection ->addOrder ('entity_id ' , Collection::SORT_ORDER_ASC );
250
+ return $ collection ;
251
+ }
136
252
}
0 commit comments