I would like data from the users.data table displayed with every "User Name" however it is displayed.

As an example, I have this in hook_preprocess_page_title():

function hook_preprocess_page_title(&$variables) {

  if (\Drupal::routeMatch()->getRouteName() == 'entity.user.canonical') {

    $gUD = \Drupal::service('user.data');
    $routeUID = \Drupal::routeMatch()->getRawParameter('user');
    $gudValue = $gUD->get('module', $routeUID, 'name');

    if ($gudValue = $moduleValue)) {



The route entity.user.canonical allows me to reference the viewed user, and so I can use \Drupal::routeMatch()->getRawParameter('user') to get the non-current user ID. (it's not the route that counts, but my ability to reference the users.data table for the displayed user)

And this in hook_preprocess_username():

function hook_preprocess_username(&$variables) {
  $gUD = getUsersData();
  $gudValue = $gUD->get('module', $variables['uid'], 'name');

So what I am looking for is something like:

hook_preprocess_HOOK() {
  $UID = // From displayed user name.
  \Drupal::service('user.data')->get('module', $UID, 'name');

for each displayed user name.

The problem here: get user id from username or email without logging in, is that I don't know, and cannot manually enter the username value.

A couple of examples where the username is displayed on a page without using the username field:

  1. Views field Name (not linked to user, uses views field base)
  2. User type entity reference field (format display label, uses field base)

hook_preprocess_page_title() and hook_preprocess_username() are pretty straight-forward. And I believe I know where/when this template/hook will be used, but I am still having trouble defining a couple of other instances, and determining the best hook to alter the output as needed.

  • loadByProperties() queries the entire user table. Why should it be restricted to current user values?
    – 4uk4
    Mar 29, 2019 at 16:57
  • While trying to get the value of username from a preprocess function, without a route parameter of .user., I have not found a way to set that value to something other than the current user. Mar 29, 2019 at 17:05
  • I might have had a little progress, and it may better define what I'm asking. Once I set the format option of the user entity reference field to Author, instead of label on my content type, and now actually renders out the username field value, hook_preprocess_username(&$variables) does return $variables['name'] relative to the referenced user, not the current user. My error? Maybe, but now I'm unsure about `label format display. Mar 30, 2019 at 3:51
  • The label display is generic to all entities, it simply prints out what is defined as label for the entity, title for a node, or username for a user. When you render the user you have the normal theming options including preprocess hooks.
    – 4uk4
    Mar 30, 2019 at 8:27
  • It's not clear for which pages you are interested to get the user information. There are many pages for which the route name is not entity.user.canonical. Some of them could be user pages, some of them are for entities with a user entity reference field, and some of them aren't related to a user account in any way.
    – apaderno
    Mar 30, 2019 at 21:03

1 Answer 1


It sounds like you should start with what you don't want.

Drupal::currentUser()->id() will retrieve the current user's UID. From there, you can use the UserStorage::loadByProperties() method you referenced to pull the UID by user name. The last step is add code to check Drupal::currentUser()->id() doesn't match the UID found from UserStorage::loadByProperties()

  • What it's not clear from the question is for which pages the OP is trying to access the user information. There are many pages for which \Drupal::routeMatch()->getRouteName() doesn't return 'entity.user.canonical'. If the page is associated to a user account, then it's quite easy to get information about that user. Differently, there are many users who aren't the currently logged-in user. UserStorage::loadByProperties() doesn't allow to load all the users for which the user ID is not a given one.
    – apaderno
    Mar 30, 2019 at 20:53
  • I think in most cases this would have been the correct answer, but @kiamlaluno is right about the UserStorage::loadByProperties() in my case. My question is how to get that value in the context of the user shown/listed. I did not ask the question very well as the issue may be a little different. Mar 30, 2019 at 23:34

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.