Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/Plugin/RulesAction/UserRoleAdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Drupal\rules\Core\RulesActionBase;
use Drupal\rules\Exception\InvalidArgumentException;
use Drupal\user\UserInterface;

use Drupal\user\Entity\Role;
/**
* Provides a 'Add user role' action.
*
Expand Down Expand Up @@ -47,7 +47,24 @@ class UserRoleAdd extends RulesActionBase {
* @throws \Drupal\rules\Exception\InvalidArgumentException
*/
protected function doExecute(UserInterface $account, array $roles) {
$drupal_roles = Role::loadMultiple();
foreach ($roles as $role) {
// If the role is not a role entity yet, load it if we can.
if (!$role instanceof Role) {
// Try to load the role by role id.
if ($r = Role::load($role)) {
$role = $r;
}
else {
// Try to load the role by role label.
foreach ($drupal_roles as $drupal_role) {
if ($drupal_role->get('label') == $role) {
$role = $drupal_role;
break;
}
}
}
}
// Skip adding the role to the user if they already have it.
if (!$account->hasRole($role->id())) {
// If you try to add anonymous or authenticated role to user, Drupal
Expand Down