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?

Monday 4 November 2019

How to sort data by array of category ids in drupal 8?

Sometime we have a case of sort data with array of categories So we can't use direct 'orderBy' in drupal 8 query. We have to use 'addExpression' in query. Here we have an example for better clear this article.
Example:
In a way of query:

    $products = db_query("select gfd.entity_id from node__field_category as gfd inner join node_field_data as nfd on nfd.nid=gfd.entity_id ORDER BY FIELD(gfd.field_category_target_id, 675, 676, 677), nfd.title ASC");

    OR
    Other way of Query:


    $products = \Drupal::database()->select('node__field_category', 'gfd');
    $products->join('node_field_data', 'nfd', 'nfd.nid=gfd.entity_id');       
    $products->fields('gfd', array('entity_id'));       
    $products->addExpression('FIELD(gfd.field_category_target_id, 675, 676, 677)', 'order_field');
    $products->orderBy('order_field', 'ASC');   
    $product_ids = $products->execute()->fetchAll();
   
Description: Here we are using '$products->addExpression' for add special type of sorting so we will pass the field alias and field name then category id which you want for sort data and then 'order_field' is some kind of alias and we will pass in this way for ASC or DESC:
$products->orderBy('order_field', 'ASC');
So this is the simple way to sort data by array ASC/DESC in Drupal 8 query.


Note: How to sort data by array of category ids 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...