Score:2

Why are cookies being caches in hook_prepress_node() and how can I prevent or work around this issue?

ng flag

In Drupal 9 we have a _preprocess_node() hook that loads a users cookies like:

function THEME_preprocess_node(&$variables) {


  $parameters = [];
  if (isset($_COOKIE['utm_source'])){
    $parameters[] = 'utm_source=' . $_COOKIE['utm_source'];
    $parameters['#cache']['contexts'][] = 'cookies:utm_source';
  }
  if (isset($_COOKIE['utm_medium'])){
    $parameters[] = 'utm_medium=' . $_COOKIE['utm_medium'];
    $parameters['#cache']['contexts'][] = 'cookies:utm_medium';
  }
  if (isset($_COOKIE['utm_campaign'])){
    $parameters[] = 'utm_campaign=' . $_COOKIE['utm_campaign'];
    $parameters['#cache']['contexts'][] = 'cookies:utm_campaign';
  }
  $parameters = implode('&', $parameters);

  $variables['parameters'] = '&' . $parameters;
}

In my Node template:

{{ parameters }}

But it will load the cached cookies, even from other visitors until the cache is cleared.

We're trying to load the cookie parameters into a node template but it appears that preprocess hooks are caching the data. Even with all caching modules turned off.

So, is there another way we should be checking and passing cookie data to a node template?

id flag
Which cache contexts did you use for the rendered content?
quantumized avatar
ng flag
Sorry, I'm not sure what you mean be rendered content. Could you please point me to the correct place to find the info to answer your question?
id flag
This is an almost identical question: https://www.drupal.org/project/drupal/issues/2587489
quantumized avatar
ng flag
I've added the cache tags but it's still not working for me. I've added the full function code to the original post.
id flag
I've updated my answer. According to the already-answered question, https://drupal.stackexchange.com/questions/189451/cache-in-hook-preprocess-page, that where the cache setting goes.
id flag
I suggest closing this question as a duplicate.
leymannx avatar
ne flag
Does this answer your question? [Cache in hook preprocess page](https://drupal.stackexchange.com/questions/189451/cache-in-hook-preprocess-page)
quantumized avatar
ng flag
@leymannx, yes, I've implimented the event subscriber and can properly load the cookie values now but I have not figured out how to pass those values into my node template from the event subscriber. Any ideas on this? Thanks!
Score:4
id flag

You must take into consideration render caching. In this case you must add a cache context that references the cookie. There are other questions in this forum with examples, and the big pipe module has another:

$has_big_pipe_nojs_cookie = $request->cookies->has(BigPipeStrategy::NOJS_COOKIE);
$page['#cache']['contexts'][] = 'cookies:' . BigPipeStrategy::NOJS_COOKIE;

You haven't shown all your code, but what I think you need here is something like:

$variables['#cache']['contexts'][] = 'cookies:utm_source';
sonfd avatar
in flag
Can you show an example?
id flag
I don’t know what $parameters is because we don’t see all of your code.
sonfd avatar
in flag
They are in preprocess node, so they probably need to add the context to the node display.
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.