Score:3

Is there an event related to the creation of a session cookie for anonymous users?

cn flag

I need to set an additional cookie whenever an anonymous user received a session cookie. This happens when selecting a facet in Search API for example. The specific use case is that I need an extra cookie set specifically for a third-party integration (GTranslate). hook_user_login doesn't meet the use case because the user isn't actually logged in but they do receive a session cookie upon selecting a facet. Is there some event I can subscribe to when that happens so I can inject my custom cookie?

4uk4 avatar
cn flag
I don't think there is a specific event, but you could subscribe to [KernelEvents::RESPONSE](https://api.drupal.org/api/drupal/vendor%21symfony%21http-kernel%21KernelEvents.php/constant/KernelEvents%3A%3ARESPONSE) and set a cookie in the response headers.
cn flag
@4k4: if you can flesh that out any, you should consider posting that as an answer. I'm going to give that a try today, but I'd like to be able to properly credit you for it if it works. ;)
Score:0
ru flag

You can try out an event response event subscriber. For E.g: Create a file in CUSTOM_MODULE/src/EventSubscriber/SetSessionCookieSubscriber.php

namespace Drupal\CUSTOM_MODULE\EventSubscriber;

use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;

class SetSessionCookieSubscriber implements EventSubscriberInterface {
  public function onResponse(FilterResponseEvent $event) {
    /** @var Request $request */
    $request = $event->getRequest();
    // Check if this is the right request, for example posting facets
    $session = $request->getSession();
    // Check for session values
    $response = $event->getResponse();
    /** Your custom code goes here */
    $response->headers->setCookie(new Cookie(...));
  }
  
  public static function getSubscribedEvents() {
    $events[KernelEvents::RESPONSE][] = ['onResponse'];
    return $events;
  }
}

Register it in your services file CUSTOM_MODULE.services.yml

services:
  CUSTOM_MODULE.setsessioncookie:
  class: Drupal\CUSTOM_MODULE\EventSubscriber\SetSessionCookieSubscriber
  tags:
    - { name: event_subscriber }
4uk4 avatar
cn flag
I've added my comment to the code.
Razeem Ahmad avatar
ru flag
Thanks 4K4 for the edit.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.