Score:0

tempstore.private not working in openIDConnect client

kr flag

I am getting some licence info via retrieveUserInfo in the openIDConnect custom client login process. Ideally i want to write it to the session but opted for the "tempstore.private" since it seems to be the drupal way.

in retrieveUserInfo I am getting the

$tempstore = \Drupal::service('tempstore.private')->get('sessionlike');

.. going through the licences and detecting the right ones and then i set:

 $tempstore->set('has_plus', true);

however retrieving the value in the custom block with

 $tempstore->get('has_plus')

comes up empty.

Do I need to apply the tempstore changes somehow ? Could not find anything on that.

Jaypan avatar
de flag
You may be misdiagnosing it - your block may be cached. Try setting the value in the tempstore, clearing the cache/registry, then refreshing the page with the block to see if the value shows correct.
kr flag
sadly the whole thing is running in debug mode now. And there is no caching. I am suspecting that the session might geht reset AFTER the retrieveUserInfo because it is a login after all.
Score:1
cn flag

Ideally i want to write it to the session but opted for the "tempstore.private" since it seems to be the drupal way.

I think you've read some old blog post advertising Tempstore as replacement for the superglobal $_SESSION. They were wrong, it was replaced by the Symfony Session object.

To make the right choice between Tempstore and Session you need to consider the use case. See What is the difference between \Drupal::request()->getSession() and \Drupal::service('user.private_tempstore')?

The best Drupal way performance-wise would be adding a session cache context to the block. Blocks with this cache context are automatically lazy-loaded through BigPipe.

public function build() {
  $session = \Drupal::request()->getSession();
  $build = [
   '#plain_text' => $session->get('value'),
  ];
  $build['#cache']['contexts'][] = 'session';
  return $build;
}

This also helps with your comment, session data does survive a user login.

kr flag
That does not answer my question but thanks, i will try using the session.
4uk4 avatar
cn flag
I've added more context why I've suggested this solution.
kr flag
Thank you. Now this a complete answer. Due to your suggestion i found the other answer as well and was about to recommend you add it. I will accept this now.
I sit in a Tesla and translated this thread with Ai:

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.