You are here

public function SessionLimitForm::buildForm in Session Limit 8

Same name and namespace in other branches
  1. 2.x src/Form/SessionLimitForm.php \Drupal\session_limit\Form\SessionLimitForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/SessionLimitForm.php, line 24

Class

SessionLimitForm

Namespace

Drupal\session_limit\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  /** @var SessionLimit $session_limit */
  $session_limit = \Drupal::service('session_limit');
  $form['title'] = [
    '#type' => 'markup',
    '#markup' => '<p>' . $this
      ->t('Your active sessions are listed below. You need to choose a session to end.') . '</p>',
  ];

  /** @var SessionManager $session_manager */
  $session_manager = \Drupal::service('session_manager');
  $current_session_id = Crypt::hashBase64($session_manager
    ->getId());
  $user = \Drupal::currentUser();
  $form['active_sessions'] = [
    '#type' => 'value',
    '#value' => $session_limit
      ->getUserActiveSessions($user),
  ];
  $session_references = [];
  foreach ($form['active_sessions']['#value'] as $session_reference => $obj) {
    $message = $current_session_id == $obj->sid ? $this
      ->t('Your current session.') : '';
    $session_references[$session_reference] = $this
      ->t('<strong>Host:</strong> %host (idle: %time) <b>@message</b>', [
      '%host' => $obj->hostname,
      '@message' => $message,
      '%time' => \Drupal::service("date.formatter")
        ->formatInterval(time() - $obj->timestamp),
    ]);
  }
  $form['session_reference'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Select a session to disconnect.'),
    '#options' => $session_references,
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Disconnect session'),
  ];
  return $form;
}