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(&$variables) {
if ($variables['elements']['#id'] == 'left_navigation' && !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->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()->getStorage('menu_link_content')->loadByProperties(array('uuid' => $parent)));
$variables['top_parent'] = [
'title' => $menu_content->getTitle(),
'url' => $menu_content->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?