How to change title of "Log in" link in account menu for anonymous users in Drupal 8? Thanks for any suggestions.
-
Are you talking about the one on the user/login page? And are you using a custom theme?– AmyCommented Jun 25, 2018 at 16:15
-
about the link which leads there. It's in account menu for anonymous users.– BelialCommented Jun 25, 2018 at 16:19
-
Changing the title of menu links should certainly have an affect when that link is rendered.– KevinCommented 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.– AmyCommented Jun 25, 2018 at 19:00
3 Answers
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'
From there you can make your own custom link like so
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.
Once you've completed this you'll need to repeat the process except for 'Authenticated user's this time.
-
1Why 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) Commented Jun 26, 2018 at 14:57
-
@mediaashley oh you're right! I don't know why I didn't think of that!– AmyCommented Jun 26, 2018 at 17:06
-
Brilliantly simple, no code solution. Thanks for this @Amy! Commented Dec 13, 2023 at 22:55
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?
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');
}
}
-
Perfect - thank you. I had added My Account & Log Out to a separate menu and I can now translate these links. (Drupal 10)– Matt BCommented May 18 at 7:24