Allow MenuLinkTree manipulators to be altered

Created on 30 October 2019, about 5 years ago
Updated 19 September 2024, 3 months ago

Problem/Motivation

Currently, across core and contrib there are a number of modules and issues that attempt to override the menu link tree. However lacking a core way to alter this behavior these overrides conflict with each other. The issue summary of #2854013 Allow SystemMenuBlock tree manipulators to be altered Needs work outlines some of these existing conflicts.

<!--break-->

A quick look through the issue queue found these additional use cases:

  • Add classes and attributes
  • Add conditional query parameters to a link (ex. ?destination=<current_route>)
  • Show links regardless of user access.
  • Hide links to unpublished entities regardless of user access.
  • Hide links without a translation.

Core can't cover all of these possible combinations but we should facilitate contrib and custom modules to cover them.

Proposed resolution

Dispatch an event within \Drupal\Core\Menu\MenuLinkTree::transform() that modules can subscribe to and alter menu link manipulators.

This solution is similar to #2854013 Allow SystemMenuBlock tree manipulators to be altered Needs work except that it applies to any menu link tree regardless of how it is built (ex. via a block or theme preprocess). This decouples the building of a menu link tree from how it is rendered.

API changes

  • Dispatch an event to alter the array of manipulators in MenuLinkTree::transform()
  • Allow adding cache metadata to menu links by implementing \Drupal\Core\Cache\RefinableCacheableDependencyTrait.
Feature request
Status

Needs work

Version

11.0 🔥

Component
Menu system 

Last updated about 19 hours ago

Created by

🇺🇸United States malcolm_p

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