Retrieve Drupal menu items
Drupal 8+
function getMenuTree(string $menuName): array {
$menuLinkTree = \Drupal::service('menu.link_tree');
$parameters = new \Drupal\Core\Menu\MenuTreeParameters();
$parameters->onlyEnabledLinks();
$tree = $menuLinkTree->load($menuName, $parameters);
$manipulators = [
['callable' => 'menu.default_tree_manipulators:checkNodeAccess'],
['callable' => 'menu.default_tree_manipulators:checkAccess'],
['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
];
return $menuLinkTree->transform($tree, $manipulators);
}
Drupal 7
/**
* Implements hook_block_info().
*/
function HOOK_block_info() {
$blocks['main_menu_footer'] = [
'info' => t('Main Menu footer'),
'cache' => DRUPAL_CACHE_PER_ROLE,
];
return $blocks;
}
/**
* Implements of hook_block_view().
*/
function HOOK_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'main_menu_footer':
$block['content'] = get_my_menu_block('main-menu');
break;
}
return $block;
}
/**
* Get the menu content.
*
* @param $menu_name
* Menu name.
*
* @return array
* Renderable array.
*/
function get_my_menu_block($menu_name) {
$menu = menu_tree_all_data($menu_name);
if (!empty($menu)) {
return menu_tree_output($menu);
}
}