Hide register link for administrator role users in Drupal 8

Posted on Friday, April 7, 2017

While working on Drupal 8 one of the issues you might face is you won't get "Register" link for anonymous user. And you need to create this menu item in the user menu.

But with this approach one issue, you might face is this menu item will be visible to users who has "Administrator" role. There are two solutions for this problem:

  1. Use Menu item visibility module

    With Menu Item Visibility module you can give specific permission to roles who can access menu items. This can be used to hide "Register" link for authenticated users.
  2. Alter menu with hook_preprocess_menu()

    If using a module is an overkill then simply use hook_preprocess_menu() to alter the menu items which will be visible to the user.
function alshaya_user_preprocess_menu(&$variables) {
// Hide particular link for the authenticated user.
$items $variables['items'];
  foreach (
$items as $key => &$item) {
// Hide register link.
if ($item['url']->getRouteName() == 'user.register' && \Drupal::currentUser()->isAuthenticated()) {



Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.