logo

User related methods in DRUPAL 8

22
April

User related methods in DRUPAL 8
By: Anonymous | Published On: Wed, 04/22/2020 - 22:13

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();

Need Help ?