Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Follow-up of #67931: UI improvement: display current user's vote, we now have at least two calls/queries when viewing the results, so we should store that.
Comment | File | Size | Author |
---|---|---|---|
#7 | 3094531-7.patch | 5.79 KB | Lendude |
#7 | interdiff-3094531-5-7.txt | 4.67 KB | Lendude |
#7 | 3094531-7-WITH_TEST_BASE.patch | 7.93 KB | Lendude |
| |||
#5 | 3094531-5.patch | 7.42 KB | Lendude |
#5 | interdiff-3094531-4-5.txt | 408 bytes | Lendude |
Comments
Comment #2
LendudeSomething like this?
The test coverage build on the kernel test base class from #3096227: poll-results.html.twig documents 'vote' as the current user's vote, but is always empty so this will fail until that lands. Might want to do it the other way around ¯\_(ツ)_/¯
This does take into account the current user switching during the request (unlikely, but who knows), but not the vote changing during the request (even less likely I think)
Comment #3
Berdir> but not the vote changing during the request (even less likely I think)
Might not be that uncommon? Isn't submitting an ajax vote request that results in rendering the results exactly that? first submission builds the form, checks the vote, shows the form, submits that, rebuilds the form and then it needs to show the results.
But that should be easy to handle, just unset the statically cached results when you save the vote in the same service.
Comment #4
LendudeHmm the vote scenario doesn't fail, but when using the 'cancel vote' button it does! Nice catch.
This now unsets the static cache on save/cancel/delete. Expanded the javascript test to also press the cancel button, which fails nicely with #2
Comment #5
LendudeRemoved PHP7 code
Comment #6
Berdirresetting it to NULL seems strange, that's not the correct type according to the @var, but neither is it initialized to an array.
I'd initialize and reset it to [].
I'm not sure these two helper methods are necessary, means the logic for the key is actually in two places, if we inline it in getUserVote() then it's just in one place.
Also, it's pretty theoretical, but the static cache doesn't consider the hostname yet for an anonymous user, it would be weird but not impossible to e.g. alter the global request object to read values of different IP's or something like that ;)
People have different opinions on early return, but I like it, especially in such a static-cache pattern. Would also make the patch much more readable, as only needs to make minimal changes in the existing code.
So I'd do something like this:
Hm. In other issue I commented on going through current user being a bit strange, but I forgot about poll vote storage using that too, so I think that makes sense. Ignore that part then over there.
Still, the choice_id can be simplified to $poll->get('choice')->get(6)->target_id (or $poll->get('choice')[6]->target_id, but get() + array access looks a bit strange). and with the suggestion in the other issue to only create 2-3 choices by default, you'd need to make it 1 or 2 I suppose.
->delete('poll_vote')->execute()? Also, query() already executes the query, so that should actually be a fatal, which we don't see yet without the base class.
Comment #7
Lendude#6.1 fixed
#6.2 was over thinking stuff, yes this is much cleaner
#6.3 fixed
#6.4 ->execute() doesn't fatal, it also doesn't do anything else :), changed to ->delete()
Adding a version with the kernel test base included so we can see the test run
Comment #8
Berdirget(2) didn't work anymore as the base class now defaults to only 2 choices, changed it to get(1), then the test passed, committed with that.