logo

Get user list by role programmatically in Drupal8

22
April

Get user list by role programmatically in Drupal8
By: Anonymous | Published On: Wed, 04/22/2020 - 22:13

User this code before

use Drupal\user\Entity\User;

Thise code will be return user list of a given role (editor)

	$ids = \Drupal::entityQuery('user')
	->condition('status', 1)
	->condition('roles', 'editor')
	->execute();
	$users = User::loadMultiple($ids);
	foreach($users as $user){
		$username = $user->get('name')->value;
		$uid = $user->get('uid')->value;
		$userlist[$uid] = $username;
	} 

Need Help ?