4

I have a certain number of main menus which need to showcased based on logged in user role. for example user with the role, A can sees few menus and user with the role B, sees another set of few menus.

2

This can be achieved using the routes to manage the permissions. Take the following example:

mymodule.routing.yml

mymodule.home:
  path: '/mymodule'
  defaults:
    _controller: '\Drupal\mymodule\Controller\MyModuleController::overview'
    _title: 'My Module'
  requirements:
    _role: 'authenticated user'

mymodule.settings:
 path: '/admin/mymodule'
 defaults:
   _form: 'Drupal\mymodule\Form\MyModuleSettingsForm'
   _title: 'Settings'
 requirements:
   _permission: 'administer mymodule'

mymodule.view:
 path: '/mymodule/view'
 defaults:
   _controller: '\Drupal\mymodule\Controller\MyModuleController::view'
   _title: 'View'
 requirements:
   _role: 'authenticated user'

mymodule.edit:
 path: '/mymodule/edit'
 defaults:
   _form: 'Drupal\mymodule\Form\MyModuleEditForm'
   _title: 'Edit'
 requirements:
   _permission: 'edit mymodule'

mymodule.links.menu.yml

mymodule.home:
  title: 'My Module'
  parent: main
  route_name: mymodule.home
  weight: 10

mymodule.settings:
  title: 'Settings'
  parent: mymodule.home
  route_name: mymodule.settings
  weight: 10

mymodule.view:
  title: 'View'
  parent: mymodule.home
  route_name: mymodule.view
  weight: 10

mymodule.edit:
  title: 'Edit'
  parent: mymodule.home
  route_name: mymodule.edit
  weight: 10

With this approach you can display the same menu to all users, but because the links take the permissions from the routes, only users with the administer mymodule permission will be able to see the Settings link for example. You can use roles to manage it as well by using _role: '$rid'.

  • You can also combine roles with , and +. One means or and the other and. I don't recall which is which. – gogowitsch May 15 at 13:16
1

All you gotta do is configure your menu block

enter image description here

Then under Visibility > Roles, check mark ☑ the roles that you want to be able to view this block menu.

enter image description here

  • @Nossweat to add on more, It needs to be achieved for each menu item in the main menu. Something similar to menu_per_role in D7. – Sidhees Mar 1 '17 at 10:22
  • @Sidhees Well, that module isn't going to port itself. I don't think there is anything similar out there for D8, so better start rolling-up the sleeves. Your need is quite a specific and picky need if you ask me. – No Sssweat Mar 1 '17 at 10:47
  • @NoSssweat, I agree, normally no need for this, because drupal checks by default the permissions. If a role has no access to the menu item it is not displayed. – 4k4 Mar 1 '17 at 12:32
  • This will still not solve the problem if one wants to make the link to "/user" depending on the role, either show the menu item or not, and label the menu item "role 1 thing" and another role "role 2 thing" – Raf A. Oct 1 '18 at 5:57
0

Just use Menu Item Role Access. It makes it much easier than writing code.

0

In Drupal 8, you can also use the following code in your funky.module file for a programmatic approach. The approach below allows to have access rules independent of menu visibility:

/**
 * Implements hook_preprocess_menu().
 *
 * @param array[] $variables
 */
function funky_preprocess_menu(&$variables) {
  if (in_array('role_b', Drupal::currentUser()->getRoles())) {
    // Hide the menu item
    unset($variables['items']['the_key_you_used_in_your_menu_links_yml_file']);
  }
}

This assumes your module is called funky. It also assumes you have a funky.links.menu.yml file containing something similar to this:

the_key_you_used_in_your_menu_links_yml_file:
  title: 'Your menu caption'
  route_name: your_route
  menu_name: account
  cache_contexts:
    - user.roles

If you only distinguish if a user has a role or not, then you can further tighten the cache_context by that role. For the example above, user.roles:role_b would be appropriate. The documentation for cache contexts is here: https://www.drupal.org/docs/8/api/cache-api/cache-contexts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.