HOOK_menu_admin_per_menu_get_permissions_alter and menu-caching

Created on 10 March 2023, over 1 year ago
Updated 13 March 2023, over 1 year ago

[edit] sorry, but my analytics was wrong - here the correct idea. Its not a bug, but I have a "Feature"-Request.

Problem/Motivation

Wrong menu-Links can be shown if HOOK_menu_admin_per_menu_get_permissions_alter is used.

Steps to reproduce

Use HOOK_menu_links_discovered_alter() to add a link to a menu "my_menu"
Use HOOK_menu_admin_per_menu_get_permissions_alter to give permission to this menu to userA - not to userB
userA and userB must have same roles !!!
Login with userA and userB in different browsers
Clear cache and reload for userA: he will see the Link to "my_menu" - reload userB: he sees this link too !!!
Clear cache and reload for userB: now he wont see the link to "my_menu"- reload for userA: he also cant see.

Proposed resolution

This showcase has menus which differ PER USER. So adding a cache-context has helped in my case.
It would be fine to read some information about this problem in menu_admin_per_menu.api.php.

As example here my preprocess-hook:

function my_module_preprocess_menu(&$variables, $hook) {
  // Add user as cache-context
  $variables['#cache'] = ['contexts' => ['user']];
}

Remaining tasks

Write about this issue in menu_admin_per_menu.api.php

Feature request
Status

Active

Version

1.5

Component

Code

Created by

🇩🇪Germany kle

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

Comments & Activities

Production build 0.71.5 2024