Created
September 27, 2017 09:12
-
-
Save Lazerproof/2af34fb50910b993a40eaabe803f0d72 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Processwire; | |
$menuModule = $modules->get('MarkupMenuBuilder'); | |
$lang_name = user("language")->name; | |
$menu_page = pages()->get(2092); | |
if($lang_name == "english") { | |
$menu_page = pages()->get(2095); | |
} elseif ($lang_name == "russian") { | |
$menu_page = pages()->get(2091); | |
} | |
$menu = $menuModule->getMenuItems($menu_page, 1, [ | |
"current_class_level" => 3 | |
]); | |
$mainmenu = buildMainMenu(0, $menu, 0, "mainmenu", "mainmenuList"); | |
?> | |
<nav class="mainmenu"> | |
<div class="mainmenu__inner"> | |
<?= $mainmenu; ?> | |
</div> | |
</nav> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Builds a nested list (menu items) of a single menu from an Array of menu items. | |
* | |
* A recursive function to display nested list of menu items. | |
* | |
* @access private | |
* @param Int $parent ID of menu item. | |
* @param Array $menu Array of menu items to display. | |
* @param Int $first Helper variable to designate first menu item. | |
* @return string $out. | |
* | |
*/ | |
function buildMainMenu($parent = 0, $menu, $first = 0, $class, $menuId) { | |
$out = ''; | |
$has_child = false; | |
foreach ($menu as $id => $m) { | |
$parentID = isset($m['parent_id']) ? $m['parent_id'] : 0; | |
$newtab = isset($m['newtab']) && $m['newtab'] ? " target='_blank'" : ''; | |
if (isset($m["pages_id"])) { | |
if($m["pages_id"] === 2026) { | |
if (page()->matches("has_parent=2026")) { | |
$m['is_current'] = 1; | |
} | |
} | |
} | |
if (isset($m["pages_id"])) { | |
if($m["pages_id"] === 1765) { | |
if (page()->matches("has_parent=1765")) { | |
$m['is_current'] = 1; | |
} | |
} | |
} | |
// if this menu item is a parent; create the sub-items/child-menu-items | |
if ($parentID == $parent) {// if this menu item is a parent; create the inner-items/child-menu-items | |
// if this is the first child | |
if ($has_child === false) { | |
$has_child = true;// This is a parent | |
if ($first == 0){ | |
$out .= "<ul id='{$menuId}' class='{$class}__list'>\n"; | |
$first = 1; | |
} | |
else $out .= "\n<ul class='{$class}__sublist'>\n"; | |
} | |
$isParentItemClass = isset($m['is_parent']) && $m['is_parent'] ? " is-dropdown-submenu-parent" : ""; | |
$isActiveItemClass = isset($m['is_current']) && $m['is_current'] ? " is-current" : ""; | |
$isFirstItemClass = isset($m['is_first']) && $m['is_first'] ? " {$class}__item--first" : ""; | |
$isLastItemClass = isset($m['is_last']) && $m['is_last'] ? " {$class}__item--last" : ""; | |
$isActiveLinkClass = isset($m['is_current']) && $m['is_current'] ? " is-current" : ""; | |
$subMenuClass = isset($m['parent_id']) ? " {$class}__subitem" : " {$class}__item"; | |
$subMenuLinkClass = isset($m['parent_id']) ? " {$class}__subitem-link" : " {$class}__item-link"; | |
// a menu item | |
$out .= "<li class='" . $isActiveItemClass . $isFirstItemClass . $isLastItemClass . $subMenuClass . $isParentItemClass . "'><a class='". $isActiveLinkClass . $subMenuLinkClass ."' href='" . $m["url"] . "'" . $newtab . "><span>" . $m["title"]; | |
// if menu item has children | |
if (isset($m['is_parent']) && $m['is_parent']) { | |
$out .= '</span></a>'; | |
} | |
else $out .= '</span></a>'; | |
// call function again to generate nested list for sub-menu items belonging to this menu item. | |
$out .= buildMainMenu($id, $menu, $first, $class, $menuId); | |
$out .= "</li>\n"; | |
}// end if parent | |
}// end foreach | |
if ($has_child === true) $out .= "</ul>\n"; | |
return $out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment