2

To access the site, all users (except the logged-in users) need to pass through an age gate. They can only leave the welcome page if they are 18 years or older.

I started by adding a session to my custom theme.theme file.

<?php

function scottishleader_preprocess(&$variables, $hook) {
  static $hooks;

  $route_name = \Drupal::routeMatch()->getRouteName();
  $response = new\Symfony\Component\HttpFoundation\RedirectResponse('/welcome');
  $session = \Drupal::request()->getSession();
  $session->set('initialSession', 'initial');

  if ($session->get('ageGate') == 'passed') {
    $session->remove('initialSession');
  }
  if ($route_name != 'agegate.route' && $session->get('initialSession') == 'initial') {
    $response->send();
  } 
  if ($route_name == 'agegate.route' && $session->get('ageGate') == 'passed') {
    $homeRoute = new\Symfony\Component\HttpFoundation\RedirectResponse('/');

    $homeRoute->send();
  }
}

Next, I created a custom module for the age gate and the routing.yml looks like the following.

agegate.route:
  path: '/welcome'
  defaults:
    _controller: '\Drupal\agegate\Controller\AgeGateController::content'
    _form: '\Drupal\agegate\Form\AgeGateForm'
  requirements:
    _permission: 'access content'

The AgeGateController class renders a form.

class AgeGateController extends ControllerBase {
  public function content(){
    return \Drupal::formBuilder()->getForm('Drupal\agegate\Form\AgeGateForm');
  }
}

It was all working fine until I realized that, if I am logged and I try to access the front page, I get redirected infinite times!

So, for some reason, my code only works on users that aren't logged-in.

How can I create this age gate functionality?

  • 1
    I don't think this belongs in a theme and you shouldn't send responses from anywhere in custom code, which usually results in unpredictable behavior. You send the redirect headers prematurely without that Drupal knows of it and continues building its own response which can't be delivered because the client is already somewhere else but still gets cached and will be re-used in future requests. – 4k4 Oct 8 at 9:07
1

Implement the redirect in a module. Either in a kernel request subscriber, see this change record https://www.drupal.org/node/2013014 or in a kernel response subscriber, see https://git.drupalcode.org/project/age_verification/blob/8.x/src/EventSubscriber/pathGate.php.

The second example is from a contrib module you can try as it is. I don't have first-hand experience with this module, so I can't recommend or not recommend it, but this would be off-topic here anyway.

0

As others have pointed out, this logic belongs in a module, not in the theme.

In D7, you could do something like this using hook_boot() and hook_init(), but in D8 you'll use a route subscriber.

This article may be helpful:

https://chromatichq.com/blog/replacing-hookboot-and-hookinit-functionality-drupal-8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.