You are here

function user_expire_form_user_form_alter in User Expire 8

Implements hook_form_FORM_ID_alter().

Add the user expire form to an individual user's account page.

See also

\Drupal\user\ProfileForm::form()

File

./user_expire.module, line 81
Main module file for User expire module.

Code

function user_expire_form_user_form_alter(&$form, FormStateInterface $form_state) {
  if (\Drupal::currentUser()
    ->hasPermission('set user expiration')) {
    $entity = $form_state
      ->getFormObject()
      ->getEntity();
    $form['user_expire'] = [
      '#type' => 'details',
      '#title' => t('User expiration'),
      '#open' => TRUE,
      '#weight' => 5,
    ];
    $form['user_expire']['user_expiration'] = [
      '#title' => t('Set expiration for this user'),
      '#type' => 'checkbox',
      '#default_value' => !empty($entity->expiration),
    ];
    $form['user_expire']['container'] = [
      '#type' => 'container',
      '#states' => [
        'invisible' => [
          ':input[name="user_expiration"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['user_expire']['container']['user_expiration_date'] = [
      '#title' => t('Expiration date'),
      '#type' => 'datetime',
      '#description' => t('The date on which this account will be disabled.'),
      '#date_date_format' => 'Y-m-d',
      '#date_time_element' => 'none',
      '#default_value' => $entity->expiration ? DrupalDateTime::createFromTimestamp($entity->expiration) : NULL,
      '#required' => !empty($form_state
        ->getValue('user_expiration')),
    ];
  }
  $form['actions']['submit']['#submit'][] = 'user_expire_user_profile_form_submit';
}