EmailConfirmerResponseForm.php in Email confirmer 8
File
src/Form/EmailConfirmerResponseForm.php
View source
<?php
namespace Drupal\email_confirmer\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\email_confirmer\InvalidConfirmationStateException;
use Symfony\Component\HttpFoundation\RedirectResponse;
class EmailConfirmerResponseForm extends EntityConfirmFormBase {
public function getDescription() {
return $this
->config('email_confirmer.settings')
->get('confirmation_response.questions.' . $this->entity
->getStatus());
}
public function getConfirmText() {
$confirmation = $this
->getEntity();
return $confirmation
->isPending() ? $this
->t('Send') : $this
->t('OK');
}
public function getCancelUrl() {
return Url::fromRoute('<front>');
}
public function getFormName() {
return $this
->getFormId();
}
public function getFormId() {
return 'email_confirmer_response';
}
public function buildForm(array $form, FormStateInterface $form_state) {
if ($this
->config('email_confirmer.settings')
->get('confirmation_response.skip_confirmation_form')) {
return $this
->skipConfirmationForm($form_state);
}
$form = parent::buildForm($form, $form_state);
unset($form['#process']);
unset($form['#after_build']);
unset($form['actions']['cancel']);
if ($this
->getEntity()
->isPending()) {
$form['cancel'] = [
'#type' => 'radios',
'#default_value' => 0,
'#options' => [
0 => $this
->t('Confirm'),
1 => $this
->t('Cancel'),
],
];
}
return $form;
}
public function getQuestion() {
$confirmation = $this
->getEntity();
$question = '';
switch ($confirmation
->getStatus()) {
case 'pending':
$question = $this
->t('Confirm %email', [
'%email' => $this->entity
->label(),
]);
break;
case 'expired':
if ($confirmation
->isConfirmed() || $confirmation
->isCancelled()) {
$question = $this
->t('Already processed');
}
else {
$question = $this
->t('Confirmation expired');
}
break;
case 'cancelled':
$question = $this
->t('Confirmation cancelled');
break;
case 'confirmed':
$question = $this
->t('Confirmation done');
break;
}
return $question;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if (!$this
->getEntity()
->isPending()) {
$form_state
->setRedirectUrl($this
->getRedirectUrl('error'));
return;
}
try {
if ($form_state
->getValue('cancel')) {
$this
->cancelConfirmation($form_state);
}
else {
$this
->confirmConfirmation($form_state);
}
} catch (InvalidConfirmationStateException $exception) {
$this
->confirmationError($form_state);
}
}
protected function cancelConfirmation(FormStateInterface $form_state) {
$confirmation = $this
->getEntity();
$confirmation
->cancel();
$this
->messenger()
->addStatus($this
->t('Email confirmation cancelled.'));
$confirmation
->save();
$form_state
->setRedirectUrl($this
->getRedirectUrl('cancel'));
}
protected function confirmConfirmation(FormStateInterface $form_state) {
$confirmation = $this
->getEntity();
if (!$confirmation
->confirm($this
->getRouteMatch()
->getParameter('hash'))) {
$this
->confirmationError($form_state);
return;
}
$this
->messenger()
->addStatus($this
->t('Email confirmation confirmed.'));
$confirmation
->save();
$form_state
->setRedirectUrl($this
->getRedirectUrl('confirm'));
}
protected function confirmationError(FormStateInterface $form_state) {
$this
->messenger()
->addError($this
->t('There was an error processing your email confirmation.'), 'error');
$form_state
->setRedirectUrl($this
->getRedirectUrl('error'));
}
protected function getRedirectUrl($operation) {
$confirmation = $this
->getEntity();
if (!($url = $confirmation
->getResponseUrl($operation))) {
$path = $this
->config('email_confirmer.settings')
->get('confirmation_response.url.' . $operation) ?: '<front>';
$url = Url::fromUri('internal:/' . $path);
}
return $url;
}
protected function skipConfirmationForm(FormStateInterface $form_state) {
try {
$this
->confirmConfirmation($form_state);
} catch (InvalidConfirmationStateException $exception) {
$this
->confirmationError($form_state);
}
$redirect_state = $form_state
->getRedirect() ?: $this
->getRedirectUrl('error');
if ($redirect_state instanceof RedirectResponse) {
$redirect_response = $redirect_state;
}
else {
$redirect_response = new RedirectResponse($redirect_state instanceof Url ? $redirect_state
->setAbsolute()
->toString() : $redirect_state);
}
return $redirect_response;
}
}