How to display a block with menu child items programmatically? I need to create block with a list of links menu child items (only first level) of a given menu like this:


Item 1
Item 2
Item 3


With this code I can have the menu items but I don't know how to process each element to obtain the menu item title and the menu item url.

$menu_name = 'main';
$menu_tree = \Drupal::menuTree();
$parameters = $menu_tree-&gt;getCurrentRouteMenuTreeParameters($menu_name);
$parameters-&gt;setMinDepth(0);

$tree = $menu_tree-&gt;load($menu_name, $parameters);
$manipulators = array(
  array('callable' =&gt; 'menu.default_tree_manipulators:checkAccess'),
  array('callable' =&gt; 'menu.default_tree_manipulators:generateIndexAndSort'),
);
$tree = $menu_tree-&gt;transform($tree, $manipulators);
I found the answer based in this question: How to turn a menu tree into an associative array?

You can use each array element as a item of MenuLinkInterface

To see others parameters check the class MenuTreeParameters

&lt;?php
/**
 * @file
 * Contains \Drupal\YOUR_MODULE\Plugin\Block\SectionsFooter.
 */

namespace Drupal\YOUR_MODULE\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Link;

/**
 * Provides a 'SectionsFooter' block.
 *
 * @Block(
 *   id = "sections_footer",
 *   admin_label = @Translation("Sections"),
 *   category = @Translation("YOUR_MODULE")
 * )
 */
class SectionsFooter extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {
    $menu_name = 'main';
    $menu_tree = \Drupal::menuTree();
    $parameters = $menu_tree-&gt;getCurrentRouteMenuTreeParameters($menu_name);
    $parameters-&gt;setMinDepth(0);
    //Delete comments to have only enabled links
    //$parameters-&gt;onlyEnabledLinks();

    $tree = $menu_tree-&gt;load($menu_name, $parameters);
    $manipulators = array(
      array('callable' =&gt; 'menu.default_tree_manipulators:checkAccess'),
      array('callable' =&gt; 'menu.default_tree_manipulators:generateIndexAndSort'),
    );
    $tree = $menu_tree-&gt;transform($tree, $manipulators);
    $list = [];

    foreach ($tree as $item) {
      $title = $item-&gt;link-&gt;getTitle();
      $url = $item-&gt;link-&gt;getUrlObject();
      $list[] = Link::fromTextAndUrl($title, $url);
    }

    $output['sections'] = array(
    '#theme' =&gt; 'item_list',
    '#items' =&gt; $list,
    );
    return $output;
  }
}


References:

Menu tree building is now a service