0

I know this generally has been asked a few times before. I've gone through all those questions and answers multiple times and tried several suggested solutions. So I try to ask this again with my specific parameters to see if anyone can come up with a better solution for me.

So in my website the visitor gets to pick a region, the choice will be stored in a session.

To store and switch the session and cookie I have a controller in a custom module with a route that looks like this;

MyModule.region:
  path: 'setregion/{id}'
  defaults:
    _controller: '\Drupal\MyModule\Controller\RegionController::setRegion'
    _title: 'Set region'
  requirements:
    _permission: 'access content'
  options:
    no_cache: 'TRUE'

The setRegion method looks like this;

public function setRegion($id) {
    $session = \Drupal::service('user.private_tempstore')->get('MyModule');
    $session->set('active_region', $id);

    return $this->redirect('<front>')->send();
}

Then I need to display this Session on my theme's page template. I took some inspiration from this answer: https://drupal.stackexchange.com/a/208231/90074 and made a twig extension to display the session. This is how it looks;

class SessionTwigExtension extends \Twig_Extension {

  public function getName() {
    return 'session_var';
  }

  public function getFunctions() {
    return array(
      new \Twig_SimpleFunction('session_var',
        array($this, 'session_var'),
        array('is_safe' => array('html')
      )),
    );
  }

  public function session_var($key) {
    $session = \Drupal::service('user.private_tempstore')->get('MyModule');
    return $session->get($key);
  }

}

Lastly in my page.html.twig I print out the session like this;

{{ session_var('active_region') }}

All this seems to work fine on my development platform while I'm logged in to the administration and the cache is turned off. Once I log out and cache is running again it stops working. So where have I gone wrong? Somewhere it has to run some cached version of that session value.

Note: I've also tried to use symfony Session object and the $_SESSION array instead of the drupal service. Also tried to display the session with {{ app.session.get('active_region') }}

Edit: Adjusted the code and deleted everything that had to do with the cookie. My problem is with the session not the cookie.

  • 3
    Cookies don't work for anonymous traffic unless you uninstall the Internal Page Cache. The private tempstore is not a session as the variable name suggests. So you have tried three ways, cookie, session and private tempstore, which makes the question a bit too broad to be answered. And don't forget to add #cache data, which would be another issue. – 4k4 Apr 3 at 11:50
  • I deleted everything that had to do with the cookie, that was not my issue really, I just need a session value, that is being set with the setRegion method, be shown in my footer on every page. How can this be done with page cache turned on? – netla5 Apr 3 at 13:02
  • See drupal.stackexchange.com/questions/278313/…, specifically the last part about lazybuilding if you want this to work with dynamic page caching. – 4k4 Apr 3 at 13:09
  • After diging into how drupal cache system works, I understand this is rather problematic to have something dynamic on a cached page. I don't want to affect the page load time by disabling cache or go halfway with dynamci page caching, so I decided to run a javascript to retreive the data instead, it's not so important this info comes to the page directly after loading it. Thanks for your help 4k4. – netla5 Apr 3 at 14:02

Your Answer

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

Browse other questions tagged or ask your own question.