0

I'm a bit lost at finding the username based on the uid. I get the uid from the address using:

  $current_path = \Drupal::service('path.current')->getPath();
  $urluid = (explode("/",$current_path));

  $us = \Drupal::entityTypeManager()->getStorage('user')
  ->loadByProperties(['uid' => $urluid[2]]);

  kint($us);

returns an array of data where i do see the uid, but not the username for that uid. Name:

'name' => array(1)
*DEPTH TOO GREAT*

returns NULL:

kint($us->name);

I'd like to return the UID->Name as the title on the ROLES and EDIT screens for the user. Exactly how it is returned for VIEW (example dvweb is: /user/10): d

EDIT:::

Was able to get it with:

  $u = Drupal\user\Entity\User::load($urluid[2]);
    kint($u->getUsername());
1

It's better to use the route instead of the path, where you'll find the already loaded user entity:

if (\Drupal::routeMatch()->getRouteName() == 'entity.user.canonical') {
  $user = \Drupal::routeMatch()->getParameter('user');
  $username = $user->getUsername();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.