Drupal 8: Alter a menu item title

Profile picture for user Phil Frilling
By Phil Frilling, 9 November, 2016
In order to alter a menu item that is provided by a module/system, you can use hook_preprocess_menu__MENUNAME() (Since hook_menu_alter() no longer exists). In my instance, I need to change the system provided 'Logout' link to read 'Log Out'. In order to do this I added the following to my THEMENAME.theme file:

/**
 * Implements hook_preprocess_menu().
 */
function MYTHEME_preprocess_menu__account(&$variables) {
  if (isset($variables['items']['user.logout']['title']) && $variables['items']['user.logout']['title'] == 'Logout') {
    $variables['items']['user.page']['title'] = 'Log Out';
  }
}

Deleting a menu item

Likewise, if you want to delete a menu item you can unset the item.

/**
 * Implements hook_preprocess_menu().
 */
function MYTHEME_preprocess_menu__account(&$variables) {
  // Remove a menu item if the user is logged in.
  if (isset($variables['items']['menu_link_content:b32ab12d-59ac-488d-a4ba-da0d70943a3f']) && \Drupal::currentUser()->isAuthenticated()) {
    unset($variables['items']['menu_link_content:b32ab12d-59ac-488d-a4ba-da0d70943a3f']);
  }
}