22
April
Get user role of current loggedin user
$user_roles = \Drupal::currentUser()->getRoles();
will return an array like:
Array
(
[0] => authenticated
[1] => administrator
[2] => some_other_role
)
Get user detail of curent loggedin user
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id()); user_delete(uid); $user_roles = \Drupal::currentUser()->getRoles(); $user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id()); $uid = $user->get('uid')->value;
You can check user is visitor or logged member by using User::isAnonymous() method. Example is below.
if (\Drupal::currentUser()->isAnonymous()) {
// Anonymous user...
}
Get user value on user load function
$user = \Drupal\user\Entity\User::load($uid); $usermail = $user->getEmail(); $username = $user->getUsername();
© 2023.ZedAngle. All Rights Reserved.