Problem/Motivation

Right now, the $expire variable in PrivateTempStore.php isn't exposed anywhere, except as a parameter in user.services.yml. This gives developers the ability to override the default expiration on private tempstore collections, but it's still a one-size-fits-all value for all collections that are created.

However, there are many cases where one might want different tempstores to have different $expire values. For example, I'm using a private tempstore in a custom module to hold a list of external HTTP_REFERER values for each user who visits the site via an external link. Then, when a user fills out the contact form, those values are submitted via a hidden field so we can track our conversion rates from different sources. Naturally, we want that collection to last several months at least, since a user might find the site via an ad, but not fill out the contact form for some time afterward.

The same site might also use a private tempstore for a multi-step form, in which case it might be useful for the tempstore to only last a few hours. And so on.

What I'd really like to see is a public function setExpire($expire) in PrivateTempStore.php that lets developers manually set the expire value on a per-collection basis in their custom modules. Or perhaps an $expire argument with a default value added to the get() function that's already there.

Steps to reproduce

NA

Proposed resolution

TBD - what was the ultimate solution

Remaining tasks

Update issue summary

User interface changes

NA

API changes

TBD

Data model changes

TBD

Release notes snippet

TBD

Issue fork drupal-2883521

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

hor8tio created an issue. See original summary.

dawehner’s picture

Maybe the best solution would be to be able to pass along the expire time when you construct a PrivateTempStorage using \Drupal\user\PrivateTempStoreFactory::get. With this you don't need to change the PrivateTempStore, right, aka. don't introduce any kind of muteable changes on that.

hor8tio’s picture

@dawehner Yeah, I agree that that would be an elegant solution. I'd love to see it implemented.

I'm currently a little nervous to try to patch it myself at the moment, but I'd be happy to take a first stab at it in the coming weeks if nobody else is willing.

drclaw’s picture

Version: 8.3.2 » 8.5.x-dev
Status: Active » Needs review
FileSize
1.02 KB

Something like this?

drclaw’s picture

Forgot to document parameter in the comment :)

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jwjoshuawalker’s picture

+1 for factory accepting $expire when getting collection!

I've ran into this a few times, wishing to have different expire times per tempstore.

jwjoshuawalker’s picture

Title: PrivateTempStore's $expire variable should be changeable for each collection » SharedTempStoreFactory and PrivateTempStoreFactory $expire variable should be changeable for each collection
Version: 8.7.x-dev » 8.6.x-dev
Component: user.module » base system
FileSize
2.92 KB

Attached is patch against latest 8.6.x-dev against the Core/TempStore since user version is deprecated.

This also applies it against SharedTempStore and minor comment corrections.

Status: Needs review » Needs work
jwjoshuawalker’s picture

New patch to solve error about being compatible with deprecated user version.

dtv_rb’s picture

I would rather prefer to set the expire value per item:

$temp_store = \Drupal::service('user.private_tempstore')->get('my_module');
$temp_store->set('key', 'value', 3600);
Etroid’s picture

I think both are valuable. Allowing a default $expire for the entire collection, AND an expire value per key/value item.

sim_1’s picture

I tested the currently passing patch against a Drupal 8.7.5 installation and it worked for me. Thank you - this is a very helpful feature!

John Cook’s picture

There needs to be documentation for how to manually test this.
We also need to add automated tests.

mxr576’s picture

Version: 8.6.x-dev » 8.9.x-dev

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

baikho’s picture

Patch in #11 doesn't apply anymore on HEAD and also because of the dropped deprecations from the user module in #3097445: Remove all @deprecated code in the User module except user:name token.

See reroll with addressed remark from #12. Still needs tests as per #15.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

TMWagner’s picture

#19 does not apply cleanly to Drupal 9.2.1 for me

angrytoast’s picture

Updating the patch from #19. Currently applies cleanly to 9.3.x HEAD and will work on 9.2.x HEAD if using patch utility which can apply with fuzz.

With respect to testing. It looks like the TempStore classes themselves already have tests that cover setting with an expiration parameter:
- https://git.drupalcode.org/project/drupal/-/blob/9.3.x/core/tests/Drupal...
- https://git.drupalcode.org/project/drupal/-/blob/9.3.x/core/tests/Drupal...

So are we looking for additional tests on the TempStoreFactory classes?

Status: Needs review » Needs work

Neograph734’s picture

I just wanted to comment here that it is in fact really easy to override this in any custom module with little to no overhead. It took me some searching to find out, but I wanted to share this with you while this issue progresses.

The tempstore.private service has a backend_overridable tag, which means that it can be overridden by another service.

tempstore.private:
  class: Drupal\Core\TempStore\PrivateTempStoreFactory
  arguments: ['@keyvalue.expirable', '@lock', '@current_user', '@request_stack', '%tempstore.expire%']
  tags:
    - { name: backend_overridable }

Every custom module can create its 'own' private tempstore with a different expiry time. To have a temp store expire in 10 seconds, add this to your module's services. You can even create multiple for different types of data.

mymodule.tempstore.private:
  class: Drupal\Core\TempStore\PrivateTempStoreFactory
  arguments: ['@keyvalue.expirable', '@lock', '@current_user', '@request_stack', 10]

Obviously you now have to use your own service name instead of tempstore.private when instanciating the temp store:

$container->get('mymodule.tempstore.private')

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

akalata’s picture

Thanks @Neograph734, #25 is exactly what I was looking for!

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

selwynpolit’s picture

Maybe I am missing something but I was not able to make the expiration time change using #25. I haven't tried the patch yet, but that seems like a good way to go.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

KeyurHShah’s picture

Version: 11.x-dev » 10.1.x-dev
Assigned: Unassigned » KeyurHShah
Category: Feature request » Bug report
Status: Needs work » Needs review
FileSize
5.6 KB

Core: Drupal 10
TypeError: Drupal\Core\TempStore\SharedTempStore::__construct(): Argument #5 ($current_user) must be of type Drupal\Core\Session\AccountProxyInterface, int given, called in /core/lib/Drupal/Core/TempStore/SharedTempStoreFactory.php on line 111 in Drupal\Core\TempStore\SharedTempStore->__construct() (line 106 of core/lib/Drupal/Core/TempStore/SharedTempStore.php).

KeyurHShah’s picture

KeyurHShah’s picture

Core: Drupal 10
TypeError: Drupal\Core\TempStore\SharedTempStore::__construct(): Argument #5 ($current_user) must be of type Drupal\Core\Session\AccountProxyInterface, int given, called in /core/lib/Drupal/Core/TempStore/SharedTempStoreFactory.php on line 111 in Drupal\Core\TempStore\SharedTempStore->__construct() (line 106 of core/lib/Drupal/Core/TempStore/SharedTempStore.php).

KeyurHShah’s picture

KeyurHShah’s picture

Core: Drupal 10
TypeError: Drupal\Core\TempStore\SharedTempStore::__construct(): Argument #5 ($current_user) must be of type Drupal\Core\Session\AccountProxyInterface, int given, called in /core/lib/Drupal/Core/TempStore/SharedTempStoreFactory.php on line 111 in Drupal\Core\TempStore\SharedTempStore->__construct() (line 106 of core/lib/Drupal/Core/TempStore/SharedTempStore.php).

smustgrave’s picture

Status: Needs review » Needs work

Previously tagged for tests which are still needed.

Have not tested

angrytoast changed the visibility of the branch 2883521- to hidden.

angrytoast changed the visibility of the branch 2883521-expose-expire to hidden.

angrytoast changed the visibility of the branch 2883521-expose-expire to active.

angrytoast changed the visibility of the branch 11.x to hidden.

angrytoast’s picture

Version: 10.1.x-dev » 11.x-dev
Assigned: KeyurHShah » Unassigned
Category: Bug report » Feature request
Status: Needs work » Needs review

Setting issue back to 11.x as the active development branch and back to Feature request as this is net new functionality and not a bug.

Opened a new MR -- https://git.drupalcode.org/project/drupal/-/merge_requests/5975 -- against 11.x as it wasn't clear how to change the target of the existing MR against 9.3.x

Changes include:
- Starting from the patch state of #36
- Updates to more SharedTempStore::set related methods that are also affected by $expire
- Updated tests for Private/Shared Tempstore classes
- Typehinting for params and returns in affected code

smustgrave’s picture

Hiding patches for clarity.

Issue summary looks like it could use some love. I added the standard template but not familiar enough with the issue to fill in the missing pieces. Left TBD for ones that could be filled out, if it doesn't apply just replace with an NA.