diff --git a/src/Plugin/RulesAction/UserRoleAdd.php b/src/Plugin/RulesAction/UserRoleAdd.php index 7cfc0a1f..4a34e654 100644 --- a/src/Plugin/RulesAction/UserRoleAdd.php +++ b/src/Plugin/RulesAction/UserRoleAdd.php @@ -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. * @@ -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