You are here

function autologout_update_8001 in Automated Logout 8

Transfer logouts time settings from configs to states.

File

./autologout.install, line 13
Install and update functions for the autologout module.

Code

function autologout_update_8001(&$sandbox) {
  $result = Database::getConnection()
    ->select('config', 'c')
    ->fields('c', [
    'name',
    'data',
  ])
    ->condition('name', 'autologout.user.%', 'LIKE')
    ->execute()
    ->fetchAll();
  if (!isset($sandbox['current'])) {
    $sandbox['current'] = 0;
    $sandbox['max'] = count($result);
  }
  $limit = 5;
  $result = array_slice($result, $sandbox['current'], $limit);
  foreach ($result as $row) {
    $key = $row->name;

    // User uid is a part of the key after. E.g. autologout.user.1 for user 1.
    $user_id = substr($key, 16);
    $data = unserialize($row->data);
    \Drupal::service('user.data')
      ->set('autologout', $user_id, 'timeout', $data['timeout']);
    \Drupal::service('user.data')
      ->set('autologout', $user_id, 'enabled', $data['enabled']);
    $sandbox['current']++;
  }
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['current'] / $sandbox['max'];
  if ($sandbox['#finished'] >= 1) {
    return t('Autologout settings are successfully updated. Updated @users', [
      "@users" => $sandbox['max'],
    ]);
  }
}