Showing posts with label session data. Show all posts
Showing posts with label session data. Show all posts

Thursday 28 November 2019

How to store temporary data in drupal 8?

Drupal 8, we have service's for store temporary data by using private_tempstore or getSession() for user data. It's simple to use store and pass the data from one place to another. Here we are discussing about two popular way to store and pass data with 'Session' and 'tempStore'.

Two different things to store, So both have different behaviors, we are discussing, where we will use and how we will use. In past time we just use '$_SESSION' for temporary data storage. But now in drupal 8 we have Service's: 'user.private_tempstore' and 'user.shared_tempstore'.

If we have to store and pass the big amount of data like multiple values or array then we will use 'user.private_tempstore', and in case we just have to store and pass the one variable or small values then we can normally use '$session'.

Private_tempstore:
This will work/Store data in duration of session time.

Use this library for private_tempstore on top of the page..
use Drupal\Core\TempStore\PrivateTempStoreFactory;

// Set a variable in the tempstore
$tempstore = \Drupal::service('user.private_tempstore')->get('yourmodule_name');
$tempstore->set('any_variable_name', $value); // store the temporary data in 'any_variable_name' variable.

// Read the variable in the tempstore
$tempstore = \Drupal::service('user.private_tempstore')->get('yourmodule_name');
$some_data = $tempstore->get('any_variable_name'); // Read the temporary variable.

Session:
A private tempstore is a kind of storage for large amount of data, big to have it preloaded in memory.
If you want to store a small amount of data, then use session. You find the session attached to the request object:
Use $session->set() and $session->get() for store:

Use this library for session on the top of the page..
use Symfony\Component\HttpFoundation\Request;

$session = \Drupal::request()->getSession();

if you want to set/store some value in session.
$session->set('myvariable', $value); //$value is any value

To retrive or get the session variable.
$detail = $session->get('myvariable');


$session = \Drupal::request()->getSession();

Example:
  $temp_array['username'] = $form_state->getValue('username');
  $temp_array['password'] = $form_state->getValue('password');
  $temp_array['otp'] = $form_state->getValue('otp');
  $temp_array['email'] = $form_state->getValue('email');

  $session = \Drupal::service('user.private_tempstore')->get('my_module_name');
  $session->set('temp_details', $temp_array); // for store temporary data
 
  $session->get('temp_details'); // for retrieve temporary data
 
Example:
    $session = $this->getRequest()->getSession();
    $session->set('temp_details', $value); //set data

    $session = $this->getRequest()->getSession();
    $session->get('temp_details', []);    // get data
   
     // make sure the form is not cached and this code is run on every request
    $form['#cache']['max-age'] = 0;
  
Here we don't need to start the session. This is done by Drupal in the background for each request.    When completing the response, Drupal checks if there is new session data and then starts a session to store the data.


Note: How to store temporary data in drupal 8?

How to resolve max execution time error in drupal ?

When you found error regarding 'max_execution_time' exceed, then you can follow steps for resolve this error: Steps:   You can put t...