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 davidjguru/c7f1d5289dd1cc8844deebba65726a47 to your computer and use it in GitHub Desktop.
Save davidjguru/c7f1d5289dd1cc8844deebba65726a47 to your computer and use it in GitHub Desktop.
Drupal 8 || 9 - Getting data from user account in Drupal

Drupal 8 || 9 - Getting data from user account in Drupal

Some actions you can take inside a hook in .module files, using services for getting user data programmatically.

Author

Acknowledgments

This snippet was composed from my current position as Senior Drupal Developer at Digitalist Sweden, one of the biggest companies oriented to Open Source in Europe and specifically focused in Drupal.

// You can check if the user is authenticathed or not.
$is_authenticated = \Drupal::currentUser()->isAuthenticated();
$is_anonymous = \Drupal::currentUser()->isAnonymous();
// Or by doing:
$current_user = \Drupal::currentUser();
$current_user->isAnonymous(); // true or false
$current_user->isAuthenticated(); // true or false

// Get a timestamp of last login for the user.
$current_user->getLastAccessedTime();

// Get the current user id, only if is authenticated.
$uid = \Drupal::currentUser()->id();
// Load a specific user by id.
// Is a user entity.
$user = \Drupal\user\Entity\User::load($uid);
// Load the current user.
// Remember: is a user object not a user entity.
$current_user = \Drupal::currentUser();

// Check existing fields and get specific values from user's fields.
$existing_field = $user->hasField('mail');
$user_display_name = $current_user->getDisplayName();
$user_account_name = $current_user->getAccountName();
$user_name = $current_user->getUsername(); 
// Different ways for getting data from fields.
$user_mail = $user->get('mail')->getValue()[0]['value'];
$user_mail_copy = $user->mail->value;
$user_mail_another_copy = $current_user->getEmail();


// Get all the fields available in the user entity type.
$fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('user', 'user');
$extra_fields = \Drupal::service('entity_field.manager')->getExtraFields('user', 'user');
$base_fields = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions('user', 'user');
$existing_field = array_key_exists('association', $fields);

// Get an array of roles from a user.
$array_roles = $user->getRoles();
// Check if the user has a specific role. 
$existing_role = in_array('administrator', $array_roles);

See more info from the returned User Object, based in the AccountProxy Class for Drupal 9.3.x:
https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Session%21AccountProxy.php/class/AccountProxy/9.3.x And the User Entity Class from the Drupal core for Drupal 9.3.x:
https://api.drupal.org/api/drupal/core%21modules%21user%21src%21Entity%21User.php/class/User/9.3.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment