logo

Method to Get Username Programmatically Drupal 8

26
August

Method to Get Username Programmatically Drupal 8
By: Anonymous | Published On: Fri, 08/26/2022 - 19:46

There are several ways to get the user's name programmatically.

$uid = 1;	// For example I have get detail of user ID 1
$account = \Drupal\user\Entity\User::load($uid); // pass your uid

Method 1

drupal_set_message($account->name->value);  

Method 2

drupal_set_message($account->get("name")->value); 

Method 3

drupal_set_message($account->getUsername());

Method 4

drupal_set_message($account->name->getValue()[0][value]);

Need Help ?