Alter user's entity label method By default the label() method of the user entity returns the username. So everywhere on the website where user references are used, the username is displayed.

I need to alter this method so everywhere the user name is displayed it will use my custom function. So instead of "custom_user_name" will display "Firstname Lastname" for example.

Any idea on how to achieve this? Thanks.
I'm not sure if there's a shortcut for that, but if not the steps are still quite minimal:


Sub-class the \Drupal\user\Entity\User class in a custom module, and override its label() method:

mymodule/src/Entity/User.php

namespace Drupal\mymodule\Entity;

use Drupal\user\Entity\User as CoreUser;

class User extends CoreUser {

  public function label() {
    return 'something new';
  }

}

Implement hook_entity_type_alter() to set the class for the User entity type:

function MYMODULE_entity_type_alter(&amp;$entity_types) {
  $entity_types['user']-&gt;setClass(\Drupal\mymodule\Entity\User::class);
}

Rebuild the cache