You are here

function autologout_form_user_form_alter in Automated Logout 8

Implements hook_form_FORM_ID_alter().

Adds a field to user/edit to change that users logout.

File

./autologout.module, line 33
Automated Logout - Module.

Code

function autologout_form_user_form_alter(&$form, FormStateInterface $form_state) {
  $user = \Drupal::currentUser();
  $account = $form_state
    ->getFormObject()
    ->getEntity();
  $user_id = $account
    ->id();
  $access = FALSE;

  // If user-specific thresholds are enabled (the default), and user has access
  // to change and they are changing their own and only
  // their own timeout, or they are an admin.
  if (!\Drupal::config('autologout.settings')
    ->get('no_individual_logout_threshold') && !\Drupal::currentUser()
    ->isAnonymous() && ($user
    ->hasPermission('change own logout threshold') && $user
    ->id() == $user_id || $user
    ->hasPermission('administer autologout'))) {
    $access = TRUE;
    if ($user_id !== NULL) {
      $autologout_data = \Drupal::service('user.data')
        ->get('autologout', $user_id, 'timeout');
    }
  }
  if ($access) {
    $max_timeout = \Drupal::config('autologout.settings')
      ->get('max_timeout');
    $form['user_' . $user_id] = [
      '#type' => 'textfield',
      '#title' => t('Your current logout threshold'),
      '#default_value' => isset($autologout_data) ? $autologout_data : '',
      '#size' => 8,
      '#description' => t('The length of inactivity time, in seconds, before automated log out. Must be between 60 and @max_timeout seconds.', [
        '@max_timeout' => $max_timeout,
      ]),
      '#element_validate' => [
        '_autologout_user_uid_timeout_validate',
      ],
    ];
    $form['actions']['submit']['#submit'][] = 'autologout_user_profile_submit';
  }
}