Problem/Motivation

The poll-results.html.twig contains a file docblock with the available variables, among which the 'vote':

vote: The choice number of the current user's vote.

However, this value is always NULL.

Proposed resolution

Add the current user's vote choice id to the poll-results.html.twig 'vote' variable.

Remaining tasks

  1. Write a patch
  2. Review
  3. Commit

User interface changes

None.

API changes

None.

Data model changes

None.

Comments

idebr created an issue. See original summary.

idebr’s picture

Status: Active » Needs review
Issue tags: +Needs tests
StatusFileSize
new872 bytes

Attached patch adds the current user's vote to the poll-results.html.twig

I will try to add some test coverage at a later time.

berdir’s picture

Status: Needs review » Needs work

It is also called in template_preprocess_poll_meter() now, can we update that to avoid calling the results multiple times? I only just now realized that the other one does call it for each option, which is not optimal :-/ Alternatively, there's a separate issue to add static caching to that method, that would be fine too then.

idebr’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new3.37 KB
new3.37 KB
new4.22 KB

#3 Lendude is implementing static caching in #3094531: Statically cache PollVoteStorage::getUserVote().

Attached patch adds test coverage.

idebr’s picture

StatusFileSize
new417 bytes
new3.36 KB
new4.21 KB

Fixed the @group annotation to the correct project.

lendude’s picture

+++ b/src/Form/PollViewForm.php
@@ -271,7 +275,7 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
+      '#vote' => $user_vote['chid'] ?? NULL,

Think we need to wait for 8.6 to go unsupported before we can ??

berdir’s picture

Status: Needs review » Needs work
  1. +++ b/tests/src/Kernel/PollKernelTestBase.php
    @@ -0,0 +1,75 @@
    +   * @param int $choice_count
    +   *   (optional) The number of choices to generate. Defaults to 7.
    +   *
    +   * @return \Drupal\poll\PollInterface
    +   *   A poll.
    +   */
    +  public function createPoll($choice_count = 7) {
    +    /** @var \Drupal\poll\PollInterface $poll */
    +    $poll = Poll::create([
    

    is 7 the same as the functional test? seems a bit overkill especially since we're not ever going to bother with testing how it looks in the UI or so, so maybe default to 2 or 3 options?

  2. +++ b/tests/src/Kernel/PollKernelTestBase.php
    @@ -0,0 +1,75 @@
    +   * Saves a vote for a given poll.
    +   */
    +  public function saveVote(PollInterface $poll, $choice_id) {
    +    $options = [];
    +    $options['chid'] = $choice_id;
    +    $options['uid'] = \Drupal::currentUser()->id();
    +    $options['pid'] = $poll->id();
    +    $options['hostname'] = \Drupal::request()->getClientIp();
    +    $options['timestamp'] = \Drupal::time()->getRequestTime();
    +    /** @var \Drupal\poll\PollVoteStorage $vote_storage */
    +    $vote_storage = \Drupal::service('poll_vote.storage');
    +    $vote_storage->saveVote($options);
    +  }
    +
    

    relying on the current user in a kernel test is a bit strange, can't we just pass in a user id? then we can skip the current user set up in the test.

  3. +++ b/tests/src/Kernel/PollResultsTest.php
    @@ -0,0 +1,43 @@
    +
    +    $choice_id = $poll->get('choice')->first()->getValue();
    +    $this->saveVote($poll, $choice_id['target_id']);
    

    $poll->get('choice')->target_id is the same and short enough to be inlined with the saveVote() call IMHO.

    Relying on __get() isn't great, but on the field level, there is no good alternative right now, we specifically optimized that, compared to getValue(), which is awkward to use and get('target_id')->getValue(), which has to create another object.

idebr’s picture

Status: Needs work » Needs review
StatusFileSize
new2.14 KB
new3.1 KB
new3.98 KB

#6 Restored the isset() implementation for backwards compatibility.

#7.1 7 choices matches the functional test. I reduced the default number to 2 for now.

#7.2 Removed the current user setup from the Kernel test

#7.3 Updated the code calling the choice target id to use $poll->get('choice')->target_id

lendude’s picture

Status: Needs review » Reviewed & tested by the community

All feedback addressed, looks good!

  • Berdir committed ea18d38 on 8.x-1.x authored by idebr
    Issue #3096227 by idebr: poll-results.html.twig documents 'vote' as the...
berdir’s picture

Status: Reviewed & tested by the community » Fixed

Yes, looks good. I realized in the other issue that poll vote storage does rely on the current user, so setting it up like that in general makes sense, but it defaults to 0, so we don't need it for that.

Committed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.