You are here

public function PollVoteStorage::cancelVote in Poll 8

Cancel a user's vote.

Parameters

PollInterface $poll:

AccountInterface $account:

Overrides PollVoteStorageInterface::cancelVote

File

src/PollVoteStorage.php, line 74

Class

PollVoteStorage
Controller class for poll vote storage.

Namespace

Drupal\poll

Code

public function cancelVote(PollInterface $poll, AccountInterface $account = NULL) {
  if ($account
    ->id()) {
    $this->connection
      ->delete('poll_vote')
      ->condition('pid', $poll
      ->id())
      ->condition('uid', $account
      ->id())
      ->execute();
  }
  else {
    $this->connection
      ->delete('poll_vote')
      ->condition('pid', $poll
      ->id())
      ->condition('uid', \Drupal::currentUser()
      ->id())
      ->condition('hostname', \Drupal::request()
      ->getClientIp())
      ->execute();
  }

  // Deleting a vote means that any cached vote might not be updated in the
  // UI, so we need to invalidate them all.
  $this->cacheTagsInvalidator
    ->invalidateTags([
    'poll-votes:' . $poll
      ->id(),
  ]);

  // Invalidate the static cache of votes.
  $this->currentUserVote = [];
}