Answer the question
In order to leave comments, you need to log in
How to programmatically change the menu?
Drupal 7
There is a module for a custom menu. I create links in the admin.
The menu is rendered on the page. How can I change it programmatically? Those. what hook should be used so that it can be retrieved and changed before rendering?
Answer the question
In order to leave comments, you need to log in
Here is an example code of my render menu 'main-menu'
function custom_main_menu() {
global $language;
$item = menu_get_item();
$bp = base_path();
$menu_data = menu_tree_all_data('main-menu');
$menu_data = i18n_menu_localize_tree($menu_data,$language->language);
$menu_data = menu_tree_output($menu_data);
$menu_data = array_intersect_key($menu_data, array_flip(array_filter(array_keys($menu_data), 'is_numeric')));
$menu = '<ul class="navbar-nav">';
foreach ($menu_data as $menu_link) {
$link = $menu_link['#original_link'];
$submenu = $menu_link['#below'];
if($submenu) {
$class = ' ts-has-child';
$submenu = array_intersect_key($submenu, array_flip(array_filter(array_keys($submenu), 'is_numeric')));
} else $class = '' ;
$link['link_path'] == $item['path'] ? $active = ' active' : $active = '' ;
$link['external'] ? $url = $link['link_path'] : $url = $bp.$link['link_path'];
$menu .= '<li class="nav-item'.$class.'">';
$menu .= '<a class="nav-link'.$active.'" href="'.$url.'">'.$menu_link['#title'];
$active ? $menu .= '<span class="sr-only">(current)</span>' : NULL ;
$menu .= '</a>';
if($submenu) {
$menu .= '<span class="d-block d-sm-none carret"></span><ul class="ts-child">';
foreach ($submenu as $submenu_link) {
$link = $submenu_link['#original_link'];
$submenu2 = $submenu_link['#below']; $submenu = array_intersect_key($submenu, array_flip(array_filter(array_keys($submenu), 'is_numeric')));
$submenu2 ? $class = ' ts-has-child' : $class = '' ;
if($submenu2) {
$class = ' ts-has-child';
$submenu2 = array_intersect_key($submenu2, array_flip(array_filter(array_keys($submenu2), 'is_numeric')));
} else $class = '' ;
$link['link_path'] == $item['path'] ? $active = ' active' : $active = '' ;
$link['external'] ? $url = $link['link_path'] : $url = $bp.$link['link_path'];
$menu .= '<li class="nav-item'.$class.'">';
$menu .= '<a class="nav-link'.$active.'" href="'.$url.'">'.$submenu_link['#title'];
$active ? $menu .= '<span class="sr-only">(current)</span>' : NULL ;
$menu .= '</a>';
if($submenu2) {
$menu .= '<ul class="ts-child">';
foreach ($submenu2 as $submenu2_link) {
$link = $submenu2_link['#original_link'];
$link['link_path'] == $item['path'] ? $active = ' active' : $active = '' ;
$menu .= '<li class="nav-item">';
$menu .= '<a class="nav-link'.$active.'" href="'.$bp.$link['link_path'].'">'.$submenu2_link['#title'];
$active ? $menu .= '<span class="sr-only">(current)</span>' : NULL ;
$menu .= '</a></li>';
}
$menu .= '</ul>';
}
$menu .= '</li>';
}
$menu .= '</ul>';
}
$menu .= '</li>';
}
$menu .= '</ul>';
return $menu;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question