1

I am pretty new to Drupal and wonder if there's any possibility to change the text of the user login / create an account and reset tab? I'd like to rename them to something like "sign in", "sign up" and "reset account".

I found no way through the backend UI. Neither in Tabs Block Content nor Menu structure.

Do I need to write a theme hook or a module to achieve that, or is there any functionality in the backend I've overlooked?

2
  • Does this answer your question? How do I change the name of the "view" node tab for a specific content type?
    – leymannx
    Commented Nov 12, 2020 at 11:00
  • Thanks for the reply @leymannx! From what I see, the linked solution aims towards the node view and node edit tabs for a certain content type. What I want to change are the tabs that are visible when a user is logged out, and goes to the login-page. So, I don't what content type to address in my case, as I just want to rename the default user login tabs titles. Commented Nov 12, 2020 at 11:16

2 Answers 2

4

As shown in the answer to the duplicate question you can use template_preprocess_menu_local_task from a custom theme or hook_preprocess_HOOK from a custom module to override the tabs.

If you want to do it right, you also set the page title and the HTML title to display the same strings. Maybe keep things DRY and get the $target_routes array from a small helper function.

First the tabs:

/**
 * Implements hook_preprocess_HOOK().
 */
function MYMODULE_preprocess_menu_local_task(&$variables) {

  $target_routes = [
    'user.login' => t('sign in'),
    'user.register' => t('sign up'),
    'user.pass' => t('reset account'),
  ];
  $current_route = \Drupal::routeMatch()->getRouteName();

  if (isset($target_routes[$current_route])) {
    /** @var \Drupal\Core\Url $url */
    $url = $variables['link']['#url'];
    $route_name = $url->getRouteName();
    $variables['link']['#title'] = $target_routes[$route_name];
  }
}

Then the page title:

/**
 * Implements hook_preprocess_HOOK().
 */
function MYMODULE_preprocess_page_title(&$variables) {

  $target_routes = [
    'user.login' => t('sign in'),
    'user.register' => t('sign up'),
    'user.pass' => t('reset account'),
  ];
  $current_route = \Drupal::routeMatch()->getRouteName();

  if (isset($target_routes[$current_route])) {
    $variables['title'] = $target_routes[$current_route];
  }
}

And finally the HTML title:

/**
 * Implements hook_preprocess_HOOK().
 */
function MYMODULE_preprocess_html(&$variables) {

  $target_routes = [
    'user.login' => t('sign in'),
    'user.register' => t('sign up'),
    'user.pass' => t('reset account'),
  ];
  $current_route = \Drupal::routeMatch()->getRouteName();

  if (isset($target_routes[$current_route])) {
    $variables['head_title']['title'] = $target_routes[$current_route];
  }
}
1
  • Wow! O.k. - that would have taken me ages - Thank you very much! I didn't know that it could be addressed through the target_routes and was confused, because your previously linked solution addressed the titles via the content type name. But now that makes sense to me. Thanks once again - big help! Commented Nov 12, 2020 at 11:44
-1

You could also make use of the Hook regarding the menu alter depending on your needs. A sample of it could be something like :

function MYMODULE_menu_alter(&$items) {
    $items['user/%user/track/navigation']['title'] = "Track Your Posts";
    $items['user/%user/view']['title'] = "View Profile";
    $items['user/%user_category/edit']['title'] = "Edit Profile";
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.