UserCancelForm::submitForm

public UserCancelForm::submitForm(array &$form, FormStateInterface $form_state)

This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state can be updated, this way the subsequently invoked handlers can retrieve a regular entity object to act on. Generally this method should not be overridden unless the entity requires the same preparation for two actions, see \Drupal\comment\CommentForm for an example with the save and preview actions.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides ContentEntityForm::submitForm

File

core/modules/user/src/Form/UserCancelForm.php, line 123

Class

UserCancelForm
Provides a confirmation form for cancelling user account.

Namespace

Drupal\user\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  // Cancel account immediately, if the current user has administrative
  // privileges, no confirmation mail shall be sent, and the user does not
  // attempt to cancel the own account.
  if (!$form_state->isValueEmpty('access') && $form_state->isValueEmpty('user_cancel_confirm') && $this->entity->id() != $this->currentUser()->id()) {
    user_cancel($form_state->getValues(), $this->entity->id(), $form_state->getValue('user_cancel_method'));

    $form_state->setRedirectUrl($this->entity->urlInfo('collection'));
  }
  else {
    // Store cancelling method and whether to notify the user in
    // $this->entity for
    // \Drupal\user\Controller\UserController::confirmCancel().
    $this->entity->user_cancel_method = $form_state->getValue('user_cancel_method');
    $this->entity->user_cancel_notify = $form_state->getValue('user_cancel_notify');
    $this->entity->save();
    _user_mail_notify('cancel_confirm', $this->entity);
    drupal_set_message($this->t('A confirmation request to cancel your account has been sent to your email address.'));
    $this->logger('user')->notice('Sent account cancellation request to %name %email.', array('%name' => $this->entity->label(), '%email' => '<' . $this->entity->getEmail() . '>'));

    $form_state->setRedirect(
    'entity.user.canonical', 
    array('user' => $this->entity->id())
    );
  }
}
doc_Drupal
2016-10-29 09:52:24
Comments
Leave a Comment

Please login to continue.