-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add menu_items option and fix an issue with include root_page option
- Loading branch information
1 parent
b2aa627
commit 1f6d343
Showing
4 changed files
with
45 additions
and
29 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
* MarkupMenu is a module for generating menu markup. See README.md for more details. | ||
* Some ideas and code in this module are based on the Markup Simple Navigation module. | ||
* | ||
* @version 0.7.0 | ||
* @version 0.8.0 | ||
* @author Teppo Koivula <[email protected]> | ||
* @license Mozilla Public License v2.0 http://mozilla.org/MPL/2.0/ | ||
*/ | ||
|
@@ -21,6 +21,7 @@ class MarkupMenu extends WireData implements Module { | |
*/ | ||
public static $defaultOptions = [ | ||
'root_page' => null, | ||
'menu_items' => null, | ||
'current_page' => null, | ||
'templates' => [ | ||
'nav' => '<nav class="{classes}">%s</nav>', | ||
|
@@ -71,34 +72,41 @@ public function render(array $options = []): string { | |
); | ||
|
||
// get the root page | ||
$options['root_page'] = $this->getPage($options['root_page'], '/'); | ||
$options['root_page'] = $this->getPage($options['root_page'], empty($options['menu_items']) ? '/' : null); | ||
|
||
// get current page | ||
$options['current_page'] = $this->getPage($options['current_page']); | ||
|
||
// load MarkupMenuData | ||
require_once __DIR__ . '/MarkupMenuData.php'; | ||
|
||
// generate and return menu markup | ||
$menu = $this->renderTree($options, $options['root_page']); | ||
// generate menu markup | ||
$menu = ''; | ||
if (!empty($options['root_page'] || !empty($options['menu_items']))) { | ||
$menu = $this->renderTree($options, $options['root_page'], $options['menu_items']); | ||
} | ||
|
||
return $menu; | ||
|
||
} | ||
|
||
/** | ||
* Render tree of items using recursion | ||
* | ||
* @param array $options Options for rendering | ||
* @param Page $root Root page for the menu | ||
* @param Page|null $root Root page for the menu | ||
* @param PageArray|null $items Menu items | ||
* @param int $level Current tree level (depth) | ||
* @return string Rendered menu markup | ||
*/ | ||
protected function renderTree(array $options = [], Page $root, int $level = 1): string { | ||
protected function renderTree(array $options = [], Page $root = null, PageArray $items = null, int $level = 1): string { | ||
|
||
$out = ''; | ||
|
||
// get items | ||
$items = $this->getItems($options, $root, $level); | ||
// get items and make sure that root page is only prepended once | ||
if (empty($items)) { | ||
$items = $this->getItems($options, $root, $level); | ||
$options['include']['root_page'] = false; | ||
} | ||
|
||
// iterate items and render markup for each separately | ||
foreach ($items as $item) { | ||
|
@@ -124,28 +132,26 @@ protected function renderTree(array $options = [], Page $root, int $level = 1): | |
} | ||
|
||
return $out; | ||
|
||
} | ||
|
||
/** | ||
* Get menu items for rendering | ||
* | ||
* @param array $options Options array. | ||
* @param Page $root Root page for the menu | ||
* @param array $options Options array | ||
* @param Page|null $root Root page for the menu | ||
* @param int $level Current tree level (depth) | ||
* @return PageArray Menu items | ||
*/ | ||
protected function ___getItems(array $options, Page $root, int $level): PageArray { | ||
protected function ___getItems(array $options, Page $root = null, int $level): PageArray { | ||
|
||
// fetch items (children of the root page), optionally filtered by a selector string | ||
$items = new PageArray(); | ||
if (!$options['include']['root_page'] || $options['flat_root']) { | ||
if (!empty($root) && (!$options['include']['root_page'] || $options['flat_root'])) { | ||
$items->add($root->children($options['include']['selector'])); | ||
} | ||
|
||
// optionally prepend the root page itself – but only once! | ||
if ($options['include']['root_page']) { | ||
$options['include']['root_page'] = false; | ||
// optionally prepend the root page itself | ||
if ($options['include']['root_page'] && !empty($root)) { | ||
$items->prepend($root); | ||
} | ||
|
||
|
@@ -162,11 +168,12 @@ protected function ___getItems(array $options, Page $root, int $level): PageArra | |
* | ||
* @param array $options Options for rendering | ||
* @param Page $item Menu item being rendered | ||
* @param Page|null $root Root page for the menu | ||
* @param bool $with_children Include markup for child pages? | ||
* @param int $level Current tree level (depth) | ||
* @return string Rendered menu item markup | ||
*/ | ||
protected function ___renderTreeItem(array $options = [], Page $item, Page $root, int $level = 1): string { | ||
protected function ___renderTreeItem(array $options = [], Page $item, Page $root = null, int $level = 1): string { | ||
|
||
$out = ''; | ||
|
||
|
@@ -186,14 +193,14 @@ protected function ___renderTreeItem(array $options = [], Page $item, Page $root | |
if ($item_is_current) $classes['current'] = $options['classes']['current']; | ||
|
||
// is this a parent page? | ||
$item_is_parent = !$item_is_current && ($item->id !== $root->id || !$options['flat_root']) && $options['current_page'] && $options['current_page']->parents->has($item); | ||
$item_is_parent = !$item_is_current && (!empty($root) && $item->id !== $root->id || !$options['flat_root']) && $options['current_page'] && $options['current_page']->parents->has($item); | ||
if ($item_is_parent) $classes['parent'] = $options['classes']['parent']; | ||
|
||
// have we reached the level limit? | ||
$level_limit_reached = $options['exclude']['level_greater_than'] && $level >= $options['exclude']['level_greater_than']; | ||
|
||
// does this page have children? | ||
$has_children = ($item->id !== $root->id || !$options['flat_root']) && !$level_limit_reached && $item->hasChildren; | ||
$has_children = (!empty($root) && $item->id !== $root->id || !$options['flat_root']) && !$level_limit_reached && $item->hasChildren; | ||
if ($has_children) $classes['has_children'] = $options['classes']['has_children']; | ||
|
||
// should we render the children for this item? | ||
|
@@ -222,14 +229,13 @@ protected function ___renderTreeItem(array $options = [], Page $item, Page $root | |
|
||
// generate markup for menu item children | ||
if ($with_children) { | ||
$item_markup .= $this->renderTree($options, $item, $level + 1); | ||
$item_markup .= $this->renderTree($options, $item, null, $level + 1); | ||
} | ||
|
||
// generate markup for current list item | ||
$out .= $this->applyTemplate('list_item', $item_markup, $placeholders, $options, $item); | ||
|
||
return $out; | ||
|
||
} | ||
|
||
/** | ||
|
@@ -254,7 +260,6 @@ protected function getPage($source = null, $default = null): ?Page { | |
} | ||
|
||
return $page; | ||
|
||
} | ||
|
||
/** | ||
|
@@ -301,7 +306,6 @@ protected function applyTemplate(string $template_name, string $content, array $ | |
} | ||
|
||
return $out; | ||
|
||
} | ||
|
||
/** | ||
|
@@ -337,7 +341,6 @@ protected function parseClassesString(array $placeholders, array $options, strin | |
} | ||
|
||
return $out; | ||
|
||
} | ||
|
||
} |
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