Drupal - How do I store the values submitted in a form in the session?
In D8 the session data is provided by a symfony session object, which is attached to the request. See this comparison of D7 against D8:
Drupal 7:
function mymodule_session_counter_increment() {
if (!isset($_SESSION['mymodule_count'])) {
$_SESSION['mymodule_count'] = 0;
}
return $_SESSION['mymodule_count']++;
}
Drupal 8:
class MymoduleSessionCounter {
function increment(Request $request) {
$session = $request->getSession();
$value = $session->get('mymodule_count', 0);
$session->set('mymodule_count', $value + 1);
return $value;
}
}
Change record: Access session data through the Request object
Session management has not changed. You don't need to do anything, because Drupal starts a session for each request automatically and you can use it right away.
Examples how to get the session:
Procedural
In procedural code get the request from the static wrapper \Drupal
:
$request = \Drupal::request();
$session = $request->getSession();
$session->set('mymodule_value', $value);
Controller
In a controller you can pull the request from the route argument stack by including a typed request parameter. An example from UserController
:
public function resetPass(Request $request, $uid, $timestamp, $hash) {
...
$session = $request->getSession();
$session->set('pass_reset_hash', $hash);
$session->set('pass_reset_timeout', $timestamp);
...
}
It's not necessary to define the route parameter in the route definition. The request is always available.
Form
In a form method get the request from getRequest()
:
public function submitForm(array &$form, FormStateInterface $form_state) {
$session = $this->getRequest()->getSession();
$session->set('mymodule_form_value', $form_state->getValue('value'));
}
Private Tempstore
The private tempstore is not a replacement for sessions. It depends on the session id and it's not possible to use it without a session. So you can store the values directly in the session as well. Drupal core uses the private tempstore for big amount of data and only for authenticated users who already have a session. For anonymous users it's not easy to get a private tempstore started, because drupal uses dummy session id's until it starts a real session. If you start a private tempstore in custom code it has the wrong session id.
Drupal 8 uses Symfony's HttpFoundation Session Management, so that's where the API is documented.
If you look in core.services.yml
you can find that Drupal's wrapper is in.
session_manager:
class: Drupal\Core\Session\SessionManager
Here is an article on how to use the appropriate service to store data in the session: http://atendesigngroup.com/blog/storing-session-data-drupal-8
There are two methods described in the article, here's the first one which doesn't use dependency injection. (code is not mine)
To set temporary data:
// For "mymodule_name," any unique namespace will do.
// I'd probably use "mymodule_name" most of the time.
$tempstore = \Drupal::service('user.private_tempstore')->get('mymodule_name');
$tempstore->set('my_variable_name', $some_data);
To read temporary data:
$tempstore = \Drupal::service('user.private_tempstore')->get('mymodule_name');
$some_data = $tempstore->get('my_variable_name');