Can I use Drupal api to get a list of users?

I would like to get a list of all users who have been assigned a specific role. I could write my own SQL, but I would like to use api as much as possible.

+6
php drupal
source share
8 answers

As a rule, there are no Drupal API functions for this task (pulling objects that meet certain criteria). It tends to focus on the CRUD functions of a single object in the API; everything else depends on the skills of the SQL developer.

The Views module allows you to create lists of users filtered by role, resolution, etc., but you can easily overdo it.

+5
source share

You can use entity_load to get an array of users. Here is an example that will create a list of all letters for admin users (used to send notifications)

<?php $users = entity_load('user'); $emails = ''; foreach($users as $user) { if (array_key_exists(3, $user->roles)) { if (strlen($emails) > 0) { $emails .= ' ' . $user->mail; } else { $emails = $user->mail; } } } ?> 
+9
source share

SQL that worked for me:

  $ users = "";
 $ result = db_query ('SELECT users.name AS users_name FROM users users
       INNER JOIN users_roles users_roles ON users.uid = users_roles.uid
       WHERE users_roles.rid = 4 ');

 while ($ existing_user = db_fetch_object ($ result)) {
       if ($ users! = "") $ users. = ",";
       $ users. = $ existing_user-> users_name;  // or do whatever
 }
 echo $ users;

Remember that this is for Drupal 6, and I'm not sure about the performance of this for large user bases. Not sure about Drupal 7.

+2
source share

One simple option is to use the views to create SQL (displayed below the view of it when you click the preview button) for you, and then use the Drupal SQL abstraction layer to get the results you need if you need to access the source data, and Do not display View.

It will look something like this:

 $result = db_query('SELECT users.uid AS uid, users.mail AS users_mail, users.name AS users_name FROM users users'); while ($existing_user = db_fetch_object($result)) { print_r($existing_user); // or do whatever } 

Just add more fields to the view to get the full request.

+1
source share

I don’t know which API can help gather users by role. Here you can find the link: http://drupal.org/node/82002 . Although SO is an awesome service, I suggest using drupal.org or #drupal channel on irc.freenode.org for Drupal related issues.

Ps .: sometimes SQL is not that evil :)

0
source share

In Drupal 7, using EntityFieldQuery is correct.

0
source share

The following works in Drupal 8 (in this example, load all users with the administrator role)

 \Drupal::service('entity_type.manager') ->getStorage('user') ->loadByProperties(['roles' => 'administrator']); 

will return a list of user objects.

To get the uid list instead, query the object field:

 $query = \Drupal::service('entity_type.manager')->getStorage('user')->getQuery(); $query->condition('roles', 'administrator'); $query->execute(); 
0
source share

In Drupal 8:

 $users = \Drupal\user\Entity\User::loadMultiple(); 

And if you want to get, for example, only administrators:

 $admins = []; foreach ($users as $user) { if ($user->hasRole('administrator')) { $admins[] = $user; } } 
0
source share

All Articles