0Day Forums
Is it possible to use the Drupal api to get a list of users? - Printable Version

+- 0Day Forums (https://zeroday.vip)
+-- Forum: Coding (https://zeroday.vip/Forum-Coding)
+--- Forum: CMS (https://zeroday.vip/Forum-CMS)
+---- Forum: Drupal (https://zeroday.vip/Forum-Drupal)
+---- Thread: Is it possible to use the Drupal api to get a list of users? (/Thread-Is-it-possible-to-use-the-Drupal-api-to-get-a-list-of-users)



Is it possible to use the Drupal api to get a list of users? - prebendaryship516209 - 07-27-2023

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


RE: Is it possible to use the Drupal api to get a list of users? - surfable795338 - 07-27-2023

I don't know about any API can help to collect users by role. Here you are some link you can get something: [

[To see links please register here]

][1].
Although SO is an awesome service I suggest using drupal.org or #drupal channel on irc.freenode.org for Drupal related questions.

Ps.: sometimes SQL is not that evil:)


[1]:

[To see links please register here]




RE: Is it possible to use the Drupal api to get a list of users? - outproducing173382 - 07-27-2023

There are generally no Drupal API functions for this sort of task (pulling up entities that match certain criteria). It tends to focus on single-entity CRUD functions in the API; everything else is up to a developer's own SQL skills.

The Views module allows you to build lists of users filtered by role, permission, etc -- but it could easily be overkill.


RE: Is it possible to use the Drupal api to get a list of users? - havocs956339 - 07-27-2023

One easy option is to use Views to generate the SQL (appears below the view its self when you press the preview button) for you and then use the [Drupal SQL abstraction layer][1] to get the results you need if you need to get access to the raw data rather than display a View.

It'd look a bit 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 complete query.

[1]:

[To see links please register here]




RE: Is it possible to use the Drupal api to get a list of users? - luxuriancy707191 - 07-27-2023

The SQL that worked for me:

<pre>
$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;
</pre>

Keep in mind this is for Drupal 6 and I'm not sure about the performance of this for large user bases. Not sure about Drupal 7.


RE: Is it possible to use the Drupal api to get a list of users? - acquittal621 - 07-27-2023

You can use entity_load to get array of users. Here is the sample that will create list of all emails for admin users (used to send notification)

<?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;
}
}
}
?>




RE: Is it possible to use the Drupal api to get a list of users? - erysipeloid363 - 07-27-2023

In Drupal 7, use EntityFieldQuery is the proper way.


RE: Is it possible to use the Drupal api to get a list of users? - gibbosity529317 - 07-27-2023

In Drupal 8 the following works (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 entities.

To get a list of `uid`s instead, an entity field query:

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




RE: Is it possible to use the Drupal api to get a list of users? - design781 - 07-27-2023

In Drupal 8:

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

And if you want to get e.g. only admins:

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