You are here

function like_and_dislike_get_votes in Like & Dislike 8

Gets the likes and dislikes for the given entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity to get votes for.

Return value

array An array containing number of likes and dislikes.

2 calls to like_and_dislike_get_votes()
LikeDislikeVoteBuilder::build in src/LikeDislikeVoteBuilder.php
Lazy builder callback for displaying like and dislike icons.
VoteController::vote in src/Controller/VoteController.php
Creates a vote for a given parameters.

File

./like_and_dislike.module, line 60
This module provides 2 voting widgets: Like and Dislike.

Code

function like_and_dislike_get_votes(EntityInterface $entity) {

  /** @var \Drupal\votingapi\VoteResultStorageInterface $vote_result_storage */
  $vote_result_storage = \Drupal::entityTypeManager()
    ->getStorage('vote_result');

  // Get like votes.
  $like = $vote_result_storage
    ->getEntityResults($entity
    ->getEntityTypeId(), $entity
    ->id(), 'like', 'vote_sum');
  $likes = !empty($like) ? (int) current($like)
    ->getValue() : 0;

  // Get dislike votes.
  $dislike = $vote_result_storage
    ->getEntityResults($entity
    ->getEntityTypeId(), $entity
    ->id(), 'dislike', 'vote_sum');
  $dislikes = !empty($dislike) ? (int) current($dislike)
    ->getValue() : 0;
  return [
    $likes,
    $dislikes,
  ];
}