diff options
author | MaurĂcio Meneghini Fauth <mauricio@fauth.dev> | 2021-09-11 15:37:41 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-11 15:37:41 +0300 |
commit | 3be84cf9b26b402b2ca18bbd4aa4ac5975757f44 (patch) | |
tree | 1116e3d05cd59a30b4e526da3729a87abd2a25ae | |
parent | 8031072f2737312b52a7180ab99f554e3ee0da43 (diff) | |
parent | 9dba96216ea28bfa727f17d24a70fe33c6f1107e (diff) |
Merge pull request #17078 from lrb2/consolidate-nav-groups
Fix #17003: Consolidate nested groups in navigation tree
-rw-r--r-- | libraries/classes/Navigation/NavigationTree.php | 100 |
1 files changed, 67 insertions, 33 deletions
diff --git a/libraries/classes/Navigation/NavigationTree.php b/libraries/classes/Navigation/NavigationTree.php index e4a73a2c6a..1172ea0db4 100644 --- a/libraries/classes/Navigation/NavigationTree.php +++ b/libraries/classes/Navigation/NavigationTree.php @@ -835,11 +835,13 @@ class NavigationTree unset($prefixes[$key]); } + $numChildren = count($node->children); + // rfe #1634 Don't group if there's only one group and no other items if (count($prefixes) === 1) { $keys = array_keys($prefixes); $key = $keys[0]; - if ($prefixes[$key] == count($node->children) - 1) { + if ($prefixes[$key] == $numChildren - 1) { unset($prefixes[$key]); } } @@ -864,33 +866,7 @@ class NavigationTree $this->largeGroupWarning = true; } - $groups[$key] = new Node( - htmlspecialchars((string) $key), - Node::CONTAINER, - true - ); - $groups[$key]->separator = $node->separator; - $groups[$key]->separatorDepth = $node->separatorDepth - 1; - $groups[$key]->icon = ['image' => 'b_group', 'title' => __('Groups')]; - $groups[$key]->pos2 = $node->pos2; - $groups[$key]->pos3 = $node->pos3; - if ( - $node instanceof NodeTableContainer - || $node instanceof NodeViewContainer - ) { - $groups[$key]->links = [ - 'text' => [ - 'route' => $node->links['text']['route'], - 'params' => array_merge($node->links['text']['params'], ['tbl_group' => $key]), - ], - 'icon' => [ - 'route' => $node->links['icon']['route'], - 'params' => array_merge($node->links['icon']['params'], ['tbl_group' => $key]), - ], - ]; - } - - $node->addChild($groups[$key]); + $newChildren = []; foreach ($separators as $separator) { $separatorLength = strlen($separator); // FIXME: this could be more efficient @@ -932,19 +908,77 @@ class NavigationTree $newChild->links = $child->links; $newChild->pos2 = $child->pos2; $newChild->pos3 = $child->pos3; - $groups[$key]->addChild($newChild); foreach ($child->children as $elm) { $newChild->addChild($elm); } - $node->removeChild($child->name); + $newChildren[] = [ + 'node' => $newChild, + 'replaces_name' => $child->name, + ]; + } + } + + if (count($newChildren) === 0) { + continue; + } + + // If the current node is a standard group (not NodeTableContainer, etc.) + // and the new group contains all of the current node's children, combine them + $class = get_class($node); + if ( + count($newChildren) === $numChildren + && substr($class, strrpos($class, '\\') + 1) === 'Node' + ) { + $node->name .= $separators[0] . htmlspecialchars((string) $key); + $node->realName .= $separators[0] . htmlspecialchars((string) $key); + $node->separatorDepth--; + foreach ($newChildren as $newChild) { + $node->removeChild($newChild['replaces_name']); + $node->addChild($newChild['node']); + } + } else { + $groups[$key] = new Node( + htmlspecialchars((string) $key), + Node::CONTAINER, + true + ); + $groups[$key]->separator = $node->separator; + $groups[$key]->separatorDepth = $node->separatorDepth - 1; + $groups[$key]->icon = ['image' => 'b_group', 'title' => __('Groups')]; + $groups[$key]->pos2 = $node->pos2; + $groups[$key]->pos3 = $node->pos3; + if ( + $node instanceof NodeTableContainer + || $node instanceof NodeViewContainer + ) { + $groups[$key]->links = [ + 'text' => [ + 'route' => $node->links['text']['route'], + 'params' => array_merge($node->links['text']['params'], ['tbl_group' => $key]), + ], + 'icon' => [ + 'route' => $node->links['icon']['route'], + 'params' => array_merge($node->links['icon']['params'], ['tbl_group' => $key]), + ], + ]; + } + + foreach ($newChildren as $newChild) { + $node->removeChild($newChild['replaces_name']); + $groups[$key]->addChild($newChild['node']); } } } - foreach ($prefixes as $key => $value) { - $this->groupNode($groups[$key]); - $groups[$key]->classes = 'navGroup'; + foreach ($groups as $group) { + if (count($group->children) === 0) { + continue; + } + + $node->addChild($group); + $this->groupNode($group); + $group->classes = 'navGroup'; } } |