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
Comment | File | Size | Author |
---|
Issue fork drupal-2883521
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:
Comments
Comment #2
dawehnerMaybe 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 thePrivateTempStore
, right, aka. don't introduce any kind of muteable changes on that.Comment #3
hor8tio CreditAttribution: hor8tio commented@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.
Comment #4
drclaw CreditAttribution: drclaw at Fuse Interactive commentedSomething like this?
Comment #5
drclaw CreditAttribution: drclaw at Fuse Interactive commentedForgot to document parameter in the comment :)
Comment #8
jwjoshuawalker CreditAttribution: jwjoshuawalker as a volunteer commented+1 for factory accepting
$expire
when getting collection!I've ran into this a few times, wishing to have different expire times per tempstore.
Comment #9
jwjoshuawalker CreditAttribution: jwjoshuawalker as a volunteer commentedAttached 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.
Comment #11
jwjoshuawalker CreditAttribution: jwjoshuawalker as a volunteer commentedNew patch to solve error about being compatible with deprecated user version.
Comment #12
dtv_rb CreditAttribution: dtv_rb commentedI would rather prefer to set the expire value per item:
Comment #13
Etroid CreditAttribution: Etroid commentedI think both are valuable. Allowing a default $expire for the entire collection, AND an expire value per key/value item.
Comment #14
sim_1I 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!
Comment #15
John Cook CreditAttribution: John Cook commentedThere needs to be documentation for how to manually test this.
We also need to add automated tests.
Comment #16
mxr576Comment #19
baikhoPatch 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.
Comment #21
TMWagner CreditAttribution: TMWagner at Tableau commented#19 does not apply cleanly to Drupal 9.2.1 for me
Comment #22
angrytoast CreditAttribution: angrytoast at Tableau commentedUpdating 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?
Comment #25
Neograph734I 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 abackend_overridable
tag, which means that it can be overridden by another service.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.
Obviously you now have to use your own service name instead of
tempstore.private
when instanciating the temp store:$container->get('mymodule.tempstore.private')
Comment #28
akalata CreditAttribution: akalata at Tag1 Consulting commentedThanks @Neograph734, #25 is exactly what I was looking for!
Comment #30
selwynpolit CreditAttribution: selwynpolit commentedMaybe 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.
Comment #32
KeyurHShah CreditAttribution: KeyurHShah commentedCore: 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).
Comment #33
KeyurHShah CreditAttribution: KeyurHShah commentedComment #34
KeyurHShah CreditAttribution: KeyurHShah commentedCore: 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).
Comment #35
KeyurHShah CreditAttribution: KeyurHShah commentedComment #36
KeyurHShah CreditAttribution: KeyurHShah commentedCore: 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).
Comment #37
smustgrave CreditAttribution: smustgrave at Mobomo commentedPreviously tagged for tests which are still needed.
Have not tested
Comment #43
angrytoast CreditAttribution: angrytoast as a volunteer and at Tableau commentedSetting 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
Comment #44
smustgrave CreditAttribution: smustgrave at Mobomo commentedHiding 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.