You are here

public function UserExpireSettingsForm::buildForm in User Expire 8

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 ConfigFormBase::buildForm

File

src/Form/UserExpireSettingsForm.php, line 64

Class

UserExpireSettingsForm
User expire admin settings form.

Namespace

Drupal\user_expire\Form

Code

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

  // Get the rules and the roles.
  $config = $this
    ->config('user_expire.settings');
  $rules = $config
    ->get('user_expire_roles') ?: [];
  $user_roles = $this->entityTypeManager
    ->getStorage('user_role')
    ->loadMultiple();
  $roles = [];
  foreach ($user_roles as $rid => $role) {
    $roles[$role
      ->id()] = $role
      ->get('label');
  }

  // Save the current roles for use in submit handler.
  $form['current_roles'] = [
    '#type' => 'value',
    '#value' => $roles,
  ];
  $form['frequency'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Frequency time in seconds'),
    '#default_value' => $config
      ->get('frequency') ?: 172800,
    '#description' => $this
      ->t('86400 = 1 day'),
  ];
  $form['offset'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Warning offset time in seconds'),
    '#default_value' => $config
      ->get('offset') ?: 604800,
    '#description' => $this
      ->t('86400 = 1 day'),
  ];

  // Now show boxes for each role.
  $form['user_expire_roles'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('User inactivity expire by role settings'),
    '#description' => $this
      ->t('Configure expiration of users by roles. Enter 0 to disable for the role. Enter 7776000 for 90 days.'),
  ];
  foreach ($roles as $rid => $role) {
    if ($rid === RoleInterface::ANONYMOUS_ID) {
      continue;
    }
    $form['user_expire_roles']['user_expire_' . $rid] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Seconds of inactivity before expiring %role users', [
        '%role' => $role,
      ]),
      '#default_value' => $rules[$rid] ?? 0,
    ];
  }
  return parent::buildForm($form, $form_state);
}