MassPasswordResetConfirm.php in Mass Password Change 8
File
src/Form/MassPasswordResetConfirm.php
View source
<?php
namespace Drupal\mass_password_change\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\user\UserStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MassPasswordResetConfirm extends ConfirmFormBase {
protected $tempStoreFactory;
protected $userStorage;
public function __construct(PrivateTempStoreFactory $temp_store_factory, UserStorageInterface $userStorage) {
$this->tempStoreFactory = $temp_store_factory;
$this->userStorage = $userStorage;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('tempstore.private'), $container
->get('entity_type.manager')
->getStorage('user'));
}
public function getFormId() {
return 'mass_password_change.password_reset_confirm';
}
public function getQuestion() {
return $this
->t('Are you sure you want to reset password these user accounts?');
}
public function getCancelUrl() {
return new Url('entity.user.collection');
}
public function getConfirmText() {
return $this
->t('Password reset');
}
public function buildForm(array $form, FormStateInterface $form_state) {
$accounts = $this->tempStoreFactory
->get('mass_password_change')
->get('password_reset');
if (!$accounts) {
return $this
->redirect('entity.user.collection');
}
$list = [];
foreach ($accounts as $account) {
$uid = $account
->id();
$list[$uid] = $account
->getAccountName();
$form['accounts'][$uid] = [
'#type' => 'hidden',
'#value' => $uid,
];
}
$form['accounts']['#tree'] = TRUE;
$form['account']['names'] = [
'#theme' => 'item_list',
'#items' => $list,
];
$form = parent::buildForm($form, $form_state);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->tempStoreFactory
->get('mass_password_change')
->delete('mass_password_change');
if ($form_state
->getValue('confirm')) {
foreach ($form_state
->getValue('accounts') as $uid) {
$account = $this->userStorage
->load($uid);
_user_mail_notify('password_reset', $account);
}
}
$form_state
->setRedirect('entity.user.collection');
}
}