When loading menu tree, sort subtree by weight

Created on 22 November 2018, about 6 years ago
Updated 18 September 2024, 3 months ago

When loading a menu, for example using:

$tree = \Drupal::menuTree()->load('menu', new MenuTreeParameters());

the subtrees is not necessarily sorted by weight. For the subtrees to be sorted by weight we need to do something like:


$tree = \Drupal::menuTree()->load('menu', new MenuTreeParameters());

foreach ($tree as $key => $item) {
  usort($tree[$key]['subtree'], function($a, $b) {
    if ($a->link->getWeight() == $b->link->getWeight()) {
      return 0;
    }
    return ($a->link->getWeight() < $b->link->getWeight()) ? -1 : 1;
  });
}

This is unwieldy for developers, and the problem is compounded by the fact that the subtree _can_ be sorted, but is not necessarily, so if a subtree is sorted during development, it is easy to overlook this requirement.

Suggestion: sort the subtree by weight automatically.

✨ Feature request
Status

Active

Version

11.0 πŸ”₯

Component
Menu systemΒ  β†’

Last updated 1 day ago

Created by

πŸ‡¨πŸ‡¦Canada alberto56

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024