autologout.install 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. use Drupal\Core\Database\Database;
  3. /**
  4. * Transfer logouts time settings from configs to states.
  5. */
  6. function autologout_update_8001(&$sandbox) {
  7. $result = Database::getConnection()
  8. ->select('config', 'c')
  9. ->fields('c', ['name', 'data'])
  10. ->condition('name', 'autologout.user.%', 'LIKE')
  11. ->execute()->fetchAll();
  12. if (!isset($sandbox['current'])) {
  13. $sandbox['current'] = 0;
  14. $sandbox['max'] = count($result);
  15. }
  16. $limit = 5;
  17. $result = array_slice($result, $sandbox['current'], $limit);
  18. foreach ($result as $row) {
  19. $key = $row->name;
  20. // User uid is a part of the key after. E.g. autologout.user.1 for user 1.
  21. $user_id = (substr($key, 16));
  22. $data = unserialize($row->data);
  23. \Drupal::service('user.data')->set('autologout', $user_id, 'timeout', $data['timeout']);
  24. \Drupal::service('user.data')->set('autologout', $user_id, 'enabled', $data['enabled']);
  25. $sandbox['current']++;
  26. }
  27. $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['current'] / $sandbox['max']);
  28. if ($sandbox['#finished'] >= 1) {
  29. return t('Autologout settings are successfully updated. Updated @users', ["@users" => $sandbox['max']]);
  30. }
  31. }