public function RateVote::undoVote in Rate 8
Undo a vote.
Parameters
string $entity_type_id: Entity type ID such as node.
int $entity_id: Entity id of the entity type.
bool $show_messages: If TRUE, standard Drupal message will be set.
File
- src/
RateVote.php, line 158
Class
- RateVote
- Returns responses for Rate routes.
Namespace
Drupal\rateCode
public function undoVote($entity_type_id, $entity_id, $show_messages = TRUE) {
$entity = $this->entityTypeManager
->getStorage($entity_type_id)
->load($entity_id);
$is_bot_vote = $this->botDetector
->checkIsBot();
if (!$is_bot_vote && $this->accountProxy
->hasPermission('cast rate vote on ' . $entity_type_id . ' of ' . $entity
->bundle())) {
$vote_storage = $this->entityTypeManager
->getStorage('vote');
$vote_result = $vote_storage
->getUserVotes($this->accountProxy
->id(), NULL, $entity_type_id, $entity_id);
// If a vote has been found, remove it.
if (!empty($vote_result)) {
$vote_ids = array_keys($vote_result);
$vote_id = array_pop($vote_ids);
$vote = $vote_storage
->load($vote_id);
if ($vote) {
$vote
->delete();
}
if ($show_messages) {
$this->messenger
->addStatus($this
->t('Your vote was canceled.'));
}
}
elseif ($show_messages) {
// Otherwise, inform user of previous vote.
$this->messenger
->addWarning($this
->t('A previous vote was not found.'));
}
}
}