You are here

public function SettingsForm::buildForm in Auto Purge Users 8.2

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

Class

SettingsForm
Class SettingsForm.

Namespace

Drupal\purge_users\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);
  $config = $this
    ->config('purge_users.settings');
  $roles_array = user_role_names(TRUE);
  $exclude_role_array = $roles_array;
  unset($exclude_role_array['authenticated']);
  $moduleHandler = \Drupal::service('module_handler');
  $notification_subject = $this
    ->t('Your account is deleted');
  $notification_users_before_subject = $this
    ->t('Your account will be deleted');
  $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");
  $notification_users_before_text = $this
    ->t("Dear User, \n\nYour account will be deleted soon 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'] = [
    '#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'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Value'),
    '#default_value' => $config
      ->get('user_never_lastlogin_value'),
    '#prefix' => '<div class="purge-interval-selector clearfix">',
    '#attributes' => [
      'class' => [
        'purge-value',
      ],
    ],
  ];
  $form['never_loggedin_user']['user_never_lastlogin_period'] = [
    '#title' => $this
      ->t('Period'),
    '#type' => 'select',
    '#options' => [
      'days' => $this
        ->t('Days'),
      'month' => $this
        ->t('Months'),
      'year' => $this
        ->t('Year'),
    ],
    '#default_value' => $config
      ->get('user_never_lastlogin_period'),
    '#attributes' => [
      'class' => [
        'purge-period',
      ],
    ],
    '#suffix' => '</div>',
  ];
  $form['never_loggedin_user']['enabled_never_loggedin_users'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enabled'),
    '#default_value' => $config
      ->get('enabled_never_loggedin_users'),
  ];
  $form['not_loggedin_user'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Purge users who have not logged in for'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  ];
  $form['not_loggedin_user']['user_lastlogin_value'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Value'),
    '#default_value' => $config
      ->get('user_lastlogin_value'),
    '#prefix' => '<div class="purge-interval-selector clearfix">',
    '#attributes' => [
      'class' => [
        'purge-value',
      ],
    ],
  ];
  $form['not_loggedin_user']['user_lastlogin_period'] = [
    '#title' => $this
      ->t('Period'),
    '#type' => 'select',
    '#options' => [
      'days' => $this
        ->t('Days'),
      'month' => $this
        ->t('Months'),
      'year' => $this
        ->t('Year'),
    ],
    '#default_value' => $config
      ->get('user_lastlogin_period'),
    '#attributes' => [
      'class' => [
        'purge-period',
      ],
    ],
    '#suffix' => '</div>',
  ];
  $form['not_loggedin_user']['enabled_loggedin_users'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enabled'),
    '#default_value' => $config
      ->get('enabled_loggedin_users'),
  ];
  $form['not_active_user'] = [
    '#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'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Value'),
    '#default_value' => $config
      ->get('user_inactive_value'),
    '#prefix' => '<div class="purge-interval-selector clearfix">',
    '#attributes' => [
      'class' => [
        'purge-value',
      ],
    ],
  ];
  $form['not_active_user']['user_inactive_period'] = [
    '#title' => $this
      ->t('Period'),
    '#type' => 'select',
    '#options' => [
      'days' => $this
        ->t('Days'),
      'month' => $this
        ->t('Months'),
      'year' => $this
        ->t('Year'),
    ],
    '#default_value' => $config
      ->get('user_inactive_period'),
    '#attributes' => [
      'class' => [
        'purge-period',
      ],
    ],
    '#suffix' => '</div>',
  ];
  $form['not_active_user']['enabled_inactive_users'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enabled'),
    '#default_value' => $config
      ->get('enabled_inactive_users'),
  ];
  $form['blocked_user'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Purge users who have been blocked for'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  ];
  $form['blocked_user']['user_blocked_value'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Value'),
    '#default_value' => $config
      ->get('user_blocked_value'),
    '#prefix' => '<div class="purge-interval-selector clearfix">',
    '#attributes' => [
      'class' => [
        'purge-value',
      ],
    ],
  ];
  $form['blocked_user']['user_blocked_period'] = [
    '#title' => $this
      ->t('Period'),
    '#type' => 'select',
    '#options' => [
      'days' => $this
        ->t('Days'),
      'month' => $this
        ->t('Months'),
      'year' => $this
        ->t('Year'),
    ],
    '#default_value' => $config
      ->get('user_blocked_period'),
    '#attributes' => [
      'class' => [
        'purge-period',
      ],
    ],
    '#suffix' => '</div>',
  ];
  $form['blocked_user']['enabled_blocked_users'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enabled'),
    '#default_value' => $config
      ->get('enabled_blocked_users'),
  ];
  $form['limit_roles'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Role limitation'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  ];
  $form['limit_roles']['purge_included_users_roles'] = [
    '#title' => $this
      ->t('Select roles to include in the purge'),
    '#description' => $this
      ->t('Any user with one of the selected roles will be included, unless that user has a role among the one selected in the exclude roles field.'),
    '#type' => 'checkboxes',
    '#required' => TRUE,
    '#options' => $roles_array,
    '#default_value' => is_array($config
      ->get('purge_included_users_roles')) ? $config
      ->get('purge_included_users_roles') : [],
  ];
  $form['limit_roles']['purge_excluded_users_roles'] = [
    '#title' => $this
      ->t('Select roles to exclude from the purge'),
    '#description' => $this
      ->t('Any user with one of the selected role will be excluded.'),
    '#type' => 'checkboxes',
    '#required' => TRUE,
    '#options' => $exclude_role_array,
    '#default_value' => is_array($config
      ->get('purge_excluded_users_roles')) ? $config
      ->get('purge_excluded_users_roles') : [],
  ];
  $user_cancel_methods = user_cancel_methods();

  // Add the possibility to choose the site policy.
  $user_cancel_methods['#options']['user_cancel_site_policy'] = $this
    ->t("Follow site's policy");
  $form['purge_user_cancel_method'] = [
    '#type' => 'radios',
    '#required' => TRUE,
    '#title' => $this
      ->t('When cancelling a user account'),
    '#default_value' => $config
      ->get('purge_user_cancel_method'),
    '#options' => $user_cancel_methods['#options'],
  ];
  $form['disregard_blocked_users'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Disregard inactive/blocked users'),
    '#default_value' => $config
      ->get('disregard_blocked_users'),
    '#description' => $this
      ->t('Do not look at inactive and blocked users. If you use a cancellation method that blocks users, this should normally be enabled, or blocked users will be included in the processing and make it unnecessarily heavy.'),
  ];
  $form['purge_on_cron'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Purge on cron'),
    '#default_value' => $config
      ->get('purge_on_cron'),
  ];
  $form['user_notification'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('User Deletion Notification'),
    '#open' => FALSE,
  ];
  $form['user_notification']['inactive_user_notify_subject'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Subject of user notification e-mail'),
    '#default_value' => $config
      ->get('inactive_user_notify_subject') ? $config
      ->get('inactive_user_notify_subject') : $notification_subject,
    '#cols' => 1,
    '#rows' => 1,
    '#description' => $this
      ->t('Customize the subject of the notification e-mail sent to the user.'),
    '#required' => TRUE,
  ];
  $form['user_notification']['inactive_user_notify_text'] = [
    '#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'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => [
        'user',
      ],
      '#show_restricted' => TRUE,
      '#show_nested' => FALSE,
    ];
  }
  $form['user_notification']['send_email_notification'] = [
    '#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['user_before_deletion'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Notification User Before Deletion'),
    '#open' => FALSE,
  ];
  $form['user_before_deletion']['user_before_deletion_subject'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Subject of user before deletion notification e-mail'),
    '#default_value' => $config
      ->get('user_before_deletion_subject') ? $config
      ->get('user_before_deletion_subject') : $notification_users_before_subject,
    '#cols' => 1,
    '#rows' => 1,
    '#description' => $this
      ->t('Customize the subject of the notification e-mail sent to the user.'),
    '#required' => TRUE,
  ];
  $form['user_before_deletion']['user_before_deletion_text'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Body of user before deletion notification e-mail'),
    '#default_value' => $config
      ->get('user_before_deletion_text') ? $config
      ->get('user_before_deletion_text') : $notification_users_before_text,
    '#cols' => 70,
    '#rows' => 10,
    '#description' => $this
      ->t('Customize the body of the notification e-mail sent to the user.'),
    '#required' => TRUE,
  ];
  $form['user_before_deletion']['user_before_notification_value'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Value'),
    '#default_value' => $config
      ->get('user_before_notification_value'),
    '#prefix' => '<div class="purge-interval-selector clearfix">',
    '#attributes' => [
      'class' => [
        'purge-value',
      ],
    ],
  ];
  $form['user_before_deletion']['user_before_notification_period'] = [
    '#title' => $this
      ->t('Period'),
    '#type' => 'select',
    '#options' => [
      'days' => $this
        ->t('Days'),
      'month' => $this
        ->t('Months'),
      'year' => $this
        ->t('Year'),
    ],
    '#default_value' => $config
      ->get('user_before_notification_period'),
    '#attributes' => [
      'class' => [
        'purge-period',
      ],
    ],
    '#suffix' => '</div>',
  ];
  $form['user_before_deletion']['send_email_user_before_notification'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable'),
    '#description' => $this
      ->t('Check to send email notification before purging users.'),
    '#default_value' => $config
      ->get('send_email_user_before_notification'),
  ];
  $form['purge_users_now'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Purge users now'),
    '#attributes' => [
      'class' => [
        'purge-now',
        'button button--primary',
      ],
    ],
    '#submit' => [
      '::purgeUsersNowSubmit',
    ],
  ];

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