Skip to content

Commit 2c2d3fd

Browse files
Add upgrade script. Layout update. Readme update
1 parent 67a1dd9 commit 2c2d3fd

File tree

5 files changed

+166
-8
lines changed

5 files changed

+166
-8
lines changed

README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ composer require scandiweb/module-core:"dev-master as 0.1.0"
1414

1515
```
1616
composer config repositories.module-menumanager git https://github.com/scandiwebcom/Menu-Manager-for-Magento-2-by-Scandiweb.git
17-
composer require scandiweb/module-menumanager:0.1.7
17+
composer require scandiweb/module-menumanager:0.1.8
1818
php -f bin/magento setup:upgrade
1919
```
2020

@@ -24,22 +24,29 @@ For configuration and more details you can visit [wiki](https://github.com/scand
2424

2525
## Example on how-to add menu to the Magento store
2626

27-
You will need to update your theme’s default.xml in order to replace the main navigation with the newly created menu. In default.xml please add the following (replace “identifier” parameter with your Menu Identifier):
27+
By default layout update is done in the module's view/frontend/layout/default.xml.
28+
29+
If you want to customize it in the theme you are using:
30+
31+
- you will need to update your theme’s default.xml in order to replace the main navigation with the newly created menu;
32+
- in default.xml please add the following (replace “identifier” parameter with your Menu Identifier):
2833

2934
So for example, if your Menu Identifier is 'cool-menu':
3035

3136
```
3237
<?xml version="1.0"?>
3338
<page xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
3439
<body>
35-
<move element="custom.topnav" destination="store.menu" after=""/>
36-
<referenceContainer name="page.top">
37-
<block class="Scandiweb\Menumanager\Block\Menu" name="custom.navigation" template="html/menu.phtml" ttl="3600">
40+
<referenceBlock name="catalog.topnav" remove="true"/>
41+
<referenceBlock name="store.menu">
42+
<block class="Scandiweb\Menumanager\Block\Menu" name="custom.navigation" template="html/menu.phtml" before="-" ttl="3600">
3843
<arguments>
3944
<argument name="identifier" xsi:type="string">cool-menu</argument>
4045
</arguments>
4146
</block>
42-
</referenceContainer>
47+
</referenceBlock>
4348
</body>
4449
</page>
4550
```
51+
52+
- if default.xml in the theme is using - please remove dafult.xml inside the module.

Setup/UpgradeSchema.php

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
namespace Scandiweb\Menumanager\Setup;
4+
5+
use Magento\Framework\App\ObjectManager;
6+
use Magento\Framework\Setup\UpgradeSchemaInterface;
7+
use Magento\Framework\Setup\SchemaSetupInterface;
8+
use Magento\Framework\Setup\ModuleContextInterface;
9+
10+
/**
11+
* @category Scandiweb
12+
* @package Scandiweb\Menumanager\Setup
13+
* @author Dmitrijs Sitovs <info@scandiweb.com / dmitrijssh@scandiweb.com / dsitovs@gmail.com>
14+
* @copyright Copyright (c) 2015 Scandiweb, Ltd (http://scandiweb.com)
15+
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
16+
*
17+
* Class InstallSchema
18+
*/
19+
class UpgradeSchema implements UpgradeSchemaInterface
20+
{
21+
22+
/**
23+
* @var \Scandiweb\Menumanager\Model\Menu
24+
*/
25+
protected $menu;
26+
27+
/**
28+
* @var \Scandiweb\Menumanager\Model\Item
29+
*/
30+
protected $item;
31+
32+
/**
33+
* @var \Scandiweb\MenuManager\Model\MenuFactory
34+
*/
35+
protected $menuFactory;
36+
37+
/**
38+
* UpgradeSchema constructor.
39+
* @param \Scandiweb\Menumanager\Model\Menu $menu
40+
* @param \Scandiweb\Menumanager\Model\Item $item
41+
* @param \Scandiweb\MenuManager\Model\MenuFactory $menuFactory
42+
*/
43+
public function __construct(
44+
\Scandiweb\Menumanager\Model\Menu $menu,
45+
\Scandiweb\Menumanager\Model\Item $item,
46+
\Scandiweb\MenuManager\Model\MenuFactory $menuFactory,
47+
\Scandiweb\Menumanager\Model\ItemFactory $itemFactory
48+
) {
49+
$this->menu = $menu;
50+
$this->item = $item;
51+
$this->menuFactory = $menuFactory;
52+
$this->itemFactory = $itemFactory;
53+
}
54+
55+
/**
56+
* @param SchemaSetupInterface $setup
57+
* @param ModuleContextInterface $context
58+
*/
59+
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
60+
{
61+
$setup->startSetup();
62+
63+
64+
if(!$context->getVersion()) {
65+
}
66+
67+
if (version_compare($context->getVersion(), '0.1.8') < 0) {
68+
69+
$identifier = 'cool-menu';
70+
$this->createMenu('Cool menu', $identifier);
71+
$collection = $this->menuFactory->create()->getCollection()
72+
->addFieldToFilter('identifier', $identifier);
73+
foreach($collection as $menu) {
74+
$id = $menu->getId();
75+
}
76+
$this->createMenuItem('menu-item-1', 'Menu item 1', $id);
77+
$this->createMenuItem('menu-item-2', 'Menu item 2', $id);
78+
$this->createMenuItem('scandiweb', 'Scandiweb', $id);
79+
$this->activateMenu($id);
80+
}
81+
82+
$setup->endSetup();
83+
}
84+
85+
/**
86+
* @param $menuTitle
87+
* @param $identifier
88+
*/
89+
public function createMenu($menuTitle, $identifier) {
90+
$this->menu ->setIdentifier($identifier)
91+
->setTitle($menuTitle)
92+
->setIsActive('1')
93+
->save();
94+
}
95+
96+
/**
97+
* @param $identifier
98+
* @param $title
99+
* @param $menuId
100+
*/
101+
public function createMenuItem($identifier, $title, $menuId) {
102+
$item = $this->itemFactory->create();
103+
$item
104+
->setIdentifier($identifier)
105+
->setMenuId($menuId)
106+
->setTitle($title)
107+
->setOpenType(0)
108+
->setUrlType(0)
109+
->setIsActive(1)
110+
->setParentId(0);
111+
switch ($identifier) {
112+
case 'menu-item-1':
113+
$item ->setUrl('#')
114+
->setPosition(0);
115+
break;
116+
case 'menu-item-2':
117+
$item ->setUrl('#')
118+
->setPosition(1);
119+
break;
120+
case 'scandiweb':
121+
$item ->setUrl('https://scandiweb.com/')
122+
->setPosition(2);
123+
break;
124+
}
125+
$item->save();
126+
}
127+
128+
/**
129+
* @param $id
130+
*/
131+
public function activateMenu($id) {
132+
$tableName = 'scandiweb_menumanager_menu_store';
133+
$mainTable = 'scandiweb_menumanager_menu';
134+
$connection = ObjectManager::getInstance()->get('Magento\Framework\App\ResourceConnection')->getConnection();
135+
$sql = "INSERT INTO " . $tableName . " (menu_id, store_id) values
136+
((select menu_id from " . $mainTable . " where " . $mainTable .".menu_id = " . $id . "), 0)";
137+
$connection->query($sql);
138+
}
139+
}

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"name": "scandiweb/module-menumanager",
33
"description": "Scandiweb Menu Manager module for Magento 2.",
44
"type": "magento2-module",
5-
"version": "0.1.7",
65
"license": [
76
"OSL-3.0",
87
"AFL-3.0"

etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
-->
1111
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
12-
<module name="Scandiweb_Menumanager" setup_version="0.1.7">
12+
<module name="Scandiweb_Menumanager" setup_version="0.1.8">
1313
<sequence>
1414
<module name="Scandiweb_Core"/>
1515
</sequence>

view/frontend/layout/default.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0"?>
2+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
3+
<body>
4+
<referenceBlock name="catalog.topnav" remove="true"/>
5+
<referenceBlock name="store.menu">
6+
<block class="Scandiweb\Menumanager\Block\Menu" name="custom.navigation" template="html/menu.phtml" before="-" ttl="3600">
7+
<arguments>
8+
<argument name="identifier" xsi:type="string">cool-menu</argument>
9+
</arguments>
10+
</block>
11+
</referenceBlock>
12+
</body>
13+
</page>

0 commit comments

Comments
 (0)