You are here

public function SettingsForm::buildForm in Auto Purge Users 8

Same name and namespace in other branches
  1. 8.3 src/Form/SettingsForm.php \Drupal\purge_users\Form\SettingsForm::buildForm()
  2. 8.2 src/Form/SettingsForm.php \Drupal\purge_users\Form\SettingsForm::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 ConfigFormBase::buildForm

File

src/Form/SettingsForm.php, line 46

Class

SettingsForm
Class SettingsForm.

Namespace

Drupal\purge_users\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);
  $user_settings = \Drupal::config('user.settings');
  $anonymous_name = $user_settings
    ->get('anonymous');
  $config = $this
    ->config('purge_users.settings');
  $roles_array = user_role_names(TRUE);
  $moduleHandler = \Drupal::service('module_handler');
  $notification_text = $this
    ->t("Dear User, \n\nYour account has been deleted due the website’s policy to automatically remove users who match certain criteria. If you have concerns regarding the deletion, please talk to the administrator of the website. \n\nThank you");
  $form['never_loggedin_user'] = array(
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Purge users who have never logged in for'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );
  $form['never_loggedin_user']['user_never_lastlogin_value'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Value'),
    '#default_value' => $config
      ->get('user_never_lastlogin_value'),
    '#prefix' => '<div class="purge-interval-selector clearfix">',
    '#attributes' => array(
      'class' => array(
        'purge-value',
      ),
    ),
  );
  $form['never_loggedin_user']['user_never_lastlogin_period'] = array(
    '#title' => $this
      ->t('Period'),
    '#type' => 'select',
    '#options' => array(
      'days' => $this
        ->t('Days'),
      'month' => $this
        ->t('Months'),
      'year' => $this
        ->t('Year'),
    ),
    '#default_value' => $config
      ->get('user_never_lastlogin_period'),
    '#attributes' => array(
      'class' => array(
        'purge-period',
      ),
    ),
    '#suffix' => '</div>',
  );
  $form['never_loggedin_user']['enabled_never_loggedin_users'] = array(
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enabled'),
    '#default_value' => $config
      ->get('enabled_never_loggedin_users'),
  );
  $form['not_loggedin_user'] = array(
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Purge users who have not logged in for'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );
  $form['not_loggedin_user']['user_lastlogin_value'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Value'),
    '#default_value' => $config
      ->get('user_lastlogin_value'),
    '#prefix' => '<div class="purge-interval-selector clearfix">',
    '#attributes' => array(
      'class' => array(
        'purge-value',
      ),
    ),
  );
  $form['not_loggedin_user']['user_lastlogin_period'] = array(
    '#title' => $this
      ->t('Period'),
    '#type' => 'select',
    '#options' => array(
      'days' => $this
        ->t('Days'),
      'month' => $this
        ->t('Months'),
      'year' => $this
        ->t('Year'),
    ),
    '#default_value' => $config
      ->get('user_lastlogin_period'),
    '#attributes' => array(
      'class' => array(
        'purge-period',
      ),
    ),
    '#suffix' => '</div>',
  );
  $form['not_loggedin_user']['enabled_loggedin_users'] = array(
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enabled'),
    '#default_value' => $config
      ->get('enabled_loggedin_users'),
  );
  $form['not_active_user'] = array(
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Purge users whose account has not been activated for'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );
  $form['not_active_user']['user_inactive_value'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Value'),
    '#default_value' => $config
      ->get('user_inactive_value'),
    '#prefix' => '<div class="purge-interval-selector clearfix">',
    '#attributes' => array(
      'class' => array(
        'purge-value',
      ),
    ),
  );
  $form['not_active_user']['user_inactive_period'] = array(
    '#title' => $this
      ->t('Period'),
    '#type' => 'select',
    '#options' => array(
      'days' => $this
        ->t('Days'),
      'month' => $this
        ->t('Months'),
      'year' => $this
        ->t('Year'),
    ),
    '#default_value' => $config
      ->get('user_inactive_period'),
    '#attributes' => array(
      'class' => array(
        'purge-period',
      ),
    ),
    '#suffix' => '</div>',
  );
  $form['not_active_user']['enabled_inactive_users'] = array(
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enabled'),
    '#default_value' => $config
      ->get('enabled_inactive_users'),
  );
  $form['blocked_user'] = array(
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Purge users who have been blocked for'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );
  $form['blocked_user']['user_blocked_value'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Value'),
    '#default_value' => $config
      ->get('user_blocked_value'),
    '#prefix' => '<div class="purge-interval-selector clearfix">',
    '#attributes' => array(
      'class' => array(
        'purge-value',
      ),
    ),
  );
  $form['blocked_user']['user_blocked_period'] = array(
    '#title' => $this
      ->t('Period'),
    '#type' => 'select',
    '#options' => array(
      'days' => $this
        ->t('Days'),
      'month' => $this
        ->t('Months'),
      'year' => $this
        ->t('Year'),
    ),
    '#default_value' => $config
      ->get('user_blocked_period'),
    '#attributes' => array(
      'class' => array(
        'purge-period',
      ),
    ),
    '#suffix' => '</div>',
  );
  $form['blocked_user']['enabled_blocked_users'] = array(
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enabled'),
    '#default_value' => $config
      ->get('enabled_blocked_users'),
  );
  $form['purge_users_roles'] = array(
    '#title' => $this
      ->t('Limit purge to the following roles'),
    '#description' => $this
      ->t('Limit users to a particular role.'),
    '#type' => 'checkboxes',
    '#required' => TRUE,
    '#options' => $roles_array,
    '#default_value' => is_array($config
      ->get('purge_users_roles')) ? $config
      ->get('purge_users_roles') : array(),
  );
  $form['purge_user_cancel_method'] = array(
    '#type' => 'radios',
    '#required' => TRUE,
    '#title' => $this
      ->t('When cancelling a user account'),
    '#default_value' => $config
      ->get('purge_user_cancel_method'),
    '#options' => array(
      'user_cancel_reassign' => $this
        ->t('Delete the account and make its content belong to the %anonymous-name user.', array(
        '%anonymous-name' => $anonymous_name,
      )),
      'user_cancel_delete' => $this
        ->t('Delete the account and its content.'),
    ),
  );
  $form['purge_on_cron'] = array(
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Purge users only on cron run'),
    '#default_value' => $config
      ->get('purge_on_cron'),
  );
  $form['user_notification'] = array(
    '#type' => 'details',
    '#title' => $this
      ->t('User Deletion Notification'),
    '#open' => FALSE,
  );
  $form['user_notification']['inactive_user_notify_text'] = array(
    '#type' => 'textarea',
    '#title' => $this
      ->t('Body of user notification e-mail'),
    '#default_value' => $config
      ->get('inactive_user_notify_text') ? $config
      ->get('inactive_user_notify_text') : $notification_text,
    '#cols' => 70,
    '#rows' => 10,
    '#description' => $this
      ->t('Customize the body of the notification e-mail sent to the user.'),
    '#required' => TRUE,
  );
  if ($moduleHandler
    ->moduleExists('token')) {
    $form['user_notification']['token_help'] = array(
      '#theme' => 'token_tree_link',
      '#token_types' => array(
        'user',
      ),
      '#show_restricted' => TRUE,
      '#show_nested' => FALSE,
    );
  }
  $form['user_notification']['send_email_notification'] = array(
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable'),
    '#description' => $this
      ->t('Check to send email notification to purged users.'),
    '#default_value' => $config
      ->get('send_email_notification'),
  );
  $form['purge_users_now'] = array(
    '#type' => 'submit',
    '#value' => $this
      ->t('Purge users now'),
    '#attributes' => array(
      'class' => array(
        'purge-now',
        'button button--primary',
      ),
    ),
    '#submit' => array(
      '::purgeUsersNowSubmit',
    ),
  );

  // Attach library.
  $form['#attached']['library'][] = 'purge_users/styling';
  return parent::buildForm($form, $form_state);
}