Overriding Drupal Core user account menu ‘Log in/Log out’ link

Pasan Gamage
3 min readApr 17, 2024

Intro

If you need to update the core user account menu’s Log in menu link into read something else such as ‘Sign in’ and for Log out to ‘Sign Out’ here’s what you need to do.

This can be done via JS, but I’m using an approach to override the Drupal core behaviour directly.

Drupal core default — anonymous user
After enabling the module — authenticated user

How it’s in Core

The Drupal core uses 2 files to set the text to ‘Log in’

  1. web/core/modules/user/user.links.menu.yml
user.logout:
weight: 10
menu_name: account
class: Drupal\user\Plugin\Menu\LoginLogoutMenuLink

2. web/core/modules/user/src/Plugin/Menu/LoginLogoutMenuLink.php

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

The Code

All we need to do is to override these two files using a custom module. Let’s call our module playground_custom_login

And in our custom module, we can add a playground_custom_login.links.menu.yml file.

playground_custom_login.logout:
weight: 10
menu_name: account
class: Drupal\playground_custom_login\Plugin\Menu\CustomLoginLogoutMenuLink

And follow the same directory structure as in core to place the CustomLoginLogoutMenuLink.php file at web/modules/custom/playground_custom_login/src/Plugin/Menu/CustomLoginLogoutMenuLink.php

<?php

namespace Drupal\playground_custom_login\Plugin\Menu;

use Drupal\Core\Menu\StaticMenuLinkOverridesInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\Plugin\Menu\LoginLogoutMenuLink;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Extension of menu link that shows "Sign in" or "Sign out" as appropriate.
*/
class CustomLoginLogoutMenuLink extends LoginLogoutMenuLink {

/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;

/**
* Constructs a new LoginLogoutMenuLink.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Menu\StaticMenuLinkOverridesInterface $static_override
* The static override storage.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, StaticMenuLinkOverridesInterface $static_override, AccountInterface $current_user) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $static_override, $current_user);
$this->currentUser = $current_user;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('menu_link.static.overrides'),
$container->get('current_user')
);
}

/**
* {@inheritdoc}
*/
public function getTitle() {
if ($this->currentUser->isAuthenticated()) {
return $this->t('Sign out');
}
else {
return $this->t('Sign in');
}
}

}

Lastly, you will need to disable the core default Login menu link from the User account menu.

You can check out the code here. Make sure to enable the module and clear cache to test it out ;)

--

--