2

I wondering how to access to session in twig ?

In my Controller I have this part of code :

   $tempstore = \Drupal::service('user.private_tempstore')->get('mdm_user');
   $tempstore->set('num_donateur', $num_donateur);

I tried to display the session in my twig template :

  {% set mdm_session = app.session.get('mdm_user') %}
  {{ dump(mdm_session) }}

But nothing is displayed. Even session is inaccessible.

  {{ dump(session) }}

Any help would be appreciated.

2
  • This should clear it up
    – Clive
    Mar 24, 2016 at 10:52
  • Hi ! Thanks for your answer. I tested 2 solutions, but in the drupal controller I don't access to $this ($this->get('session')), and add global in twigEnvironment doesn't work. ($twig->addGlobal('session', $_SESSION);)
    – Debbz
    Mar 24, 2016 at 11:26

2 Answers 2

0

I would suggest you to firstly check if the data is stored in num_donateur variable successfully using $tempstore->get('num_donateur')

If not stored then try to save it using \Drupal::service('tempstore.private'), As i have just googled it and seen user.private_tempstore is deprecated [ https://www.drupal.org/project/entity/issues/2951683 ]

If data is stored successfully then use code to print the variable

{% set mdm_session = app.session.get('mdm_user') %} {{ mdm_session }}

-1

Try this:

{% set varname = app.session.get('varname') %}

This is the session value {{varname}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.