user_cancel example

return $form;
  }

  /** * {@inheritdoc} */
  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->toUrl('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);
      
$admin_form_state->unsetValue('user_cancel_confirm');
          // The $user global is not a complete user entity, so load the full           // entity.           $account = $this->userStorage->load($uid);
          $admin_form = $this->entityTypeManager->getFormObject('user', 'cancel');
          $admin_form->setEntity($account);
          // Calling this directly required to init form object with $account.           $admin_form->buildForm($admin_form_mock$admin_form_state);
          $admin_form->submitForm($admin_form_mock$admin_form_state);
        }
        else {
          user_cancel($form_state->getValues()$uid$form_state->getValue('user_cancel_method'));
        }
      }
    }
    $form_state->setRedirect('entity.user.collection');
  }

}

  public function deleteIndividual(EntityInterface $entity) {
    // @todo Replace with entity handlers in: https://www.drupal.org/project/drupal/issues/3230434     if ($entity->getEntityTypeId() === 'user') {
      $cancel_method = \Drupal::service('config.factory')->get('user.settings')->get('cancel_method');

      // Allow other modules to act.
      user_cancel([]$entity->id()$cancel_method);
      // Since user_cancel() is not invoked via Form API, batch processing       // needs to be invoked manually.       $batch =& batch_get();
      // Mark this batch as non-progressive to bypass the progress bar and       // redirect.       $batch['progressive'] = FALSE;
      batch_process();
    }
    else {
      $entity->delete();
    }

    
// Time out in seconds until cancel URL expires; 24 hours = 86400 seconds.     $timeout = 86400;

    // Basic validation of arguments.     $account_data = $this->userData->get('user', $user->id());
    if (isset($account_data['cancel_method']) && !empty($timestamp) && !empty($hashed_pass)) {
      // Validate expiration and hashed password/login.       if ($user->id() && $this->validatePathParameters($user$timestamp$hashed_pass$timeout)) {
        $edit = [
          'user_cancel_notify' => $account_data['cancel_notify'] ?? $this->config('user.settings')->get('notify.status_canceled'),
        ];
        user_cancel($edit$user->id()$account_data['cancel_method']);
        // Since user_cancel() is not invoked via Form API, batch processing         // needs to be invoked manually and should redirect to the front page         // after completion.         return batch_process('<front>');
      }
      else {
        $this->messenger()->addError($this->t('You have tried to use an account cancellation link that has expired. Please request a new one using the form below.'));
        return $this->redirect('entity.user.cancel_form', ['user' => $user->id()]['absolute' => TRUE]);
      }
    }
    throw new AccessDeniedHttpException();
  }
Home | Imprint | This part of the site doesn't use cookies.