Get the menu items as a block

Last updated on 02/10/2016

In the cases when you need to display for example the "main-menu" in the Header and Footer and you don't want any other contrib module you can just create your own custom block:

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