You are here

public function VoteManager::addVote in Fivestar 8

Add vote.

Parameters

\Drupal\Core\Entity\FieldableEntityInterface $entity:

int $rating:

string $vote_type:

int|null $uid:

Return value

\Drupal\votingapi\Entity\Vote

File

src/VoteManager.php, line 77

Class

VoteManager
Contains methods for managing votes.

Namespace

Drupal\fivestar

Code

public function addVote(FieldableEntityInterface $entity, $rating, $vote_type = 'vote', $uid = NULL) {
  $uid = is_numeric($uid) ? $uid : $this->currentUser
    ->id();
  $rating = $rating > 100 ? 100 : $rating;
  $vote = $this->voteStorage
    ->create([
    'type' => $vote_type,
  ]);
  $vote
    ->setVotedEntityId($entity
    ->id());
  $vote
    ->setVotedEntityType($entity
    ->getEntityTypeId());
  $vote
    ->setOwnerId($uid);
  $vote
    ->setValue($rating);
  $vote
    ->save();
  return $vote;
}