drupal-9

Set expandable a MenuLinkTreeElement programatically


When loading a menuTree a MenuLinkTreeElement instance's subtree is empty when this instance is not expanded. How to set its expanded property to 1 programmatically ?

Example:

$parameters = $menu_link_tree->getCurrentRouteMenuTreeParameters('admin');
$tree = $menu_link_tree->load('admin', $parameters);
$admin_link = reset($tree);
dpm($admin_link);

return

Drupal\Core\Menu\MenuLinkTreeElement {#242 ▼
  +link: Drupal\Core\Menu\MenuLinkDefault {#261 ▶}
  +subtree: []
  +depth: 1
  +hasChildren: true
  +inActiveTrail: false
  +access: null
  +options: []
}

Solution

  • This can be achieved with the following line:

    $parameters->expandedParents = [];