Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 318 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is it possible to use the Drupal api to get a list of users?

#1
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.
Reply

#2
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]

Reply

#3
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.
Reply

#4
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]

Reply

#5
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.
Reply

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

Reply

#7
In Drupal 7, use EntityFieldQuery is the proper way.
Reply

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

Reply

#9
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;
}
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through