diff --git a/uw_fdsu_theme_resp.theme b/uw_fdsu_theme_resp.theme index 9728d8d827fc46bde4001ef6e07f7e87ce63d08a..9ef7494fd4c7c7700d2ee0b92cc4d8da1bf67d23 100644 --- a/uw_fdsu_theme_resp.theme +++ b/uw_fdsu_theme_resp.theme @@ -53,6 +53,41 @@ function uw_fdsu_theme_resp_preprocess_responsive_image(&$variables) { $variables['sources'] = $new_sources; } +/** + * A recursive function to count the number of menu items. + * + * @param $menus + * The array list of menu items. + * @param $menu_items_count + * The reference to the number of items variable. + */ +function _uw_fdsu_theme_resp_count_menu_items($menus, &$menu_items_count) { + + // Step through each of the menus. + foreach ($menus as $menu) { + + // If there is a submenu, then recursively go down through the list, + // until we get to place that doesnt have a submenu and we will + // increment the menu items count. + if (isset($menu['submenu']) && count($menu['submenu'] > 0)) { + + // Step through each of the submenus and send back to this function. + foreach ($menu['submenus'] as $submenu) { + + // Keep calling this function until we have no submenus. + _uw_fdsu_theme_resp_count_menu_items($submenu, $menu_items_count); + } + } + + // Here we have no submenus, so increment the menu items counter. + else { + + // Increment the menu items counter. + $menu_items_count++; + } + } +} + /** * Implements hook_preprocess_region(). */ @@ -66,6 +101,73 @@ function uw_fdsu_theme_resp_preprocess_region(&$variables) { // (i.e. faculty colour and get the correct class here). if ($region == "header") { + // Get the main menu from the simplify menu module. + $main_menu = \Drupal::service('simplify_menu.menu_items')->getMenuTree('main'); + + // Set it to the menu_tree which is done by simplify menu. + $main_menu = $main_menu['menu_tree']; + + // Step through the menu and do a few things: + // (a) Remove the Home link (this will be a house icon) + // (b) Add the number of menu items per menu link. + foreach ($main_menu as $index => $menu) { + + // If this is the Home link, then remove it. + if ($menu['text'] == "Home") { + + // Remove the home link. + unset($main_menu[$index]); + } + + // If we are not on the home link, then add the number + // of menu items in this menu link. + else { + + // Set the menu_item_count, initially to 0. + $menu_item_count = 0; + + // If there is a submenu, recursivley call the count function. + if (isset($menu['submenu']) && count($menu['submenu']) > 0) { + + // Call the count function recursively till we have the number of + // menu items. + _uw_fdsu_theme_resp_count_menu_items($menu, $menu_item_count); + + // Now that we have a count of the number of menu items, we need to + // increment it by one as the main menu will also contain a link + // to the parent (itself) in the Tray. + // For example, the menu structure will look like: + // Parent (click box in the menu) + // Parent (link to the the Parent) + // Child #1 + // Grandchild #1-1 + // Grandchild #1-2 + // Child #2 + // Child #3 + // Grandchild #3-1 + // Great grandchild #3-1-1 + // Great grandchild #3-1-2 + // Grandchild #3-2 + // Grandchild #3-3 + // Great grandchild #3-3-1 + // So it should with the recursive function it will return 11, + // however as we are printing the parent again in the tray, we + // actually need 12, so hence the increase by1. + $main_menu[$index]['menu_items_count'] = $menu_item_count + 1; + } + + // If there are no submenus, the count is 0, there will be no tray. + else { + + // Set the number of items count, which is 0. + $main_menu[$index]['menu_items_count'] = $menu_item_count; + } + } + } + + // Set the main menu variable. + $variables['main_menu'] = $main_menu; + // The class that is used for the header. $variables['classes'][] = 'uw-header';