diff --git a/src/Controller/ThankYou.php b/src/Controller/ThankYou.php new file mode 100644 index 0000000000000000000000000000000000000000..2e5af865b2b1890dda38ad5e26e09a522edc3c43 --- /dev/null +++ b/src/Controller/ThankYou.php @@ -0,0 +1,40 @@ +<?php + +namespace Drupal\verify_email\Controller; + +use Drupal\Core\Controller\ControllerBase; +use Drupal\verify_email\Entity\VerifyEmail; + +/** + * Controller for handling the thank you page. + */ +class ThankYou extends ControllerBase { + + /** + * Renders the thank you page. + * + * @param \Drupal\verify_email\Entity\VerifyEmail|null $entity + * The verify email entity passed from the route. + * + * @return array + * A render array. + */ + public function thankYouPage(VerifyEmail|null $entity = NULL) { + if ($entity) { + // Display the custom message. + $message = t('Please check your email and click on the included link to verify your email address.'); + + return [ + '#markup' => $message, + '#title' => t('Thank you'), + ]; + } + + // Fallback message in case entity doesn't exist. + return [ + '#markup' => t('Something has gone wrong. Please try submitting your email address again.'), + '#title' => t('Sorry'), + ]; + } + +} diff --git a/src/Form/VerifyEmailForm.php b/src/Form/VerifyEmailForm.php index 798a7095a2d0b90df392bd71370b2880d7258680..13c7765078e86dd5a63e1f6cf9cc035049013678 100644 --- a/src/Form/VerifyEmailForm.php +++ b/src/Form/VerifyEmailForm.php @@ -10,6 +10,7 @@ use Drupal\verify_email\Service\VerifierInterface; use Drupal\verify_email\VerifyEmailInterface; use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Url; /** * Provides a Verify Email form. @@ -91,7 +92,9 @@ public function submitForm(array &$form, FormStateInterface $form_state): void { // Send email containing confirmation link. $this->verifier->sendVerificationEmail($entity, $email); $this->messenger()->addStatus($this->t('The message has been sent.')); - $form_state->setRedirect('<front>'); + + // Redirect to the 'thank you' page of the specific entity. + $form_state->setRedirectUrl(Url::fromRoute('verify_email.' . $entity->id() . '.thanks')); } } diff --git a/src/Routing/VerifyEmailRoutes.php b/src/Routing/VerifyEmailRoutes.php index e35bfb8d66eb3e6b688a82d26d0e1611685ee2bc..f6c32f2682bb19bac9ae8f5a66f3c31252ba2d53 100644 --- a/src/Routing/VerifyEmailRoutes.php +++ b/src/Routing/VerifyEmailRoutes.php @@ -55,10 +55,29 @@ public function routes(): array { ], ], ); + + // New route for the "/thanks" path with the controller. + $routes["verify_email.$entity_id.thanks"] = new Route( + path: '/' . $entity->getPath() . '/thanks', + defaults: [ + '_controller' => 'Drupal\verify_email\Controller\ThankYou::thankYouPage', + '_title' => 'Thank you', + 'entity' => $entity_id, + ], + requirements: [ + '_access' => 'TRUE', + ], + options: [ + 'parameters' => [ + 'entity' => [ + 'type' => 'entity:verify_email', + ], + ], + ], + ); } return $routes; - } } diff --git a/tests/src/Functional/VerifyEmailRoutingTest.php b/tests/src/Functional/VerifyEmailRoutingTest.php index b5c13c53338f64be31bd002dad464eceb1f4dde6..c549b5dda5a333d8b99e35597c8e6ac33b12f20f 100644 --- a/tests/src/Functional/VerifyEmailRoutingTest.php +++ b/tests/src/Functional/VerifyEmailRoutingTest.php @@ -105,13 +105,21 @@ public function testInvalidEmail(): void { */ public function testVerifyEmailForm(): void { $this->drupalGet('/verify-email-form'); - $this->assertSession()->statusCodeEquals(200); - // Submit the form with an valid email address. + // Store assertSession in a variable. + $session = $this->assertSession(); + + $session->statusCodeEquals(200); + + // Submit the form with a valid email address. $this->submitForm([ 'email' => 'another@test.com', ], 'Verify'); - $this->assertSession()->statusCodeEquals(200); - $this->assertSession()->statusMessageContains('The message has been sent.'); + + // Check redirection to the 'Thank You' page after form submission. + $session->statusCodeEquals(200); + $session->addressEquals('/verify-email-form/thanks'); + $session->elementExists('xpath', '//h1[text() = "Thank you"]'); + $session->pageTextContains('Please check your email and click on the included link to verify your email address.'); } /**