Drupal 8 userful functions in users

$role_names = user_role_names();

It returns array of all the user roles like anonymous,authenticated,admin.

To check whether a username exists or email exists then drupal 8 provide the below functions to check

$username = “Mahtab”

$hasUsername = user_load_by_name($username);

the $hasUsername will return true or false, the same we can check for email by

$hasEmail = user_load_by_mail($email);

To get the current user id

$user = \Drupal::currentUser();
$uid = $user->id();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s