You are here

function user_expire_set_expiration in User Expire 8

Same name and namespace in other branches
  1. 7 user_expire.module \user_expire_set_expiration()

Set a specific user's expiration time.

Parameters

object $account: A user object to modify.

int $expiration: (Optional) An expiration time to set for the user. If this value is omitted, it will be used to reset a user's expiration time.

5 calls to user_expire_set_expiration()
UserExpireTest::testUserExpire in tests/src/Functional/UserExpireTest.php
Tests user expiration functionality.
user_expire_expire_users in ./user_expire.module
Expire a group of users.
user_expire_user_cancel in ./user_expire.module
Implements hook_user_cancel().
user_expire_user_delete in ./user_expire.module
Implements hook_user_delete().
_user_expire_save in ./user_expire.module
Save expiration date from user edit form.

File

./user_expire.module, line 217
Main module file for User expire module.

Code

function user_expire_set_expiration($account, $expiration = NULL) {
  if (!empty($expiration)) {

    // If there's an expiration, save it.
    \Drupal::database()
      ->merge('user_expire')
      ->key([
      'uid' => $account
        ->id(),
    ])
      ->fields([
      'uid' => $account
        ->id(),
      'expiration' => $expiration,
    ])
      ->execute();
    $account->expiration = $expiration;
    user_expire_notify_user($account);
  }
  else {

    // If the expiration is not set, delete any value that might be set.
    if (!$account
      ->isNew()) {

      // New accounts can't have a record to delete.
      // Existing records (!is_new) might.
      // Remove user expiration times for this user.
      $deleted = \Drupal::database()
        ->delete('user_expire')
        ->condition('uid', $account
        ->id())
        ->execute();

      // Notify user that expiration time has been deleted.
      if ($deleted) {
        \Drupal::messenger()
          ->addMessage(t("%name's expiration date has been reset.", [
          '%name' => $account
            ->getAccountName(),
        ]));
      }
    }
  }
}