By editor, 18 November, 2015 Question How to set and get data using $_SESSION on Drupal 8? Store data on session. Example: $session = new \Symfony\Component\HttpFoundation\Session\Session(); $session->set('var_name', $session->get('var_name',0) + 1); drupal_set_message($session->get('var_name')); This is similar to : $_SESSION['mymodule']['test'] = "Just for Test"; $data = $_SESSION['mymodule']['test']; To save data on configuration database. To set data: $session = \Drupal::service('user.private_tempstore')->get('mymodule'); $session->set('test', "Just for Test"); To get data: $session = \Drupal::service('user.private_tempstore')->get('mymodule'); $data = $session->get('test'); Tags Drupal 8 Code User Add new comment Comments1 You must have JavaScript enabled to use this form. Your name Subject Comment About text formats Plain text No HTML tags allowed. Lines and paragraphs break automatically. Web page addresses and email addresses turn into links automatically. Works :) In my project I've also added : $session->delete('mymodule'); to destroy that data after being retrieved to prevent being reused. Great stuff, Drupal 8 rulez ! Reply
Works :) In my project I've also added : $session->delete('mymodule'); to destroy that data after being retrieved to prevent being reused. Great stuff, Drupal 8 rulez ! Reply
Comments1
Works :)
In my project I've also added :
$session->delete('mymodule');
to destroy that data after being retrieved to prevent being reused.
Great stuff, Drupal 8 rulez !