You are here

public function RoleExpireConfigurationForm::buildForm in Role Expire 2.x

Same name and namespace in other branches
  1. 8 src/Form/RoleExpireConfigurationForm.php \Drupal\role_expire\Form\RoleExpireConfigurationForm::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/RoleExpireConfigurationForm.php, line 58

Class

RoleExpireConfigurationForm
Configure order for this site.

Namespace

Drupal\role_expire\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $excluded_roles = [
    'anonymous',
    'authenticated',
  ];
  $parsed_roles = [];
  $roles = user_roles();
  foreach ($roles as $role) {
    $parsed_roles[$role
      ->id()] = $role
      ->label();
  }
  $values = $this->roleExpireApi
    ->getRolesAfterExpiration();
  $valuesStatus = $this->roleExpireApi
    ->getRolesExpirationStatus();
  $default = [
    0 => $this
      ->t('- None -'),
  ];

  // It is important to respect the keys on this array merge.
  $roles_select = $default + $parsed_roles;
  unset($roles_select['anonymous']);
  unset($roles_select['authenticated']);
  $form['general'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('General settings'),
    '#weight' => 1,
  ];
  $form['general']['role_after'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Role assignment after expiration'),
    '#weight' => 1,
    '#open' => TRUE,
  ];
  foreach ($parsed_roles as $rid => $role_name) {
    if (!in_array($rid, $excluded_roles)) {
      $form['general']['role_after'][$rid] = [
        '#type' => 'select',
        '#options' => $roles_select,
        '#title' => $this
          ->t('Role to assign after the role ":r" expires', [
          ':r' => $role_name,
        ]),
        '#default_value' => isset($values[$rid]) ? $values[$rid] : 0,
      ];
    }
  }
  $form['general']['disabled_role'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Role expiration scope'),
    '#weight' => 1,
    '#open' => TRUE,
  ];
  foreach ($parsed_roles as $rid => $role_name) {
    if (!in_array($rid, $excluded_roles)) {
      $form['general']['disabled_role']['disable_' . $rid] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Disable role expiration for :r', [
          ':r' => $role_name,
        ]),
        '#default_value' => isset($valuesStatus[$rid]) ? $valuesStatus[$rid] : 1,
      ];
    }
  }
  return parent::buildForm($form, $form_state);
}