From 700244a6c3c405b41085cfe319cfcfd502f7f50a Mon Sep 17 00:00:00 2001 From: OscarMerida Date: Fri, 25 Oct 2024 15:48:12 -0400 Subject: [PATCH 1/2] USAGOV-1997-mobile-menu-block: Moves mobile menu generation to the usagov_menus module. Fixes a race condition bug where the mobile menu on sidebar pages could be exported incomplete by tome. --- config/sync/block.block.englishbannercode.yml | 2 +- config/sync/block.block.languageswitcher.yml | 2 +- config/sync/block.block.leftmenuspanish_3.yml | 2 + config/sync/block.block.mobile_navigation.yml | 6 +- .../block.block.mobile_navigation_dr_en.yml | 4 +- .../block.block.mobile_navigation_dr_es.yml | 4 +- .../sync/block.block.mobile_navigation_es.yml | 6 +- ...block.mobile_navigation_wizard_english.yml | 4 +- ...block.mobile_navigation_wizard_spanish.yml | 4 +- .../block.block.navigation_page_items.yml | 2 + ...ck.block.navigation_page_items_spanish.yml | 2 + config/sync/block.block.spanishbannercode.yml | 2 +- config/sync/block.block.usagov_branding.yml | 2 +- .../block.block.usagov_mobilemenublock.yml | 31 +++ config/sync/block.block.wizard.yml | 2 + .../src/Plugin/Block/AbstractMenuBlock.php | 154 +++++++++++++ .../src/Plugin/Block/MobileMenuBlock.php | 157 +++++++++++++ .../src/Plugin/Block/SidebarFirstBlock.php | 157 +------------ .../templates/usagov-menu-mobile.html.twig | 213 ++++++++++++++++++ .../templates/usagov-menu-sidebar.html.twig | 2 +- .../custom/usagov_menus/usagov_menus.info.yml | 3 +- .../custom/usagov_menus/usagov_menus.module | 16 ++ .../custom/usagov/sass/_mobile-menu.scss | 2 +- .../block--mobile-navigation.html.twig | 2 +- .../usagov/templates/breadcrumb.html.twig | 2 +- 25 files changed, 615 insertions(+), 168 deletions(-) create mode 100644 config/sync/block.block.usagov_mobilemenublock.yml create mode 100644 web/modules/custom/usagov_menus/src/Plugin/Block/AbstractMenuBlock.php create mode 100644 web/modules/custom/usagov_menus/src/Plugin/Block/MobileMenuBlock.php create mode 100644 web/modules/custom/usagov_menus/templates/usagov-menu-mobile.html.twig diff --git a/config/sync/block.block.englishbannercode.yml b/config/sync/block.block.englishbannercode.yml index 238e8fa0ba..8b32f96cbc 100644 --- a/config/sync/block.block.englishbannercode.yml +++ b/config/sync/block.block.englishbannercode.yml @@ -12,7 +12,7 @@ dependencies: id: englishbannercode theme: usagov region: header_top -weight: -16 +weight: -18 provider: null plugin: 'block_content:d61595f0-7e1c-4081-9eef-cdc5ebbf822b' settings: diff --git a/config/sync/block.block.languageswitcher.yml b/config/sync/block.block.languageswitcher.yml index 655f32da7b..a2fe6562da 100644 --- a/config/sync/block.block.languageswitcher.yml +++ b/config/sync/block.block.languageswitcher.yml @@ -9,7 +9,7 @@ dependencies: id: languageswitcher theme: usagov region: header_top -weight: -12 +weight: -9 provider: null plugin: 'language_block:language_interface' settings: diff --git a/config/sync/block.block.leftmenuspanish_3.yml b/config/sync/block.block.leftmenuspanish_3.yml index 3aa8f640e8..8a135e195e 100644 --- a/config/sync/block.block.leftmenuspanish_3.yml +++ b/config/sync/block.block.leftmenuspanish_3.yml @@ -29,7 +29,9 @@ settings: depth: 0 expand_all_items: false parent: 'left-menu-spanish:' + render_parent: false suggestion: left_menu_spanish + hide_on_nonactive: false visibility: language: id: language diff --git a/config/sync/block.block.mobile_navigation.yml b/config/sync/block.block.mobile_navigation.yml index abe0783bff..e110770120 100644 --- a/config/sync/block.block.mobile_navigation.yml +++ b/config/sync/block.block.mobile_navigation.yml @@ -1,6 +1,6 @@ uuid: c7dd9b01-0afc-404b-ba8e-a6ad8f901151 langcode: en -status: true +status: false dependencies: config: - system.menu.left-menu-english @@ -13,7 +13,7 @@ dependencies: id: mobile_navigation theme: usagov region: header_top -weight: -11 +weight: -15 provider: null plugin: 'menu_block:left-menu-english' settings: @@ -29,7 +29,9 @@ settings: depth: 0 expand_all_items: true parent: 'left-menu-english:' + render_parent: false suggestion: left_menu_english + hide_on_nonactive: false visibility: language: id: language diff --git a/config/sync/block.block.mobile_navigation_dr_en.yml b/config/sync/block.block.mobile_navigation_dr_en.yml index 5b734243f3..903c03b753 100644 --- a/config/sync/block.block.mobile_navigation_dr_en.yml +++ b/config/sync/block.block.mobile_navigation_dr_en.yml @@ -1,6 +1,6 @@ uuid: ba88b0a4-52e5-4783-9a97-d569a3371e56 langcode: en -status: true +status: false dependencies: module: - language @@ -11,7 +11,7 @@ dependencies: id: mobile_navigation_dr_en theme: usagov region: header_top -weight: -10 +weight: -14 provider: null plugin: system_main_block settings: diff --git a/config/sync/block.block.mobile_navigation_dr_es.yml b/config/sync/block.block.mobile_navigation_dr_es.yml index a51b2d94e8..172caceb1d 100644 --- a/config/sync/block.block.mobile_navigation_dr_es.yml +++ b/config/sync/block.block.mobile_navigation_dr_es.yml @@ -1,6 +1,6 @@ uuid: b223852b-7aab-455d-bc81-5ef1a403c378 langcode: en -status: true +status: false dependencies: module: - language @@ -11,7 +11,7 @@ dependencies: id: mobile_navigation_dr_es theme: usagov region: header_top -weight: -8 +weight: -12 provider: null plugin: system_main_block settings: diff --git a/config/sync/block.block.mobile_navigation_es.yml b/config/sync/block.block.mobile_navigation_es.yml index a78d785081..41ad0643c6 100644 --- a/config/sync/block.block.mobile_navigation_es.yml +++ b/config/sync/block.block.mobile_navigation_es.yml @@ -1,6 +1,6 @@ uuid: 396e479e-afa6-43a5-878e-c4d95e6fb08a langcode: en -status: true +status: false dependencies: config: - system.menu.left-menu-spanish @@ -13,7 +13,7 @@ dependencies: id: mobile_navigation_es theme: usagov region: header_top -weight: -9 +weight: -13 provider: null plugin: 'menu_block:left-menu-spanish' settings: @@ -29,7 +29,9 @@ settings: depth: 0 expand_all_items: true parent: 'left-menu-spanish:' + render_parent: false suggestion: left_menu_spanish + hide_on_nonactive: false visibility: language: id: language diff --git a/config/sync/block.block.mobile_navigation_wizard_english.yml b/config/sync/block.block.mobile_navigation_wizard_english.yml index 30361fab09..53bedd2532 100644 --- a/config/sync/block.block.mobile_navigation_wizard_english.yml +++ b/config/sync/block.block.mobile_navigation_wizard_english.yml @@ -13,7 +13,7 @@ dependencies: id: mobile_navigation_wizard_english theme: usagov region: header_top -weight: 0 +weight: -11 provider: null plugin: 'menu_block:wizard' settings: @@ -29,7 +29,9 @@ settings: depth: 0 expand_all_items: false parent: 'wizard:' + render_parent: false suggestion: wizard + hide_on_nonactive: false visibility: 'entity_bundle:node': id: 'entity_bundle:node' diff --git a/config/sync/block.block.mobile_navigation_wizard_spanish.yml b/config/sync/block.block.mobile_navigation_wizard_spanish.yml index 79a87c5734..5581caf05e 100644 --- a/config/sync/block.block.mobile_navigation_wizard_spanish.yml +++ b/config/sync/block.block.mobile_navigation_wizard_spanish.yml @@ -13,7 +13,7 @@ dependencies: id: mobile_navigation_wizard_spanish theme: usagov region: header_top -weight: 0 +weight: -10 provider: null plugin: 'menu_block:wizard-espanol' settings: @@ -29,7 +29,9 @@ settings: depth: 0 expand_all_items: false parent: 'wizard-espanol:' + render_parent: false suggestion: wizard_espanol + hide_on_nonactive: false visibility: 'entity_bundle:node': id: 'entity_bundle:node' diff --git a/config/sync/block.block.navigation_page_items.yml b/config/sync/block.block.navigation_page_items.yml index cd6f553860..010e15ce7a 100644 --- a/config/sync/block.block.navigation_page_items.yml +++ b/config/sync/block.block.navigation_page_items.yml @@ -29,7 +29,9 @@ settings: depth: 0 expand_all_items: false parent: 'left-menu-english:' + render_parent: false suggestion: navigation_page_items + hide_on_nonactive: false visibility: 'entity_bundle:node': id: 'entity_bundle:node' diff --git a/config/sync/block.block.navigation_page_items_spanish.yml b/config/sync/block.block.navigation_page_items_spanish.yml index fd860cc63b..52cf7fa3c8 100644 --- a/config/sync/block.block.navigation_page_items_spanish.yml +++ b/config/sync/block.block.navigation_page_items_spanish.yml @@ -29,7 +29,9 @@ settings: depth: 0 expand_all_items: false parent: 'left-menu-spanish:' + render_parent: false suggestion: navigation_page_items + hide_on_nonactive: false visibility: 'entity_bundle:node': id: 'entity_bundle:node' diff --git a/config/sync/block.block.spanishbannercode.yml b/config/sync/block.block.spanishbannercode.yml index 5b1c9a6cd5..883910ac8c 100644 --- a/config/sync/block.block.spanishbannercode.yml +++ b/config/sync/block.block.spanishbannercode.yml @@ -12,7 +12,7 @@ dependencies: id: spanishbannercode theme: usagov region: header_top -weight: -15 +weight: -17 provider: null plugin: 'block_content:08f40fa5-9c3d-4d4b-b5ed-60f8614b1fd0' settings: diff --git a/config/sync/block.block.usagov_branding.yml b/config/sync/block.block.usagov_branding.yml index 16efbf0863..396bd3ee51 100644 --- a/config/sync/block.block.usagov_branding.yml +++ b/config/sync/block.block.usagov_branding.yml @@ -11,7 +11,7 @@ _core: id: usagov_branding theme: usagov region: header -weight: 0 +weight: -18 provider: null plugin: system_branding_block settings: diff --git a/config/sync/block.block.usagov_mobilemenublock.yml b/config/sync/block.block.usagov_mobilemenublock.yml new file mode 100644 index 0000000000..0bfbf9ab9d --- /dev/null +++ b/config/sync/block.block.usagov_mobilemenublock.yml @@ -0,0 +1,31 @@ +uuid: b460fa8c-9bac-400d-820a-af6b3ae303d9 +langcode: en +status: true +dependencies: + module: + - node + - usagov_menus + theme: + - usagov +id: usagov_mobilemenublock +theme: usagov +region: header_top +weight: -16 +provider: null +plugin: usagov_mobile_menu_block +settings: + id: usagov_mobile_menu_block + label: 'Mobile Menu Block' + label_display: '0' + provider: usagov_menus +visibility: + 'entity_bundle:node': + id: 'entity_bundle:node' + negate: false + context_mapping: + node: '@node.node_route_context:node' + bundles: + basic_page: basic_page + bears_life_event: bears_life_event + directory_record: directory_record + state_directory_record: state_directory_record diff --git a/config/sync/block.block.wizard.yml b/config/sync/block.block.wizard.yml index 6564fb4a3c..54f4bcb575 100644 --- a/config/sync/block.block.wizard.yml +++ b/config/sync/block.block.wizard.yml @@ -28,7 +28,9 @@ settings: depth: 0 expand_all_items: false parent: 'wizard:' + render_parent: false suggestion: wizard + hide_on_nonactive: false visibility: 'entity_bundle:node': id: 'entity_bundle:node' diff --git a/web/modules/custom/usagov_menus/src/Plugin/Block/AbstractMenuBlock.php b/web/modules/custom/usagov_menus/src/Plugin/Block/AbstractMenuBlock.php new file mode 100644 index 0000000000..a375062b0c --- /dev/null +++ b/web/modules/custom/usagov_menus/src/Plugin/Block/AbstractMenuBlock.php @@ -0,0 +1,154 @@ +get('language_manager')->getCurrentLanguage(), + request: $container->get('request_stack')->getCurrentRequest(), + routeMatch: $container->get('current_route_match'), + menuLinkManager: $container->get('plugin.manager.menu.link'), + menuTree: $container->get('menu.link_tree'), + trail: $container->get('menu.active_trail'), + ); + } + + /** + * Get the menu items for the path of crumbs passed. + * + * @return array + * A renderable array. + * + * @throws \Drupal\Component\Plugin\Exception\PluginException + */ + protected function getMenuTreeItems( + string $menuID, + array $crumbs = [], + ?MenuLinkInterface $active = NULL, + bool $closeLastTrail = FALSE, + ?int $maxLevels = 3, + ): array { + // Get siblings from menu. + $params = new MenuTreeParameters(); + $params->onlyEnabledLinks(); + + if ($crumbs) { + $params->setActiveTrail($crumbs); + $depth = count($crumbs); + } + else { + $depth = 1; + } + + if ($active) { + $children = $this->menuLinkManager->getChildIds($active->getPluginId()); + $children = array_filter($children, function (string $uuid) { + // Above, getChildIds returns children regardless of visibility. + return $this->menuLinkManager->createInstance($uuid)->isEnabled(); + }); + + // Check if the expanded menu is 3 or more levels deep and adjust + // what we show based on if we have children elements to show. + if ($maxLevels > 0 && $depth >= $maxLevels && $children) { + // Current link has children, so only show + // grandparent through children. + $params->setMinDepth($depth - 1); + } + elseif ($maxLevels > 0 && $depth >= $maxLevels) { + // No children to show, display the menu starting + // 2 Levels above us. + $params->setMinDepth($depth - 2); + } + } + else { + // There's no active path, just show the top level + // topic menu link elements. + $params->setMaxDepth(1); + } + + if ($closeLastTrail) { + // Don't open beyond the last link in $crumb. + $params->setMaxDepth($depth); + } + + $tree = $this->menuTree->load($menuID, $params); + // Remove items not in trail. + if ($crumbs) { + $tree = array_filter($tree, function (MenuLinkTreeElement $item) { + return $item->inActiveTrail; + }); + } + + // Sort by menu weight and ensure user can access the + // entities and nodes linked in the menu. + $tree = $this->menuTree->transform($tree, [ + ['callable' => 'menu.default_tree_manipulators:checkNodeAccess'], + ['callable' => 'menu.default_tree_manipulators:checkAccess'], + ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'], + ]); + + return $this->menuTree->build($tree); + } + + /** + * Find all the parents for the menu link. + * + * @return string[] + * Array of menu_link_content UUIDS. + * + * @throws \Drupal\Component\Plugin\Exception\PluginException + */ + protected function getParents(MenuLinkInterface $active): array { + $parentUUID = $active->getParent(); + $crumbs = [$active->getPluginId()]; + + while ($parentUUID) { + array_unshift($crumbs, $parentUUID); + $parent = $this->menuLinkManager->createInstance($parentUUID); + $parentUUID = $parent->getParent(); + } + + return $crumbs; + } + +} diff --git a/web/modules/custom/usagov_menus/src/Plugin/Block/MobileMenuBlock.php b/web/modules/custom/usagov_menus/src/Plugin/Block/MobileMenuBlock.php new file mode 100644 index 0000000000..8a10c154d9 --- /dev/null +++ b/web/modules/custom/usagov_menus/src/Plugin/Block/MobileMenuBlock.php @@ -0,0 +1,157 @@ +language->getId()) { + case 'es': + $menuID = 'left-menu-spanish'; + $this->translations = [ + 'home' => 'Página principal', + 'home_URL' => '/es', + 'close' => 'Cerrar', + 'search' => 'Buscar', + 'search_placeholder' => 'Busque en este sitio...', + 'search_affiliate' => 'usagov_es_internal', + 'all_topics' => 'Todos los temas y servicios', + 'phone_URL' => '/es/llamenos', + 'form_id' => 'usagov_es_internal-mobile', + ]; + break; + + case 'en': + default: + $menuID = 'left-menu-english'; + $this->translations = [ + 'home' => 'Home', + 'home_URL' => '/', + 'close' => 'Close', + 'search' => 'Search', + 'search_placeholder' => 'Search all government', + 'search_affiliate' => 'usagov_all_gov', + 'all_topics' => 'All topics and services', + 'phone_URL' => '/phone', + 'form_id' => 'usagov_all_gov-mobile' + ]; + break; + } + + if ($active = $this->trail->getActiveLink($menuID)) { + $crumbs = $this->menuLinkManager->getParentIds($active->getPluginId()); + $items = $this->getMenuTreeItems($menuID, $crumbs, $active, maxLevels: -1); + $twigVars = $this->prepareMenuItemsForTemplate($items, $active); + return $this->renderItems($items, $twigVars); + + } + + // We're not in the menu. + // Display first level of this menu. + $items = $this->getMenuTreeItems($menuID); + + $twigVars = [ + '#active_trail' => [], // tells twig to render submenu + '#found_active_item' => FALSE, + '#active_item_has_children' => TRUE, + '#siblings_of_active_item' => [], + '#submenu' => $items['#items'], + ]; + return $this->renderItems($items, $twigVars); + } + + /** + * Returns the render array to theme the navigation lists. + */ + private function renderItems(array $items, array $twigVars): array { + + $node = $this->routeMatch->getParameter('node'); + return array_merge( + [ + '#theme' => 'usagov_menu_mobile', + '#main_nav_items' => $items, + '#node' => $node, + '#translations' => $this->translations, + + // Ensure drupal knows this block should be cached per path. + '#cache' => [ + 'contexts' => ['url.path', 'url.query_args'], + ] + ], + $twigVars + ); + } + + /** + * @param array $submenu A render-array with #items + * + * @return array + */ + private function prepareMenuItemsForTemplate(array $submenu, MenuLinkInterface $active): array { + $active_trail = []; + $found_active_item = FALSE; + $active_item_has_children = FALSE; + $siblings_of_active_item = NULL; + + $currentURL = $active->getUrlObject()->toString(); + + // Create an array of the active trail items from each level of the + // menu (up to the active item) + $submenu = $submenu['#items']; + while ($submenu && !$found_active_item) { + $menuItem = array_filter($submenu, fn($item) => $item['in_active_trail'] === TRUE); + $key = array_key_first($menuItem); + $menuItem = $menuItem[$key] ?? false; + + if (!$menuItem && !$key) { + // No active link in the menu? We should bail. + // Template takes care of showing things. + break; + } + + // we're done when we find the current page + if ($menuItem['url']->toString() === $currentURL) { + $menuItem['active'] = TRUE; + $found_active_item = TRUE; + if (!empty($menuItem['below'])) { + $active_item_has_children = TRUE; + $submenu = $menuItem['below']; + } + else { + $submenu[$key]['active'] = TRUE; + $siblings_of_active_item = $submenu; + $submenu = []; + } + } + else { + $submenu = $menuItem['below'] ?: []; + } + + // add to active trail + $active_trail[] = $menuItem; + } + + return [ + '#active_trail' => $active_trail, + '#found_active_item' => $found_active_item, + '#active_item_has_children' => $active_item_has_children, + '#siblings_of_active_item' => $siblings_of_active_item, + '#submenu' => $submenu, + ]; + } + +} diff --git a/web/modules/custom/usagov_menus/src/Plugin/Block/SidebarFirstBlock.php b/web/modules/custom/usagov_menus/src/Plugin/Block/SidebarFirstBlock.php index 9d020486db..683ffac371 100644 --- a/web/modules/custom/usagov_menus/src/Plugin/Block/SidebarFirstBlock.php +++ b/web/modules/custom/usagov_menus/src/Plugin/Block/SidebarFirstBlock.php @@ -3,20 +3,8 @@ namespace Drupal\usagov_menus\Plugin\Block; use Drupal\Core\Block\Attribute\Block; -use Drupal\Core\Block\BlockBase; -use Drupal\Core\Language\LanguageInterface; -use Drupal\Core\Menu\MenuActiveTrailInterface; use Drupal\Core\Menu\MenuLinkInterface; -use Drupal\Core\Menu\MenuLinkManagerInterface; -use Drupal\Core\Menu\MenuLinkTreeElement; -use Drupal\Core\Menu\MenuLinkTreeInterface; -use Drupal\Core\Menu\MenuTreeParameters; -use Drupal\Core\Plugin\ContainerFactoryPluginInterface; -use Drupal\Core\Render\Element\Item; -use Drupal\Core\Routing\ResettableStackedRouteMatchInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; -use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\HttpFoundation\Request; /** * Provides a Custom Sidebar Navigation block. @@ -41,27 +29,13 @@ admin_label: new TranslatableMarkup("Left Menu Sidebar Block"), category: new TranslatableMarkup("USAgov"), )] -class SidebarFirstBlock extends BlockBase implements ContainerFactoryPluginInterface { +class SidebarFirstBlock extends AbstractMenuBlock { private const AGENCIES_NID_EN = 629; private const AGENCIES_NID_ES = 1696; private const STATES_NID_EN = 1885; private const STATES_NID_ES = 1870; - public function __construct( - array $configuration, - string $plugin_id, - mixed $plugin_definition, - protected LanguageInterface $language, - protected Request $request, - protected ResettableStackedRouteMatchInterface $routeMatch, - protected MenuLinkManagerInterface $menuLinkManager, - protected MenuLinkTreeInterface $menuTree, - protected MenuActiveTrailInterface $trail, - ) { - parent::__construct($configuration, $plugin_id, $plugin_definition); - } - /** * {@inheritdoc} * @@ -127,102 +101,6 @@ private function buildFromParentNodeId(string $menuID, $parentNodeID, bool $clos return $this->renderItems($items, $active, $leaf); } - /** - * Get the menu items for the path of crumbs passed. - * - * @return array - * A renderable array. - * - * @throws \Drupal\Component\Plugin\Exception\PluginException - */ - public function getMenuTreeItems( - string $menuID, - array $crumbs = [], - ?MenuLinkInterface $active = NULL, - bool $closeLastTrail = FALSE, - ): array { - // Get siblings from menu. - $params = new MenuTreeParameters(); - $params->onlyEnabledLinks(); - - if ($crumbs) { - $params->setActiveTrail($crumbs); - $depth = count($crumbs); - } - else { - $depth = 1; - } - - if ($active) { - $children = $this->menuLinkManager->getChildIds($active->getPluginId()); - $children = array_filter($children, function (string $uuid) { - // Above, getChildIds returns children regardless of visibility. - return $this->menuLinkManager->createInstance($uuid)->isEnabled(); - }); - - // Check if the expanded menu is 3 or more levels deep and adjust - // what we show based on if we have children elements to show. - if ($depth >= 3 && $children) { - // Current link has children, so only show - // grandparent through children. - $params->setMinDepth($depth - 1); - } - elseif ($depth >= 3) { - // No children to show, display the menu starting - // 2 Levels above us. - $params->setMinDepth($depth - 2); - } - } - else { - // There's no active path, just show the top level - // topic menu link elements. - $params->setMaxDepth(1); - } - - if ($closeLastTrail) { - // Don't open beyond the last link in $crumb. - $params->setMaxDepth($depth); - } - - $tree = $this->menuTree->load($menuID, $params); - // Remove items not in trail. - if ($crumbs) { - $tree = array_filter($tree, function (MenuLinkTreeElement $item) { - return $item->inActiveTrail; - }); - } - - // Sort by menu weight and ensure user can access the - // entities and nodes linked in the menu. - $tree = $this->menuTree->transform($tree, [ - ['callable' => 'menu.default_tree_manipulators:checkNodeAccess'], - ['callable' => 'menu.default_tree_manipulators:checkAccess'], - ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'], - ]); - - return $this->menuTree->build($tree); - } - - /** - * Find all the parents for the menu link. - * - * @return string[] - * Array of menu_link_content UUIDS. - * - * @throws \Drupal\Component\Plugin\Exception\PluginException - */ - private function getParents(MenuLinkInterface $active): array { - $parentUUID = $active->getParent(); - $crumbs = [$active->getPluginId()]; - while ($parentUUID) { - array_unshift($crumbs, $parentUUID); - $parent = $this->menuLinkManager->createInstance($parentUUID); - $parentUUID = $parent->getParent(); - } - - return $crumbs; - } - /** * Returns the render array to theme the navigation lists. */ @@ -233,11 +111,13 @@ private function renderItems( ): array { if (!empty($items['#items'])) { - $currentURL = $active->getUrlObject()->toString(); + $currentURL = $active?->getUrlObject()->toString(); if ($leaf) { $currentURL = $leaf['url']; } - $menutree = reset($this->prepareMenuItemsForTemplate($items['#items'], $currentURL, $leaf)); + + $menutree = $this->prepareMenuItemsForTemplate($items['#items'], $currentURL, $leaf); + $menutree = reset($menutree); $theme = [ '#theme' => 'usagov_menu_sidebar', @@ -265,7 +145,8 @@ private function prepareMenuItemsForTemplate($items, $currentURL, $leaf): array $menuTree = []; foreach ($items as $item) { $below = NULL; - if ($item['in_active_trail']) { + $in_active_trail = $item['in_active_trail'] ?? false; + if ($in_active_trail) { if ($item['below']) { $below = $this->prepareMenuItemsForTemplate($item['below'], $currentURL, $leaf); } @@ -281,7 +162,7 @@ private function prepareMenuItemsForTemplate($items, $currentURL, $leaf): array array_push($menuTree, (object) [ 'title' => $item['title'], 'url' => $url, - 'active' => $item['in_active_trail'], + 'active' => $in_active_trail, 'current' => $currentURL === $url, 'below' => $below, ]); @@ -289,26 +170,4 @@ private function prepareMenuItemsForTemplate($items, $currentURL, $leaf): array return $menuTree; } - /** - * {@inheritdoc} - */ - public static function create( - ContainerInterface $container, - array $configuration, - $plugin_id, - $plugin_definition, - ): self { - return new static( - $configuration, - $plugin_id, - $plugin_definition, - language: $container->get('language_manager')->getCurrentLanguage(), - request: $container->get('request_stack')->getCurrentRequest(), - routeMatch: $container->get('current_route_match'), - menuLinkManager: $container->get('plugin.manager.menu.link'), - menuTree: $container->get('menu.link_tree'), - trail: $container->get('menu.active_trail'), - ); - } - } diff --git a/web/modules/custom/usagov_menus/templates/usagov-menu-mobile.html.twig b/web/modules/custom/usagov_menus/templates/usagov-menu-mobile.html.twig new file mode 100644 index 0000000000..20f4ff2481 --- /dev/null +++ b/web/modules/custom/usagov_menus/templates/usagov-menu-mobile.html.twig @@ -0,0 +1,213 @@ +{# Create an array of the active trail items from each level of the menu (up to the active item) #} + +{% if node.type[0].value.target_id == "directory_record" %} + {% if node.langcode.langcode == "es" %} + {% set active_trail = [ + { + title: "Acerca de EE. UU. y directorios del Gobierno", + url: "/es/acerca-de-estados-unidos", + in_active_trail: true, + active: false + }, + { + title: "Agencias federales", + url: "/es/indice-agencias", + in_active_trail: true, + active: false + } + ] %} + {% else %} + {% set active_trail = [ + { + title: "About the U.S. and its government", + url: "/about-the-us", + in_active_trail: true, + active: false + }, + { + title: "A-Z index of U.S. government departments and agencies", + url: "/agency-index", + in_active_trail: true, + active: false + } + ] %} + {% endif %} +{% endif %} + +{% if node.type[0].value.target_id == "state_directory_record" %} + {% if node.langcode.langcode == "es" %} + {% set active_trail = [ + { + title: "Acerca de EE. UU. y directorios del Gobierno", + url: "/es/acerca-de-estados-unidos", + in_active_trail: true, + active: false + }, + { + title: "Gobiernos estatales", + url: "/es/gobiernos-estatales", + in_active_trail: true, + active: false + }, + ] %} + {% else %} + {% set active_trail = [ + { + title: "About the U.S. and its government", + url: "/about-the-us", + in_active_trail: true, + active: false + }, + { + title: "State governments", + url: "/state-governments", + in_active_trail: true, + active: false + } + ] %} + {% endif %} +{% endif %} + + +{# Build the mobile menu #} + diff --git a/web/modules/custom/usagov_menus/templates/usagov-menu-sidebar.html.twig b/web/modules/custom/usagov_menus/templates/usagov-menu-sidebar.html.twig index 901030df73..d6692fa0ef 100644 --- a/web/modules/custom/usagov_menus/templates/usagov-menu-sidebar.html.twig +++ b/web/modules/custom/usagov_menus/templates/usagov-menu-sidebar.html.twig @@ -54,4 +54,4 @@ lang (String) The current page's langcode {% endfor %} {% endif %} -{% endmacro %} \ No newline at end of file +{% endmacro %} diff --git a/web/modules/custom/usagov_menus/usagov_menus.info.yml b/web/modules/custom/usagov_menus/usagov_menus.info.yml index e76ae39ef7..b8cf7871bd 100644 --- a/web/modules/custom/usagov_menus/usagov_menus.info.yml +++ b/web/modules/custom/usagov_menus/usagov_menus.info.yml @@ -1,4 +1,4 @@ -name: 'USAGov Sidebar Menu' +name: 'USAGov Menu Blocks' type: module description: 'Provides blocks to display sidebars' core_version_requirement: ^9 || ^10 @@ -6,3 +6,4 @@ package: USAgov dependencies: - drupal:block - usa_twig_vars + - simplify_menu diff --git a/web/modules/custom/usagov_menus/usagov_menus.module b/web/modules/custom/usagov_menus/usagov_menus.module index db44790221..eab088aa41 100644 --- a/web/modules/custom/usagov_menus/usagov_menus.module +++ b/web/modules/custom/usagov_menus/usagov_menus.module @@ -5,6 +5,22 @@ */ function usagov_menus_theme($existing, $type, $theme, $path) { return [ + 'usagov_menu_mobile' => [ + 'variables' => [ + 'main_nav_items' => NULL, + 'node' => NULL, + 'translations' => NULL, + + 'active_trail' => NULL, + 'found_active_item' => NULL, + 'active_item_has_children' => NULL, + 'siblings_of_active_item' => NULL, + 'submenu' => NULL, + + 'depth' => NULL, + 'current' => NULL, + ], + ], 'usagov_menu_sidebar' => [ 'variables' => [ 'menutree' => NULL, diff --git a/web/themes/custom/usagov/sass/_mobile-menu.scss b/web/themes/custom/usagov/sass/_mobile-menu.scss index 7296159c2e..6093661be3 100644 --- a/web/themes/custom/usagov/sass/_mobile-menu.scss +++ b/web/themes/custom/usagov/sass/_mobile-menu.scss @@ -31,7 +31,7 @@ ul.navigation__items { } } - .umbrella a { + .umbrella > a { @include u-bg('blue-warm-70v'); @include u-text('white'); } diff --git a/web/themes/custom/usagov/templates/block--mobile-navigation.html.twig b/web/themes/custom/usagov/templates/block--mobile-navigation.html.twig index 5327332bef..2b30279869 100644 --- a/web/themes/custom/usagov/templates/block--mobile-navigation.html.twig +++ b/web/themes/custom/usagov/templates/block--mobile-navigation.html.twig @@ -270,5 +270,5 @@ {% endif %} {% endblock %} - + diff --git a/web/themes/custom/usagov/templates/breadcrumb.html.twig b/web/themes/custom/usagov/templates/breadcrumb.html.twig index 358ea7b575..9156b6291f 100644 --- a/web/themes/custom/usagov/templates/breadcrumb.html.twig +++ b/web/themes/custom/usagov/templates/breadcrumb.html.twig @@ -41,7 +41,7 @@ {% set breadcrumb = wizard_breadcrumb %} {% endif %} - + {% if breadcrumb %}