What is the equivalent of hook_translated_menu_link_alter() to alter a menu link? I want to alter the menu link in Drupal 8. In Drupal 7 we can achieve this using hook_translated_menu_link_alter().

function module_translated_menu_link_alter(&amp;$item, $map) {
  global $user;

  if (!empty($item['access'])) {
    // Administrator will access all menu's.
    if (strpos(current_path(), 'admin/structure/menu/manage/' . $item['menu_name']) === 0 &amp;&amp; user_access('administer menu')) {
      return;
    }
    // If userid is equal to menu user id.
    if ($user-&gt;uid == 1) {
      $access = 'UNBLOCK';
      break;
    }
   // Block menu.
    else {
      $access = 'BLOCK';
    }
    // Access FALSE if Menu not for USER.
    if ($access == 'BLOCK') {
      $item['access'] = FALSE;
    }
  }
}


In Drupal 7 I am hiding/disabling menu based on some conditions like the user ID.

Similarly, I want to alter the menu link in Drupal 8. How can I achieve it?
HOOK_link_alter would be one D8 equivalent to hook_translated_menu_link_alter:

/**
 * Implements hook_link_alter().
 *
 * @inheritdoc
 */
function HOOK_link_alter(&amp;$variables) {
  $url = $variables['url'];
  // Check url is routed in Drupal.
  if ($url-&gt;isRouted()) {
    // Load the current user by user ID.
    $currentUser = User::load(\Drupal::currentUser()-&gt;id());
    // Check access.
    $urlAccess = $url-&gt;access();
    // Get url options.
    $urlOptions = $url-&gt;getOptions();
    // Your code here.
  }
}


See Drupal 8 class URL for more API info.

However, you could use Hook_preprocess_HOOK() as such:

/**
 * Implements hook_preprocess_hook().
 *
 * @inheritdoc
 */
function HOOK_preprocess_menu(&amp;$variables) {
  if (isset($variables['menu_name']) &amp;&amp; $variables['menu_name'] === 'menu_name') {
    foreach($variables['items'] as $key =&gt; $item) {
      $url = $item['url']-&gt;toString();
      if ($url == '/user') {
        // Remove menu item.
        unset($variables['items'][$key]);
      }
    }
  }
}


Just checking path above, but it shows you can hide a single menu item based on certain criteria.



For the access restrictions, depending on your needs, you might look at routing and permissions based on the entity instead of the menu item and let Drupal handle the menu item display.

For menu access by role look at this module: https://www.drupal.org/project/menu_item_role_access