How to hide the menu items for anonymous users in drupal 8? Since the hook_translated_link_alter has been deprecated , which hooks can be used to perform the task?
Asked
Active
Viewed 2,689 times
2 Answers
2
You need to override/extends the DefaultMenuLinkTreeManipulators class to provide the role as access on menu items.
Namespace is Drupal\Core\Menu\DefaultMenuLinkTreeManipulators;
OR
You can use menu per role module to set the role based access on menu items

Vernit Gupta
- 339
- 1
- 10
1
Your menu items will have a certain route bound to them. In your .routing.yml file you can set permissions on your route which will be carried on to the link.
Quick example: Lets say we create a link to the following route:
profile.page:
path: 'profile'
defaults:
_controller: '\Drupal\profile\Controller\ProfileController::profile'
_title: 'Profile'
requirements:
_permission: 'access profile content'
Here, 'access profile content' is a custom permission but it doesn't really matter what permission it is. Your link will also get this permission and Drupal will hide your link when the user doesn't have permission to access the content behind it.
Hope this helps

VJamie
- 616
- 3
- 14
-
Any custom module with stable release for the same? – Prerit Mohan Dec 06 '16 at 07:54
-
This is a good answer, but it only works if the link corresponds to a route. I would be interested to see a solution that works for an external link. – AdamS Jun 11 '17 at 16:12