View source
<?php
namespace Drupal\email_confirmer_user\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\ContentEntityConfirmFormBase;
use Drupal\user\UserDataInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Component\Datetime\TimeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\email_confirmer\EmailConfirmerManagerInterface;
class UserEmailChangeCancelForm extends ContentEntityConfirmFormBase {
protected $userData;
protected $emailConfirmer;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, TimeInterface $time, UserDataInterface $user_data, EmailConfirmerManagerInterface $email_confirmer) {
parent::__construct($entity_type_manager, $entity_type_bundle_info, $time);
$this->userData = $user_data;
$this->emailConfirmer = $email_confirmer;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('entity_type.bundle.info'), $container
->get('datetime.time'), $container
->get('user.data'), $container
->get('email_confirmer'));
}
public function getCancelUrl() {
return $this->entity
->toUrl('edit-form');
}
public function getDescription() {
return ($this
->currentUser()
->id() == $this->entity
->id() ? $this
->t('Your current email address %email will be preserved.', [
'%email' => $this->entity
->getEmail(),
]) : $this
->t("The current user's email address %email will be preserved.", [
'%email' => $this->entity
->getEmail(),
])) . ' ' . parent::getDescription();
}
public function getFormName() {
return $this
->getFormId();
}
public function getFormId() {
return 'email_confirmer_user_email_change_cancel';
}
public function getQuestion() {
$new_email = $this->userData
->get('email_confirmer_user', $this->entity
->id(), 'email_change_new_address');
return $this
->currentUser()
->id() == $this->entity
->id() ? $this
->t('Are you sure you want to cancel the pending change of your email address to %email?', [
'%email' => $new_email,
]) : $this
->t('Are you sure you want to cancel the pending change of the email address of user %user to %email?', [
'%email' => $new_email,
'%user' => $this->entity
->getDisplayName(),
]);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$new_email = $this->userData
->get('email_confirmer_user', $this->entity
->id(), 'email_change_new_address');
$this->userData
->delete('email_confirmer_user', $this->entity
->id(), 'email_change_new_address');
$cancel_url = NULL;
foreach ($this->emailConfirmer
->getConfirmations($new_email, 'pending', 0, 'email_confirmer_user') as $confirmation) {
$confirmation
->cancel();
$confirmation
->save();
$cancel_url = $confirmation
->getResponseUrl('cancel');
}
$form_state
->setRedirectUrl($cancel_url ?: $this->entity
->toUrl('edit-form'));
}
}