1
votes

typo3 9.5.8 we are implementing a newsletter subscription flow with multiple steps, on submit of the second step we query graphQL in a finisher to see if the Email is valid to subscribe or not - and we set a cookie with the "state" of the email address.

setcookie("isEmailSubscribable", ($content->data->isEmailSubscribable) ? "1" : "0", time() - 3600, "/", "x.at", false);

we have to display a message on the third step based on that "state" written into a cookie. but no matter what i try the cookie does not get set (i guess).

whats the deal with cookies and typo3? is it to late to set a cookie inside a form finisher? but if yes how could i solve this?

help is much appreciated

1

1 Answers

3
votes

Inside the Finisher:

// Set a cookie
$this->getTypoScriptFrontendController()->fe_user->setKey('ses', 'value', 'test');

// Get the cookie
$this->getTypoScriptFrontendController()->fe_user->getKey('ses', 'test');
  • ses are Session cookies
  • user This cookies require a fe_user to be logged in

Value can be an array, it will be stored serialized in the Database fe_sessions.ses_data


UPDATE:

Or you can try it with an PSR-15 Middleware: https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/RequestHandling/Index.html

In your Middleware Class you get a $request and $response and use them to set or read a cookie:

// Write a cookie
$response = $response->withAddedHeader(
    'Set-Cookie',
    $cookieName . '=' . $yourValue . '; Path=/; Max-Age=' . (time()+60*60*24*30)
);

// Read a cookie
$request->getCookieParams()[$cookieName];

You just have to check request for email address and maybe an hidden field to detect that your for was submitted.