Weesperstraat 61-105
1018 VN Amsterdam
The Netherlands

How to set and get data with Drupal's 'tempstore' service | Example code

02 Jun, 2021 Drupal

If you need a temporary buffer in Drupal to save data to, Drupal's tempstore service might come in handy. We used it for example in a webshop module, where users didn't have a saved address yet and also had to option to never save their address permanently (because of gdpr).

So, after some searching, the code is pretty straight forward. This example might save you some time to implement it

We implemented this in a custom Drupal webshop service, the functions are called in other code via dependency injection. So here is how to set and get your custom data with help of Drupal's tempstore:

(Be aware: the tempstore data wíll expire automatically)

  /**
   * Set data in Drupal's temp_store.
   *
   * @param array $vars
   */
  public function setMyDataInTempStore(array $vars) {
    // Get tempstore service.
    $tempstore = \Drupal::service('tempstore.private');
    // Get tempstore data we need.
    $tempstore_data = $tempstore->get('my_custom_key');
    $params = $tempstore_data->get('params');
    // Fill vars.
    $params['var_1'] = $vars['var_1'];
    $params['var_2'] = $vars['var_2'];
    // Save vars to tempstore.
    $tempstore_data->set('params', $params);
  }

  /**
   * Get data from Drupal's temp_store.
   *
   * @return array
   */
  public function getMyDataInTempStore() {
    // Get tempstore service.
    $tempstore = \Drupal::service('tempstore.private');
    // Get tempstore data we need.
    $tempstore_data = $tempstore->get('my_custom_key');
    $tempstore_params = $tempstore_data->get('params');
    // Get vars.
    $my_var_1 = $tempstore_params['var_1'];
    $my_var_2 = $tempstore_params['var_2'];
    // Return data in array.
    return [
      'var_1' => $my_var_1,
      'var_2' => $my_var_2,
    ];
  }
Drupal code Planet Drupal
Written by Joris Snoek | Jun 02, 2021
Drupal project? Let's Talk

Please tell me all about your project!
Mail , or send a message:

Got some more time?

Related content

23 Oct 2026 Drupal

Legacy code, all cleaned up

**I had been postponing Drupal coding standards fixes in Drupal distro [OpenLucius](https://www.drupal.org/project/openlucius) for far too long. There were always more interesting things to do: fixing bugs, building new features, and improving existing functionality.** Fixing hundreds of PHPCS…

Read more →

16 Oct 2026 Drupal

There’s an incredible amount of things happening in the world of Drupal and AI. It’s very exciting, but almost impossible to keep up with. I’d been planning for a while to seriously dive into agentic coding. I feel a little late to the party, but at the same time, I get the impression that many…

Read more →

28 Sep 2026 Drupal

AI as a programming partner

A request from a product owner: their Drupal website has a content type called 'Event'. The editorial team wants to display a block on the homepage showing the next three events. ## The initial request and AI prompt > “Create a Drupal block that displays the next three events, sorted by start…

Read more →

21 Sep 2026 Drupal

With new features like: @-mentions, drag-and-drop images & general settings.

Last week we released a new version of OpenLucius: a lean and fast Drupal social collaboration distribution with features like: groups, social posts, messages, group chats, stories, file -and folder management, notebooks, categories, activity streams, notifications, @-mentions, comments and…

Read more →