0

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?

2 Answers2

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