Skip to content

Commit

Permalink
Fix call to user_role_names()
Browse files Browse the repository at this point in the history
  • Loading branch information
Berdir committed Jul 29, 2024
1 parent 507fb24 commit 1d2bf22
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/Drupal/Driver/Cores/Drupal8.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,18 +262,19 @@ public function userDelete(\stdClass $user) {
*/
public function userAddRole(\stdClass $user, $role_name) {
// Allow both machine and human role names.
$roles = user_role_names();
$id = array_search($role_name, $roles);
if (FALSE !== $id) {
$role_name = $id;
}

if (!$role = Role::load($role_name)) {
$query = \Drupal::entityQuery('user_role');
$conditions = $query->orConditionGroup()
->condition('id', $role_name)
->condition('label', $role_name);
$rids = $query
->condition($conditions)
->execute();
if (!$rids) {
throw new \RuntimeException(sprintf('No role "%s" exists.', $role_name));
}

$account = User::load($user->uid);
$account->addRole($role->id());
$account->addRole(reset($rids));
$account->save();
}

Expand Down

0 comments on commit 1d2bf22

Please sign in to comment.