UserMultipleCancelConfirm::buildForm

public UserMultipleCancelConfirm::buildForm(array $form, FormStateInterface $form_state)

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

File

core/modules/user/src/Form/UserMultipleCancelConfirm.php, line 97

Class

UserMultipleCancelConfirm
Provides a confirmation form for cancelling multiple user accounts.

Namespace

Drupal\user\Form

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
public function buildForm(array $form, FormStateInterface $form_state) {
  // Retrieve the accounts to be canceled from the temp store.
  $accounts = $this->tempStoreFactory
    ->get('user_user_operations_cancel')
    ->get($this->currentUser()->id());
  if (!$accounts) {
    return $this->redirect('entity.user.collection');
  }
 
  $root = NULL;
  $form['accounts'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE);
  foreach ($accounts as $account) {
    $uid = $account->id();
    // Prevent user 1 from being canceled.
    if ($uid <= 1) {
      $root = intval($uid) === 1 ? $account : $root;
      continue;
    }
    $form['accounts'][$uid] = array(
      '#type' => 'hidden',
      '#value' => $uid,
      '#prefix' => '<li>',
      '#suffix' => $account->label() . "</li>\n",
    );
  }
 
  // Output a notice that user 1 cannot be canceled.
  if (isset($root)) {
    $redirect = (count($accounts) == 1);
    $message = $this->t('The user account %name cannot be canceled.', array('%name' => $root->label()));
    drupal_set_message($message, $redirect ? 'error' : 'warning');
    // If only user 1 was selected, redirect to the overview.
    if ($redirect) {
      return $this->redirect('entity.user.collection');
    }
  }
 
  $form['operation'] = array('#type' => 'hidden', '#value' => 'cancel');
 
  $form['user_cancel_method'] = array(
    '#type' => 'radios',
    '#title' => $this->t('When cancelling these accounts'),
  );
 
  $form['user_cancel_method'] += user_cancel_methods();
 
  // Allow to send the account cancellation confirmation mail.
  $form['user_cancel_confirm'] = array(
    '#type' => 'checkbox',
    '#title' => $this->t('Require email confirmation to cancel account'),
    '#default_value' => FALSE,
    '#description' => $this->t('When enabled, the user must confirm the account cancellation via email.'),
  );
  // Also allow to send account canceled notification mail, if enabled.
  $form['user_cancel_notify'] = array(
    '#type' => 'checkbox',
    '#title' => $this->t('Notify user when account is canceled'),
    '#default_value' => FALSE,
    '#access' => $this->config('user.settings')->get('notify.status_canceled'),
    '#description' => $this->t('When enabled, the user will receive an email notification after the account has been canceled.'),
  );
 
  $form = parent::buildForm($form, $form_state);
 
  return $form;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.