3

How to change title of "Log in" link in account menu for anonymous users in Drupal 8? Thanks for any suggestions.

4
  • Are you talking about the one on the user/login page? And are you using a custom theme?
    – Amy
    Jun 25, 2018 at 16:15
  • about the link which leads there. It's in account menu for anonymous users.
    – Belial
    Jun 25, 2018 at 16:19
  • Changing the title of menu links should certainly have an affect when that link is rendered.
    – Kevin
    Jun 25, 2018 at 16:38
  • @Kevin that was my first instinct but it looks like the User module does not allow changing the title of that link.
    – Amy
    Jun 25, 2018 at 19:00

3 Answers 3

3

EDIT: mediaashley suggested a much simpler solution of creating a new menu, and using the menu block. Not sure why I didn't think of that.

If you're talking about the default link that appears in the 'User account menu' block. You'll notice that you see the message

This link is provided by the User module. The title and path cannot be edited.

There are several ways you can work around this. But I think the quickest and simplest would be to make your own custom blocks and put links to the account pages in there manually. First you'll need to remove the 'User Account Menu' block from the block layout. Then click on the 'Custom Block Layout' tab and click 'Add custom block'

enter image description here

From there you can make your own custom link like so

enter image description here

In this example I made a custom link for logged out users. Once you save it you'll have a series of options for conditions on whether the block should be displayed in the visibility section. Click on the role tab and check 'anonymous' if you want this block to show only for logged in users. And make sure you assign it to a region.

enter image description here

Once you've completed this you'll need to repeat the process except for 'Authenticated user's this time.

3
  • 1
    Why would you create a custom block with HTML instead of using a menu? A menu would give you a block, and then you would also have standard markup. (I mean create a second menu for anonymous users) Jun 26, 2018 at 14:57
  • @mediaashley oh you're right! I don't know why I didn't think of that!
    – Amy
    Jun 26, 2018 at 17:06
  • Brilliantly simple, no code solution. Thanks for this @Amy! Dec 13, 2023 at 22:55
10

As described by @Matt in the answer the menu link is provided by the user module and you can simply decide not to use it and build your own links.

The link in the user module is a dynamic link where a menu link plugin generates a different title depending on whether the user is authenticated or not.

To change the title you can replace this plugin class:

/**
 * Implements hook_menu_links_discovered_alter().
 */
function mymodule_menu_links_discovered_alter(&$links) {
  $links['user.logout']['class'] = 'Drupal\mymodule\Plugin\Menu\MyLoginLogoutMenuLink';
}

and override the method getTitle() with your own content:

mymodule/src/Plugin/Menu/MyLoginLogoutMenuLink.php:

<?php

namespace Drupal\mymodule\Plugin\Menu;

use Drupal\user\Plugin\Menu\LoginLogoutMenuLink;

class MyLoginLogoutMenuLink extends LoginLogoutMenuLink {

  public function getTitle() {
    if ($this->currentUser->isAuthenticated()) {
      return $this->t('My Log out');
    }
    else {
      return $this->t('My Log in');
    }
  }

}

A third option would be to change the title in an English to English translation, see How do I translate to English?

1

This was the quickest method for me; I had to run this code inside a custom module.

function mymodule_link_alter(&$variables) {
  if ($variables['text'] == 'My account') {
    $variables['text'] = t('Account');
  }
  if ($variables['text'] == 'Log out') {
    $variables['text'] = t('Sign out');
  }
}

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.