Skip to content
Snippets Groups Projects
Commit 5941e868 authored by Nidhi Patadia's avatar Nidhi Patadia Committed by James Shields
Browse files

Add option to redirect logged in users to destination.

parent 2c1e6d51
No related branches found
No related tags found
1 merge request!11Add option to redirect logged in users to destination.
Pipeline #470224 passed
...@@ -7,10 +7,12 @@ ...@@ -7,10 +7,12 @@
use Drupal\Component\Utility\EmailValidatorInterface; use Drupal\Component\Utility\EmailValidatorInterface;
use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Url;
use Drupal\verify_email\Service\VerifierInterface; use Drupal\verify_email\Service\VerifierInterface;
use Drupal\verify_email\VerifyEmailInterface; use Drupal\verify_email\VerifyEmailInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Url; use Symfony\Component\HttpFoundation\RedirectResponse;
/** /**
* Provides a Verify Email form. * Provides a Verify Email form.
...@@ -21,6 +23,7 @@ final class VerifyEmailForm extends FormBase { ...@@ -21,6 +23,7 @@ final class VerifyEmailForm extends FormBase {
* Constructs a new VerifyEmailForm. * Constructs a new VerifyEmailForm.
*/ */
public function __construct( public function __construct(
protected readonly AccountProxyInterface $currentUser,
protected readonly EmailValidatorInterface $emailValidator, protected readonly EmailValidatorInterface $emailValidator,
protected readonly VerifierInterface $verifier, protected readonly VerifierInterface $verifier,
) {} ) {}
...@@ -30,6 +33,7 @@ public function __construct( ...@@ -30,6 +33,7 @@ public function __construct(
*/ */
public static function create(ContainerInterface $container): self { public static function create(ContainerInterface $container): self {
return new static( return new static(
$container->get('current_user'),
$container->get('email.validator'), $container->get('email.validator'),
$container->get('verify_email.verifier'), $container->get('verify_email.verifier'),
); );
...@@ -45,9 +49,12 @@ public function getFormId(): string { ...@@ -45,9 +49,12 @@ public function getFormId(): string {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function buildForm(array $form, FormStateInterface $form_state, VerifyEmailInterface|null $entity = NULL): array { public function buildForm(array $form, FormStateInterface $form_state, VerifyEmailInterface|null $entity = NULL): RedirectResponse|array {
$form_state->set('entity', $entity); $form_state->set('entity', $entity);
if ($this->currentUser()->isAuthenticated()) {
return new RedirectResponse($entity->getRedirectPath());
}
$form['intro'] = [ $form['intro'] = [
'#markup' => $entity->getIntro(), '#markup' => $entity->getIntro(),
......
...@@ -86,6 +86,17 @@ public function testVerifyEmailRoute(): void { ...@@ -86,6 +86,17 @@ public function testVerifyEmailRoute(): void {
$this->assertSession()->pageTextContains('Enter your email address.'); $this->assertSession()->pageTextContains('Enter your email address.');
} }
/**
* Test the verify email form for authenticated user.
*/
public function testVerifyEmailRouteAuthenticatedUser(): void {
$this->drupalLogin($this->adminUser);
$this->drupalGet('/verify-email-form');
// Test redirection.
$this->assertSession()->addressEquals('node/1');
$this->drupalLogout();
}
/** /**
* Tests invalid email validation. * Tests invalid email validation.
*/ */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment