Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usagov 1997 mobile menu block #2039

Merged
merged 3 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/sync/block.block.englishbannercode.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion config/sync/block.block.languageswitcher.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies:
id: languageswitcher
theme: usagov
region: header_top
weight: -12
weight: -9
provider: null
plugin: 'language_block:language_interface'
settings:
Expand Down
2 changes: 2 additions & 0 deletions config/sync/block.block.leftmenuspanish_3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions config/sync/block.block.mobile_navigation.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
uuid: c7dd9b01-0afc-404b-ba8e-a6ad8f901151
langcode: en
status: true
status: false
dependencies:
config:
- system.menu.left-menu-english
Expand All @@ -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:
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions config/sync/block.block.mobile_navigation_dr_en.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
uuid: ba88b0a4-52e5-4783-9a97-d569a3371e56
langcode: en
status: true
status: false
dependencies:
module:
- language
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions config/sync/block.block.mobile_navigation_dr_es.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
uuid: b223852b-7aab-455d-bc81-5ef1a403c378
langcode: en
status: true
status: false
dependencies:
module:
- language
Expand All @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions config/sync/block.block.mobile_navigation_es.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
uuid: 396e479e-afa6-43a5-878e-c4d95e6fb08a
langcode: en
status: true
status: false
dependencies:
config:
- system.menu.left-menu-spanish
Expand All @@ -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:
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion config/sync/block.block.mobile_navigation_wizard_english.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'
Expand Down
4 changes: 3 additions & 1 deletion config/sync/block.block.mobile_navigation_wizard_spanish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'
Expand Down
2 changes: 2 additions & 0 deletions config/sync/block.block.navigation_page_items.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 2 additions & 0 deletions config/sync/block.block.navigation_page_items_spanish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion config/sync/block.block.spanishbannercode.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion config/sync/block.block.usagov_branding.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ _core:
id: usagov_branding
theme: usagov
region: header
weight: 0
weight: -18
provider: null
plugin: system_branding_block
settings:
Expand Down
31 changes: 31 additions & 0 deletions config/sync/block.block.usagov_mobilemenublock.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions config/sync/block.block.wizard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
154 changes: 154 additions & 0 deletions web/modules/custom/usagov_menus/src/Plugin/Block/AbstractMenuBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

namespace Drupal\usagov_menus\Plugin\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\Routing\ResettableStackedRouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;

abstract class AbstractMenuBlock extends BlockBase implements ContainerFactoryPluginInterface {

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}
*/
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'),
);
}

/**
* 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;
}

}
Loading
Loading