class UserMultipleCancelConfirm in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/user/src/Form/UserMultipleCancelConfirm.php \Drupal\user\Form\UserMultipleCancelConfirm
Provides a confirmation form for cancelling multiple user accounts.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfirmFormBase implements ConfirmFormInterface
- class \Drupal\user\Form\UserMultipleCancelConfirm
- class \Drupal\Core\Form\ConfirmFormBase implements ConfirmFormInterface
Expanded class hierarchy of UserMultipleCancelConfirm
1 string reference to 'UserMultipleCancelConfirm'
- user.routing.yml in core/
modules/ user/ user.routing.yml - core/modules/user/user.routing.yml
File
- core/
modules/ user/ src/ Form/ UserMultipleCancelConfirm.php, line 21 - Contains \Drupal\user\Form\UserMultipleCancelConfirm.
Namespace
Drupal\user\FormView source
class UserMultipleCancelConfirm extends ConfirmFormBase {
/**
* The temp store factory.
*
* @var \Drupal\user\PrivateTempStoreFactory
*/
protected $tempStoreFactory;
/**
* The user storage.
*
* @var \Drupal\user\UserStorageInterface
*/
protected $userStorage;
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* Constructs a new UserMultipleCancelConfirm.
*
* @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
* The temp store factory.
* @param \Drupal\user\UserStorageInterface $user_storage
* The user storage.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
*/
public function __construct(PrivateTempStoreFactory $temp_store_factory, UserStorageInterface $user_storage, EntityManagerInterface $entity_manager) {
$this->tempStoreFactory = $temp_store_factory;
$this->userStorage = $user_storage;
$this->entityManager = $entity_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('user.private_tempstore'), $container
->get('entity.manager')
->getStorage('user'), $container
->get('entity.manager'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'user_multiple_cancel_confirm';
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this
->t('Are you sure you want to cancel these user accounts?');
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('entity.user.collection');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this
->t('Cancel accounts');
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$current_user_id = $this
->currentUser()
->id();
// Clear out the accounts from the temp store.
$this->tempStoreFactory
->get('user_user_operations_cancel')
->delete($current_user_id);
if ($form_state
->getValue('confirm')) {
foreach ($form_state
->getValue('accounts') as $uid => $value) {
// Prevent programmatic form submissions from cancelling user 1.
if ($uid <= 1) {
continue;
}
// Prevent user administrators from deleting themselves without confirmation.
if ($uid == $current_user_id) {
$admin_form_mock = array();
$admin_form_state = $form_state;
$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->entityManager
->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');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfirmFormBase:: |
public | function |
Returns a caption for the link which cancels the action. Overrides ConfirmFormInterface:: |
2 |
ConfirmFormBase:: |
public | function |
Returns additional text to display as a description. Overrides ConfirmFormInterface:: |
8 |
ConfirmFormBase:: |
public | function |
Returns the internal name used to refer to the confirmation item. Overrides ConfirmFormInterface:: |
|
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 3 |
FormBase:: |
protected | property | The logger factory. | |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 3 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
64 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Returns a redirect response object for the specified route. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. | |
UserMultipleCancelConfirm:: |
protected | property | The entity manager. | |
UserMultipleCancelConfirm:: |
protected | property | The temp store factory. | |
UserMultipleCancelConfirm:: |
protected | property | The user storage. | |
UserMultipleCancelConfirm:: |
public | function |
Form constructor. Overrides ConfirmFormBase:: |
|
UserMultipleCancelConfirm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
UserMultipleCancelConfirm:: |
public | function |
Returns the route to go to if the user cancels the action. Overrides ConfirmFormInterface:: |
|
UserMultipleCancelConfirm:: |
public | function |
Returns a caption for the button that confirms the action. Overrides ConfirmFormBase:: |
|
UserMultipleCancelConfirm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
UserMultipleCancelConfirm:: |
public | function |
Returns the question to ask the user. Overrides ConfirmFormInterface:: |
|
UserMultipleCancelConfirm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
UserMultipleCancelConfirm:: |
public | function | Constructs a new UserMultipleCancelConfirm. |