MDL-72530 navigation: Optional max limit of displayed secondary nodes

Includes an optional function argument $maxdisplayednodes (int/null)
in force_nodes_into_more_menu() which will enable configuration of the
max limit of displayed nodes in the secondary navigation. If null,
a limit of displayed nodes will not be applied.
This commit is contained in:
Mihail Geshoski 2021-09-20 12:02:33 +08:00
parent 9145d80b0b
commit 2c9ab4980d
2 changed files with 47 additions and 5 deletions

View File

@ -345,8 +345,9 @@ class secondary extends view {
*
* @param array $defaultmoremenunodes Array with navigation node keys of the pre-defined nodes that
* should be added into the "more" menu by default
* @param int|null $maxdisplayednodes The maximum limit of navigation nodes displayed in the secondary navigation
*/
protected function force_nodes_into_more_menu(array $defaultmoremenunodes = []) {
protected function force_nodes_into_more_menu(array $defaultmoremenunodes = [], ?int $maxdisplayednodes = null) {
// Counter of the navigation nodes that are initially displayed in the secondary nav
// (excludes the nodes from the "more" menu).
$displayednodescount = 0;
@ -356,8 +357,9 @@ class secondary extends view {
continue;
}
// If the navigation node is in the pre-defined list of nodes that should be added by default in the
// "more" menu or the maximum limit of displayed navigation nodes has been reached.
if (in_array($child->key, $defaultmoremenunodes) || $displayednodescount >= self::MAX_DISPLAYED_NAV_NODES) {
// "more" menu or the maximum limit of displayed navigation nodes has been reached (if defined).
if (in_array($child->key, $defaultmoremenunodes) ||
(!is_null($maxdisplayednodes) && $displayednodescount >= $maxdisplayednodes)) {
// Force the node and its children into the "more" menu.
$child->set_force_into_more_menu(true);
continue;

View File

@ -247,12 +247,13 @@ class secondary_test extends \advanced_testcase {
* @param array $secondarynavnodesdata The array which contains the data used to generate the secondary navigation
* @param array $defaultmoremenunodes The array containing the keys of the navigation nodes which should be added
* to the "more" menu by default
* @param int|null $maxdisplayednodes The maximum limit of navigation nodes displayed in the secondary navigation
* @param array $expecedmoremenunodes The array containing the keys of the expected navigation nodes which are
* forced into the "more" menu
* @dataProvider test_force_nodes_into_more_menu_provider
*/
public function test_force_nodes_into_more_menu(array $secondarynavnodesdata, array $defaultmoremenunodes,
array $expecedmoremenunodes) {
?int $maxdisplayednodes, array $expecedmoremenunodes) {
global $PAGE;
// Create a dummy secondary navigation.
@ -263,7 +264,7 @@ class secondary_test extends \advanced_testcase {
$method = new ReflectionMethod('core\navigation\views\secondary', 'force_nodes_into_more_menu');
$method->setAccessible(true);
$method->invoke($secondary, $defaultmoremenunodes);
$method->invoke($secondary, $defaultmoremenunodes, $maxdisplayednodes);
$actualmoremenunodes = [];
foreach ($secondary->children as $node) {
@ -300,6 +301,7 @@ class secondary_test extends \advanced_testcase {
'navnode2',
'navnode4',
],
5,
[
'navnode2',
'navnode4',
@ -321,6 +323,27 @@ class secondary_test extends \advanced_testcase {
'navnode2',
'navnode4',
],
5,
[
'navnode2',
'navnode4',
],
],
'The max display limit of navigation nodes is not defined; ' .
'navnode2 and navnode4 are forced into "more" menu by default.' =>
[
[
[ 'text' => 'Navigation node 1', 'key' => 'navnode1'],
[ 'text' => 'Navigation node 2', 'key' => 'navnode2'],
[ 'text' => 'Navigation node 3', 'key' => 'navnode3'],
[ 'text' => 'Navigation node 4', 'key' => 'navnode4'],
[ 'text' => 'Navigation node 5', 'key' => 'navnode5'],
],
[
'navnode2',
'navnode4',
],
null,
[
'navnode2',
'navnode4',
@ -340,6 +363,7 @@ class secondary_test extends \advanced_testcase {
[ 'text' => 'Navigation node 8', 'key' => 'navnode8'],
],
[],
5,
[
'navnode6',
'navnode7',
@ -358,10 +382,26 @@ class secondary_test extends \advanced_testcase {
[ 'text' => 'Navigation node 6', 'key' => 'navnode6'],
],
[],
5,
[
'navnode6',
],
],
'The max display limit of navigation nodes is not defined; ' .
'no forced navigation nodes into "more" menu by default.' =>
[
[
[ 'text' => 'Navigation node 1', 'key' => 'navnode1'],
[ 'text' => 'Navigation node 2', 'key' => 'navnode2'],
[ 'text' => 'Navigation node 3', 'key' => 'navnode3'],
[ 'text' => 'Navigation node 4', 'key' => 'navnode4'],
[ 'text' => 'Navigation node 5', 'key' => 'navnode5'],
[ 'text' => 'Navigation node 6', 'key' => 'navnode6'],
],
[],
null,
[],
],
];
}
}