I need to render just the submenu I need to print the current nodes submenu somewhere on the page. Inside my page.twig.html file I have this markup:

&lt;div id="children-menu"&gt;
  {{menu_children}}
&lt;/div&gt;


Inside my mytheme_preprocess_page() function I have this which rendering all the submenus.

function mytheme_preprocess_page(&amp;$vars) {
  if ($vars['node']-&gt;id()) {
    $menu_tree = \Drupal::menuTree();
    $menu_name = 'main';
    $parameters = $menu_tree-&gt;getCurrentRouteMenuTreeParameters($menu_name);
    $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'),
      array('callable' =&gt; 'toolbar_menu_navigation_links'),
    );
    $tree = $menu_tree-&gt;transform($tree,$manipulators);
    $subtrees = array();
    foreach ($tree as $element) {
      $link = $element-&gt;link;
      if ($element-&gt;subtree) {
        $subtree = $menu_tree-&gt;build($element-&gt;subtree);
        $output = drupal_render($subtree);
        array_push($subtrees, $output);
      }
    }
    //dpm($subtrees);
  }

}


I was looking into using manipulators, but that wasn't working. How can I render JUST the current nodes submenu using this function? Again, what I have now is rendering all the submenus and I need just the current one.
I managed to grab just the child submenu of the current page. Each subtree has a getRouteParameters() fn that contains the node ID that it links to. I simply test if that route param node id == the current pages node id.

function mytheme_preprocess_page(&amp;$vars) {
  if (isset($vars['node']) &amp;&amp; is_numeric($vars['node']-&gt;id())) {
    $nid = $vars['node']-&gt;id();
    $menu_tree = \Drupal::menuTree();
    $menu_name = 'main';
    $parameters = $menu_tree-&gt;getCurrentRouteMenuTreeParameters($menu_name);
    $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'),
      array('callable' =&gt; 'toolbar_menu_navigation_links'),
    );
    $tree = $menu_tree-&gt;transform($tree,$manipulators);
    $subtrees = array();
    foreach ($tree as $element) {
      $link = $element-&gt;link;
      if ($element-&gt;subtree) {
        $routeId = $element-&gt;link-&gt;getRouteParameters()['node'];
        if ($routeId == $nid) {
          $vars['menu_children'] = $menu_tree-&gt;build($element-&gt;subtree);
        }
      }
    }
  }
}