logo

User command drupal 8

admin
16 Dec 2021

Following code can be used to get user picture or image by loading user data in Drupal 8

$user = \Drupal\user\Entity\User::load($uid);
$imgpath = $user->get('user_picture')->entity->uri->value;
if(!empty($imgpath)){
	$user_pic = file_create_url($picurl);
}
admin
22 Apr 2020

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