You are here

autologout.install in Automated Logout 8

Install and update functions for the autologout module.

File

autologout.install
View source
<?php

/**
 * @file
 * Install and update functions for the autologout module.
 */
use Drupal\Core\Database\Database;

/**
 * Transfer logouts time settings from configs to states.
 */
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'],
    ]);
  }
}

/**
 * Configure default value for dialog title.
 */
function autologout_update_8002(&$sandbox) {
  $autologout_settings = \Drupal::service('config.factory')
    ->getEditable('autologout.settings');
  $default_dialog_title = \Drupal::config('system.site')
    ->get('name') . ' Alert';
  $autologout_settings
    ->set('dialog_title', $default_dialog_title)
    ->save();
}

/**
 * Store default value for setting 'Disable user-specific logout thresholds'.
 */
function autologout_update_8003(&$sandbox) {
  $config_factory = \Drupal::configFactory();
  $config = $config_factory
    ->getEditable('autologout.settings');
  $config
    ->set('no_individual_logout_threshold', FALSE);
  $config
    ->save(TRUE);
}

Functions

Namesort descending Description
autologout_update_8001 Transfer logouts time settings from configs to states.
autologout_update_8002 Configure default value for dialog title.
autologout_update_8003 Store default value for setting 'Disable user-specific logout thresholds'.