You are here

protected function RoleExpireRemoveExpireTime::doExecute in Role Expire 8

Same name and namespace in other branches
  1. 2.x src/Plugin/RulesAction/RoleExpireRemoveExpireTime.php \Drupal\role_expire\Plugin\RulesAction\RoleExpireRemoveExpireTime::doExecute()

Assign expire time for user and role.

Parameters

\Drupal\user\UserInterface $account: User object.

string $roles: Array of User roles ID.

Throws

\Drupal\rules\Exception\InvalidArgumentException

File

src/Plugin/RulesAction/RoleExpireRemoveExpireTime.php, line 39

Class

RoleExpireRemoveExpireTime
Provides a 'Remove expire time' action.

Namespace

Drupal\role_expire\Plugin\RulesAction

Code

protected function doExecute(UserInterface $account, array $roles) {
  foreach ($roles as $role) {

    // Skip adding the expire time for the role if user doesn't have it.
    if ($account
      ->hasRole($role)) {
      try {
        \Drupal::service('role_expire.api')
          ->deleteRecord($account
          ->id(), $role);
      } catch (\InvalidArgumentException $e) {
        throw new InvalidArgumentException($e
          ->getMessage());
      }
    }
  }
}