Following up with my previous question, I need to set and retrieve a cookie in two difference places:
- set – inside
postSave()
in a Webform handler - get – inside an event subscriber.
In looking around, it seems that there are a few different options for tools, such as SymfonyComponentHttpFoundationResponse
and SymfonyComponentHttpFoundationCookie
, or GuzzleHttpCookieSetCookie
and GuzzleHttpCookieCookieJar
.
For setting the cookie, if I try this in my webform handler:
$values = $webform_submission->getData(); $response = new Response(); $cookie = new Cookie('Dixon customer info form', $values['destination_url'], 0, '/' , NULL, FALSE); $response->headers->setCookie($cookie); $response->send();
I get redirected to a blank page. If I try this:
$values = $webform_submission->getData(); $cookie_jar = new CookieJar(); // Get the current host. $host = Drupal::request()->getSchemeAndHttpHost(); // Set a cookie for the specific form. $cookie = new SetCookie([ 'Name' => 'formEnterCookie', 'Value' => $values['destination_url'], 'Domain' => $host, 'Secure' => FALSE, ]); $cookie_jar->setCookie($cookie);
no cookie gets set. Using either option, how do I set my cookie within my webform handler?