Skip to content
Snippets Groups Projects
Commit 5e6b633b authored by Eric Bremner's avatar Eric Bremner Committed by Igor Biki
Browse files

ISTWCMS-4199: adding functions for getting menus to the uwService

parent 05abfe23
No related branches found
No related tags found
1 merge request!17Feature/istwcms 4199 ebremner menu items count
...@@ -271,24 +271,26 @@ class UWService implements UWServiceInterface { ...@@ -271,24 +271,26 @@ class UWService implements UWServiceInterface {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getUwMenu(string $menu_name = 'main'): array { public function uwGetMenu(string $menu_name = 'main', $include_parent_in_count = FALSE): array {
// Get the main menu from the simplify menu module. // Get the main menu from the simplify menu module.
$main_menu = $this->simplifyMenu->getMenuTree('main'); $menu = $this->simplifyMenu->getMenuTree($menu_name);
// Set it to the menu_tree which is done by simplify menu. // Set it to the menu_tree which is done by simplify menu.
$main_menu = $main_menu['menu_tree']; $menu = $menu['menu_tree'];
$menu_items_count = 0;
// Add the count of the menu items. // Add the count of the menu items.
$this->uwCountMenuItems($main_menu); $menu = $this->uwSetMenuItems($menu, $include_parent_in_count);
return $main_menu; return $menu;
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function uwCountMenuItems(&$menus): void { public function uwSetMenuItems($menus, $include_parent_in_count = FALSE): array {
// Step through the menu and do a few things: // Step through the menu and do a few things:
// (a) Remove the Home link (this will be a house icon) // (a) Remove the Home link (this will be a house icon)
...@@ -307,26 +309,65 @@ class UWService implements UWServiceInterface { ...@@ -307,26 +309,65 @@ class UWService implements UWServiceInterface {
else { else {
// Set the menu_item_count, initially to 0. // Set the menu_item_count, initially to 0.
$menu_item_count = 0; $menu_items_count = 0;
// If there is a submenu, recursivley call the count function. // If there is a submenu, recursivley call the count function.
if (isset($menu['submenu']) && count($menu['submenu']) > 0) { if (isset($menu['submenu']) && count($menu['submenu']) > 0) {
// Call the count function recursively till we have the number of // Call the count function recursively till we have the number of
// menu items. // menu items.
_uw_fdsu_theme_resp_count_menu_items($menu, $menu_item_count); $this->uwCountMenuItems($menu['submenu'], $menu_items_count);
// Set the menu items count. // Set the menu items count.
$main_menu[$index]['menu_items_count'] = $menu_item_count + 1; // If we need to include the parent in the count, for example the
// main menu will print the parent inside the tray, then we increment
// the menu items count by 1.
if ($include_parent_in_count) {
$menu_items_count++;
}
// Set the menu items count in the menu.
$menus[$index]['menu_items_count'] = $menu_items_count;
} // If there are no submenus, the count is 0, there will be no tray. }
// If there are no submenus, the count is 0, there will be no tray.
else { else {
// Set the number of items count, which is 0. // Set the number of items count, which is 0.
$main_menu[$index]['menu_items_count'] = $menu_item_count; $menus[$index]['menu_items_count'] = $menu_items_count;
} }
} }
} }
return $menus;
} }
/**
* {@inheritDoc}
*/
public function uwCountMenuItems($menus, &$menu_items_count): void {
// Step through the menus and check for submenus and count.
foreach ($menus as $index => $menu) {
// If there is a submenu, recursivley call the count function.
if (isset($menu['submenu']) && count($menu['submenu']) > 0) {
// Increment the menu items counter.
$menu_items_count++;
// Recursively check the submenu.
$this->uwCountMenuItems($menu['submenu'], $menu_items_count);
}
// If there are no submenus, simple increment the menu items counter.
else {
// Increment the menu items counter.
$menu_items_count++;
}
}
}
} }
...@@ -87,17 +87,28 @@ interface UWServiceInterface { ...@@ -87,17 +87,28 @@ interface UWServiceInterface {
* A function to get an array of menu. * A function to get an array of menu.
* *
* @param string $menu_name * @param string $menu_name
* A boolean to state if we want content types that can have sidebars. * A string to the machine name of the menu to get.
* @param $include_parent_in_count
* A boolean on whether to include the parent in the menu items count.
* *
* @return array * @return array
* An array of the menu. * An array of the menu.
*/ */
public function getUwMenu(string $menu_name = 'main'): array; public function uwGetMenu(string $menu_name = 'main', $include_parent_in_count = false): array;
/** /**
* A recursive function to count the number of menu items. * A function to setup the menu for UW display.
*
* This function will count of the number of menu items,
* and use a recursive function (uwCountMenuItems) to count
* the total number of menu items.
*
* The variable include_parent_in_count tells us if we need
* to add one to the total count of menu items. This is required
* for displaying some menus as the parent is also included
* in the displaying of the menu (i.e. the tray of the main and
* information for menus).
* *
* This function will count of the number of menu items.
* For example, the menu structure will look like: * For example, the menu structure will look like:
* Parent * Parent
* Child #1 * Child #1
...@@ -112,12 +123,26 @@ interface UWServiceInterface { ...@@ -112,12 +123,26 @@ interface UWServiceInterface {
* Grandchild #3-3 * Grandchild #3-3
* Great grandchild #3-3-1 * Great grandchild #3-3-1
* So it should with the recursive function it will return 11. * So it should with the recursive function it will return 11.
* If we include the parent the count will be 12.
*
* @param &$menus
* A reference to the array list of menu items.
* @param $include_parent_in_count
* A boolean on whether to include the parent in the menu items count.
*
* @return array
* An array of the updated menu items.
*/
public function uwSetMenuItems($menus, $include_parent_in_count = false): array;
/**
* A function to recursively count the number of menu items in the submenu.
* *
* @param $menus * @param $menu
* The array list of menu items. * An array that contains the submenu.
* @param &$menu_items_count * @param $menu_items_count
* The reference to the number of items variable. * A reference to the integer that is storing the number of menu items.
*/ */
public function uwCountMenuItems(&$menus): void; public function uwCountMenuItems(array $menu, int &$menu_items_count): void;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment