Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobertCastro/6e16945716f1cd3d2a611c1d8dd0c83a to your computer and use it in GitHub Desktop.
Save RobertCastro/6e16945716f1cd3d2a611c1d8dd0c83a to your computer and use it in GitHub Desktop.
Get current user in drupal 8
get current user in drupal 8
Description: In drupal 7 we get the current user object by defining global $user but in drupal 8 this quite different. If you want to get current user object then follow below code.
$current_user = \Drupal::currentUser();
$uid = $current_user->id();
It returns user id of current user.
$user_mail = $current_user->getEmail();
It returns user email id.
$user_display_name = $current_user->getDisplayName();
It returns user display name.
$user_account_name = $current_user->getAccountName()
It returns user account name.
$user_roles = $current_user->getRoles();
It returns array of current user has.
$current_user->isAnonymous();
It returns true or false.
$current_user->isAuthenticated();
It returns true or false.
$current_user->getLastAccessedTime();
It returns timestamp of last logged in time for this user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment