How to load the immediate parent link in a preprocess function? I am using menu block to show a section of the navigation. I have a requirement to show the parent link at the top, but that would not gel with the menu configuration.

Therefore, I should be able to solve this by taking the first link in the list, and loading the parent. Then, I want to add that as a variable, and render it in my twig file above the loop that would output the entire link tree. However, I am coming up short on how to do this effectively.

So far, I have:

function mytheme_preprocess_block__menu_block(&amp;$variables) {
  if ($variables['elements']['#id'] == 'left_navigation' &amp;&amp; !empty($variables['elements']['content']['#items'])) {
    $children = $variables['elements']['content']['#items'];
    $top_child = key($children);

    $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
    $menu_link_parents = $menu_link_manager-&gt;getParentIds($top_child);

    // the one we want should be the second one. the first one is the one in the main navigation across the top.
    $parent = end($menu_link_parents);
    $parent = str_replace('menu_link_content:', '' , $parent);
    $menu_content = current(\Drupal::entityTypeManager()-&gt;getStorage('menu_link_content')-&gt;loadByProperties(array('uuid' =&gt; $parent)));

    $variables['top_parent'] = [
      'title' =&gt; $menu_content-&gt;getTitle(),
      'url' =&gt; $menu_content-&gt;getUrlObject()
    ];
  }
}


I am lost at the point I retrieve the parent. Is there a way to get the UUID instead of the dirty str_replace? Also, how can I take that UUID, and create an object similar to menu items that would be rendered?
I think you can do it with the 'plugin.manager.menu.link' service:

$link = $this-&gt;menuActiveTrail-&gt;getActiveLink();

if(!empty($link-&gt;getParent())) {
  $parent_link = Drupal::service('plugin.manager.menu.link')-&gt;createInstance($link-&gt;getParent());
  $menu_item = MenuLinkContent::load($parent_link-&gt;getMetaData()['entity_id']);
}


This way you don't have to replace the prefix.