sector_megamenu-1.0.2/sector_megamenu.module
sector_megamenu.module
<?php
/**
* Implements hook_theme().
*/
function sector_megamenu_theme() {
return [
'menu__sector_megamenu_body' => [
'variables' => [
'sorted' => TRUE,
'items' => [],
'menu_name' => NULL,
'data' => NULL,
'type' => NULL,
'heading_link_text' => NULL,
],
'template' => 'menu--sector-megamenu-body',
],
'menu__sector_megamenu_root' => [
'variables' => [
'sorted' => TRUE,
'items' => [],
'menu_name' => NULL,
'data' => NULL,
'type' => NULL,
],
'template' => 'menu--sector-megamenu-root',
]
];
}
/**
* Implements hook_preprocess_menu().
*/
function sector_megamenu_preprocess_menu(&$variables, $hook) {
// No changes for menu toolbar.
if ($hook != 'menu__sector_megamenu_root' || $hook != 'menu__sector_megamenu_body') {
return;
}
// Get the current path.
$current_path = \Drupal::request()->getRequestUri();
foreach ($variables['items'] as $k => &$item) {
sector_megamenu_recursive_add_menu_link_attributes($item, $current_path);
}
}
/**
* Loop through menu item children, make sure menu_link_attributes attributes are available.
*/
function sector_megamenu_recursive_add_menu_link_attributes(&$item, $current_path) {
// Add your custom property to the menu item.
$menu_link = $item['original_link'] ?? NULL;
$options = !empty($menu_link) ? $menu_link->getOptions() : NULL;
$item['extra'] = $options['attributes'] ?? NULL;
$item['current'] = FALSE;
if ($item['url']->toString() === $current_path) {
$item['current'] = TRUE;
}
// Check if the menu item has children and process them recursively.
if (!empty($item['below'])) {
foreach ($item['below'] as &$child_item) {
sector_megamenu_recursive_add_menu_link_attributes($child_item, $current_path);
}
}
}
