You are here

class RoleExpireRemoveExpireTime in Role Expire 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/RulesAction/RoleExpireRemoveExpireTime.php \Drupal\role_expire\Plugin\RulesAction\RoleExpireRemoveExpireTime

Provides a 'Remove expire time' action.

Plugin annotation


@RulesAction(
  id = "role_expire_remove_expire_time",
  label = @Translation("Remove expire time for user roles"),
  category = @Translation("Role expire"),
  context_definitions = {
    "user" = @ContextDefinition("entity:user",
      label = @Translation("User")
    ),
    "roles" = @ContextDefinition("string",
      label = @Translation("Roles ID"),
      multiple = TRUE
    )
  }
)

Hierarchy

Expanded class hierarchy of RoleExpireRemoveExpireTime

File

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

Namespace

Drupal\role_expire\Plugin\RulesAction
View source
class RoleExpireRemoveExpireTime extends RulesActionBase {

  /**
   * Assign expire time for user and role.
   *
   * @param \Drupal\user\UserInterface $account
   *   User object.
   * @param string $roles
   *   Array of User roles ID.
   *
   * @throws \Drupal\rules\Exception\InvalidArgumentException
   */
  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());
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RoleExpireRemoveExpireTime::doExecute protected function Assign expire time for user and role.