protected function UserRoleRemove::doExecute in Rules 8.3
Remove role from a user.
Parameters
\Drupal\user\UserInterface $user: User object the roles should be removed from.
\Drupal\user\RoleInterface[] $roles: Array of user roles.
Throws
\Drupal\rules\Exception\InvalidArgumentException
File
- src/
Plugin/ RulesAction/ UserRoleRemove.php, line 48
Class
- UserRoleRemove
- Provides a 'Remove user role' action.
Namespace
Drupal\rules\Plugin\RulesActionCode
protected function doExecute(UserInterface $user, array $roles) {
foreach ($roles as $role) {
// Check if user has role.
if ($user
->hasRole($role
->id())) {
// If you try to add anonymous or authenticated role to user, Drupal
// will throw an \InvalidArgumentException. Anonymous or authenticated
// role ID must not be assigned manually.
try {
$user
->removeRole($role
->id());
} catch (\InvalidArgumentException $e) {
throw new InvalidArgumentException($e
->getMessage());
}
// Set flag that indicates if the entity should be auto-saved later.
$this->saveLater = TRUE;
}
}
}