How to Fetch Users with Specific Roles in Drupal 10 – Beginner Friendly Guide

As a Drupal developer, sometimes you need to get a list of users with specific roles in Drupal. For example, you might want all users with the Editor role so that you can send them notifications, assign tasks, or show them in a block.

In Drupal 10, this is easy using the Entity Query API. You can quickly fetch users by their roles, which makes it simple to work with user-specific features in your custom modules or blocks.

What is Entity Query in Drupal 10?

The Entity Query API allows you to query entities like users, nodes, taxonomy terms, and filter them based on their fields or properties.

Unlike Drupal 7, in Drupal 10, user roles are stored as a field, so you can directly filter users by roles without any complicated joins.

Fetching Users with a Specific Role

Here’s a simple example to get all active users with the role editor:

<?php
use Drupal\user\Entity\User;

// Define the role you want to filter
$role = 'editor';

// Step 1: Create an entity query for users
$query = \Drupal::entityQuery('user')
  ->condition('status', 1) // Only active users
  ->condition('roles', $role); // Filter by role

// Step 2: Execute the query to get user IDs
$uids = $query->execute();

// Step 3: Load full user entities
$users = User::loadMultiple($uids);

// Step 4: Display usernames
foreach ($users as $user) {
  echo 'User: ' . $user->getAccountName() . '<br>';
}

?>

Explanation:

  1. status = 1 ensures we only get active users.
  2. roles filters users with the specified role.
  3. User::loadMultiple() loads full user objects.
  4. Loop through the users to display or use their information.

Fetching Users with Multiple Roles

If you want users with any of multiple roles, you can pass an array:

<?php
$roles = ['editor', 'author'];

$query = \Drupal::entityQuery('user')
  ->condition('status', 1)
  ->condition('roles', $roles, 'IN'); // Users having any of these roles

$uids = $query->execute();
$users = User::loadMultiple($uids);

foreach ($users as $user) {
  echo 'User: ' . $user->getAccountName() . '<br>';
}

?>

To get a list of all roles, use:

<?php
use Drupal\user\Entity\Role;

$all_roles = Role::loadMultiple();
foreach ($all_roles as $role) {
  echo $role->id() . ': ' . $role->label() . '<br>';
}

?>
  • You can combine conditions with email, creation date, or other fields to filter users further.
  • Always ensure roles are correctly spelled and exist in your site to avoid empty results.

Key Takeaways

  • In Drupal 10, roles are fields, so querying by role is simple.
  • Use Entity Query to filter users efficiently.
  • Load full user objects with User::loadMultiple() to access details.
  • Beginners can easily combine multiple filters to build custom lists or reports.

Conclusion

Fetching users by role in Drupal 10 is straightforward. By understanding the Entity Query API and the roles field, you can easily create custom functionality, blocks, or reports for users with specific roles.