Show username to authenticated

Show username to authenticated user instead of just "My account".

/**
 * Implements hook_preprocess_hook().
 *
 * @inheritdoc
 */
function custom_fs_preprocess_menu(&$variables) {
  $current_user = \Drupal::currentUser();
  if ($current_user->isAuthenticated() && isset($variables['menu_name']) && $variables['menu_name'] === 'account') {
    foreach($variables['items'] as $key => $item) {
      $url = $item['url']->toString();
      if ($url == '/user') {
        $variables['items']['user.page']['title'] = $current_user->getDisplayName();
        $variables['items']['user.page']['attributes']->setAttribute('title', 'My account');
      }
    }
  }
}
Category: