Skip to content
This repository was archived by the owner on Oct 3, 2024. It is now read-only.

Commit 4cf4968

Browse files
authored
Merge pull request #16 from outeredge/use-subcategories
use subcategories option added to menu items
2 parents bfe667d + 63a7da0 commit 4cf4968

File tree

9 files changed

+256
-21
lines changed

9 files changed

+256
-21
lines changed

Api/Data/ItemInterface.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@ interface ItemInterface
1111
/**#@+
1212
* Constants for keys of data array. Identical to the name of the getter in snake case
1313
*/
14-
const ITEM_ID = 'item_id';
15-
const MENU_ID = 'menu_id';
16-
const PARENT_ID = 'parent_id';
17-
const TITLE = 'title';
18-
const DESCRIPTION = 'description';
19-
const URL = 'url';
20-
const IMAGE = 'image';
21-
const PRODUCT_ID = 'product_id';
22-
const CATEGORY_ID = 'category_id';
23-
const PAGE_ID = 'page_id';
24-
const SORT_ORDER = 'sort_order';
14+
const ITEM_ID = 'item_id';
15+
const MENU_ID = 'menu_id';
16+
const PARENT_ID = 'parent_id';
17+
const TITLE = 'title';
18+
const DESCRIPTION = 'description';
19+
const URL = 'url';
20+
const IMAGE = 'image';
21+
const PRODUCT_ID = 'product_id';
22+
const CATEGORY_ID = 'category_id';
23+
const USE_SUBCATEGORIES = 'use_subcategories';
24+
const PAGE_ID = 'page_id';
25+
const SORT_ORDER = 'sort_order';
2526
/**#@-*/
2627

2728
/**
@@ -86,6 +87,13 @@ public function getProductId();
8687
* @return int|null
8788
*/
8889
public function getCategoryId();
90+
91+
/**
92+
* Get use subcategories
93+
*
94+
* @return int|null
95+
*/
96+
public function getUseSubcategories();
8997

9098
/**
9199
* Get page ID
@@ -172,6 +180,14 @@ public function setProductId($productId);
172180
* @return $this
173181
*/
174182
public function setCategoryId($categoryId);
183+
184+
/**
185+
* Set use subcategories
186+
*
187+
* @param int $useSubcategories
188+
* @return $this
189+
*/
190+
public function setUseSubcategories($useSubcategories);
175191

176192
/**
177193
* Set page ID

Helper/Menu.php

Lines changed: 124 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@
33
namespace OuterEdge\Menu\Helper;
44

55
use Magento\Framework\App\Helper\AbstractHelper;
6+
use OuterEdge\Menu\Model\Menu as MenuModel;
67
use OuterEdge\Menu\Model\MenuFactory as MenuFactory;
8+
use OuterEdge\Menu\Model\ResourceModel\Item\Collection as ItemCollection;
79
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;
912
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;
1017

1118
class Menu extends AbstractHelper
1219
{
@@ -19,28 +26,60 @@ class Menu extends AbstractHelper
1926
* @var ItemCollectionFactory
2027
*/
2128
private $itemCollectionFactory;
29+
30+
/**
31+
* @var ItemFactory
32+
*/
33+
private $itemFactory;
2234

2335
/**
2436
* @var Escaper
2537
*/
2638
private $escaper;
27-
39+
40+
/**
41+
* @var StoreManagerInterface
42+
*/
43+
private $storeManager;
44+
45+
/**
46+
* @var CategoryCollectionFactory
47+
*/
48+
private $categoryCollectionFactory;
49+
2850
/**
2951
* @param MenuFactory $menuFactory
3052
* @param ItemCollectionFactory $itemCollectionFactory
53+
* @param ItemFactory $itemFactory
3154
* @param Escaper $escaper
55+
* @param StoreManagerInterface $storeManager
56+
* @param CategoryCollectionFactory $categoryCollectionFactory
3257
* @codeCoverageIgnore
3358
*/
3459
public function __construct(
3560
MenuFactory $menuFactory,
3661
ItemCollectionFactory $itemCollectionFactory,
37-
Escaper $escaper
62+
ItemFactory $itemFactory,
63+
Escaper $escaper,
64+
StoreManagerInterface $storeManager,
65+
CategoryCollectionFactory $categoryCollectionFactory
3866
) {
3967
$this->menuFactory = $menuFactory;
4068
$this->itemCollectionFactory = $itemCollectionFactory;
69+
$this->itemFactory = $itemFactory;
4170
$this->escaper = $escaper;
71+
$this->storeManager = $storeManager;
72+
$this->categoryCollectionFactory = $categoryCollectionFactory;
4273
}
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+
*/
4483
public function getMenuHtml($menu, $includeWrapper = false, $loadByCode = false)
4584
{
4685
if ($menu instanceof MenuModel) {
@@ -59,7 +98,14 @@ public function getMenuHtml($menu, $includeWrapper = false, $loadByCode = false)
5998

6099
return $this->_getMenuHtml($menuModel, $includeWrapper);
61100
}
62-
101+
102+
/**
103+
* Get menu html protected
104+
*
105+
* @param MenuModel $menu
106+
* @param boolean $includeWrapper
107+
* @return string
108+
*/
63109
protected function _getMenuHtml($menu, $includeWrapper = false)
64110
{
65111
$items = $this->itemCollectionFactory->create()
@@ -74,13 +120,24 @@ protected function _getMenuHtml($menu, $includeWrapper = false)
74120
}
75121
return $menuHtml;
76122
}
77-
123+
124+
/**
125+
* Add sub menu html to menu
126+
*
127+
* @param ItemCollection $items
128+
* @param int $level
129+
* @return string
130+
*/
78131
protected function _addSubMenu($items, $level = 0)
79132
{
80133
$html = '';
81134

82135
foreach ($items as $item) {
83136
$children = $this->_getItemChildren($item);
137+
138+
if ($item->getCategoryId() && $item->getUseSubcategories()) {
139+
$this->_addSubcategoriesToChildren($children, $item->getCategoryId(), $level + 1);
140+
}
84141

85142
$item->setLevel($level);
86143
$item->setChildren($children->count());
@@ -111,7 +168,13 @@ protected function _addSubMenu($items, $level = 0)
111168

112169
return $html;
113170
}
114-
171+
172+
/**
173+
* Get item classes for menu item
174+
*
175+
* @param Item $item
176+
* @return string
177+
*/
115178
protected function _getItemClasses($item)
116179
{
117180
$classes = ['level' . $item->getLevel()];
@@ -126,11 +189,64 @@ protected function _getItemClasses($item)
126189
}
127190
return implode(' ', $classes);
128191
}
129-
192+
193+
/**
194+
* Get menu item children collection
195+
*
196+
* @param Item $item
197+
* @return ItemCollection
198+
*/
130199
protected function _getItemChildren($item)
131200
{
132201
return $this->itemCollectionFactory->create()
133202
->addFieldToFilter('parent_id', ['eq' => $item->getId()])
134203
->setOrder('sort_order', 'ASC');
135204
}
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+
}
136252
}

Model/Item.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ public function getCategoryId()
140140
{
141141
return parent::getData(self::CATEGORY_ID);
142142
}
143+
144+
/**
145+
* Get use subcategories
146+
*
147+
* @return int
148+
*/
149+
public function getUseSubcategories()
150+
{
151+
return parent::getData(self::USE_SUBCATEGORIES);
152+
}
143153

144154
/**
145155
* Get page ID
@@ -259,6 +269,17 @@ public function setCategoryId($categoryId)
259269
{
260270
return $this->setData(self::CATEGORY_ID, $categoryId);
261271
}
272+
273+
/**
274+
* Set use subcategories
275+
*
276+
* @param int $useSubcategories
277+
* @return ItemInterface
278+
*/
279+
public function setUseSubcategories($useSubcategories)
280+
{
281+
return $this->setData(self::USE_SUBCATEGORIES, $useSubcategories);
282+
}
262283

263284
/**
264285
* Set page ID

Setup/InstallSchema.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
class InstallSchema implements InstallSchemaInterface
1212
{
13+
/**
14+
* {@inheritdoc}
15+
*/
1316
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
1417
{
1518
$installer = $setup;

Setup/UpgradeSchema.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace OuterEdge\Menu\Setup;
4+
5+
use Magento\Framework\Setup\UpgradeSchemaInterface;
6+
use Magento\Framework\Setup\ModuleContextInterface;
7+
use Magento\Framework\Setup\SchemaSetupInterface;
8+
use Magento\Framework\DB\Ddl\Table;
9+
10+
class UpgradeSchema implements UpgradeSchemaInterface
11+
{
12+
/**
13+
* {@inheritdoc}
14+
*/
15+
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
16+
{
17+
$setup->startSetup();
18+
19+
if (version_compare($context->getVersion(), '1.0.1', '<')) {
20+
$this->addUseSubcategoriesColumnToItemTable($setup);
21+
}
22+
23+
$setup->endSetup();
24+
}
25+
26+
/**
27+
* Add the column 'use_subcategories' to the MenuItem table
28+
* It allows to use subcategory tree on a menu item
29+
*
30+
* @param SchemaSetupInterface $setup
31+
*
32+
* @return void
33+
*/
34+
private function addUseSubcategoriesColumnToItemTable(SchemaSetupInterface $setup)
35+
{
36+
$connection = $setup->getConnection();
37+
38+
$tableItem = $setup->getTable('menu_item');
39+
if ($connection->isTableExists($tableItem) == true) {
40+
$connection->addColumn(
41+
$tableItem,
42+
'use_subcategories',
43+
[
44+
'type' => Table::TYPE_SMALLINT,
45+
'nullable' => false,
46+
'default' => 0,
47+
'comment' => 'Use Subcategory Tree'
48+
]
49+
);
50+
}
51+
}
52+
}

etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0"?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
3-
<module name="OuterEdge_Menu" setup_version="1.0.0"/>
3+
<module name="OuterEdge_Menu" setup_version="1.0.1"/>
44
</config>

0 commit comments

Comments
 (0)