I think you need to reset the collection first, as the ::loadLinksByRoute()
annotation states that it returns an array of MenuLinkInterfaces[]
.
See /core/lib/Drupal/Core/Menu/MenuLinkManagerInterface.php
:
/**
* Loads multiple plugin instances based on route.
*
* @param string $route_name
* The route name.
* @param array $route_parameters
* (optional) The route parameters. Defaults to an empty array.
* @param string $menu_name
* (optional) Restricts the found links to just those in the named menu.
*
* @return \Drupal\Core\Menu\MenuLinkInterface[]
* An array of instances keyed by plugin ID.
*/
public function loadLinksByRoute($route_name, array $route_parameters = [], $menu_name = NULL);
So do something like:
/** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
$menu_link_manager = \Drupal::service('plugin.manager.menu.link');
$menu_links = $menu_link_manager->loadLinksByRoute('entity.node.canonical', array('node' => $node_id));
// Reset collection by retrieving the first item.
$menu_link = reset($menu_links);
// Now call the parent method.
$parent = $menu_link->getParent();