Previously, there were only explicit manipulators that are deliberately added during rendering of the menu link tree. However, sometimes modules may want to introduce additional manipulators. For example, a module may want to remove untranslated links from the menu link tree.
The menu.link_tree service now provides an API for modules to add contextual menu tree manipulators. The contextual manipulators are provided by other modules based on the context where the links are rendered. These are used for side effects that a module needs to introduce to other modules rendering menu link trees.
Note: It is important for each implementation to carefully consider in which context the manipulator should be applied. Whether the manipulator applies for the instance or not, is defined by the return value of \Drupal\Core\Menu\MenuLinkTreeContextualManipulatorInterface::applies method. It's recommended to use as specific condition for targeting the right instance as possible, to not cause hard to track, undesired side-effects.
Example for adding a new manipulator:
module_name.services.yml:
services:
module_name.menu_tree_contextual_manipulator:
class: Drupal\module_name\MenuLinkManipulator
tags:
- { name: menu_tree_contextual_manipulator }
Drupal\module_name\MenuLinkManipulator must implement \Drupal\Core\Menu\MenuLinkTreeContextualManipulatorInterface.