Problem/Motivation

MenuLinkTreeInterface::load() does not do any sorting or access checks, as might be expected. This should be better documented.

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.

Proposed resolution

Improve documentation.

Remaining tasks

Finalize documentation improvement.

User interface changes

None.

Introduced terminology

None.

API changes

None.

Data model changes

None.

Release notes snippet

CommentFileSizeAuthor
#17 3015631-nr-bot.txt797 bytesneeds-review-queue-bot

Issue fork drupal-3015631

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

alberto56 created an issue. See original summary.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

batkor’s picture

Hi!
thing close this issues
method transform

$manipulators = [
  ['callable' => 'menu.default_tree_manipulators:checkNodeAccess'],
  ['callable' => 'menu.default_tree_manipulators:checkAccess'],
  ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
];
$tree = $menu_tree_service->transform($tree, $manipulators);

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

imclean’s picture

Status: Active » Closed (works as designed)

#3 is the solution.

liam morland’s picture

Version: 9.5.x-dev » 11.0.x-dev
Status: Closed (works as designed) » Active

This should be better documented.

liam morland’s picture

Version: 11.0.x-dev » 11.x-dev
Status: Active » Needs review
liam morland’s picture

Title: When loading menu tree, sort subtree by weight » Improve documentation of MenuLinkTreeInterface
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update

For good practice should have a complete issue summary, using the standard template.

liam morland’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs issue summary update
needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new797 bytes

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

liam morland’s picture

Status: Needs work » Needs review

Rebased. Spell check passes.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Believe this is an improvement.

amateescu’s picture

Status: Reviewed & tested by the community » Needs work

Added a comment to the MR.

liam morland’s picture

Status: Needs work » Needs review

Fixed

amateescu’s picture

Status: Needs review » Reviewed & tested by the community

Thanks!

quietone’s picture

Update credit

  • larowlan committed 7ae414dd on 11.1.x
    Issue #3015631 by liam morland, smustgrave, alberto56, amateescu, batkor...

  • larowlan committed 2d552b3d on 11.x
    Issue #3015631 by liam morland, smustgrave, alberto56, amateescu, batkor...
larowlan’s picture

Version: 11.x-dev » 11.1.x-dev
Category: Feature request » Task
Status: Reviewed & tested by the community » Fixed

Committed to 11.x and backported to 11.1.x.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.