Hide register link for administrator role users in Drupal 8

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

But with this approach, you might face a that this menu item will be visible to users with an "Administrator" role. There are two solutions to this problem:

Solution 1: Use the Menu item visibility module

With the Menu Item Visibility module, you can give specific permission to roles that can access menu items. This can hide the "Register" link for authenticated users.

Solution 2: Alter menu with hook_preprocess_menu()

If using a module is overkill then simply use hook_preprocess_menu() to alter the menu items which will be visible to the user.

function mymodule_user_preprocess_menu(&$variables) {
  // Hide a particular link for the authenticated user.
  $items = $variables['items'];
  foreach ($items as $key => &$item) {
    // Hide register link.
    if (!$item['url']->isExternal() 
      && $item['url']->getRouteName() == 'user.register' 
      && \Drupal::currentUser()->isAuthenticated()) {
      unset($variables['items'][$key]);
    }
  }
}