class VoteManager in Fivestar 8
Contains methods for managing votes.
Hierarchy
- class \Drupal\fivestar\VoteManager
Expanded class hierarchy of VoteManager
1 string reference to 'VoteManager'
1 service uses VoteManager
File
- src/
VoteManager.php, line 12
Namespace
Drupal\fivestarView source
class VoteManager {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* The vote storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $voteStorage;
/**
* Constructs a new VoteManager object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* The current user.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, AccountProxyInterface $current_user) {
$this->entityTypeManager = $entity_type_manager;
$this->currentUser = $current_user;
$this->voteStorage = $entity_type_manager
->getStorage('vote');
}
/**
* Get vote types.
*
* @return array
* An associative array with keys equal to the vote type machine ID and
* values equal to the vote type human-readable label.
*/
public function getVoteTypes() {
$options = [];
$vote_type_storage = $this->entityTypeManager
->getStorage('vote_type');
foreach ($vote_type_storage
->loadMultiple() as $vote_type) {
$options[$vote_type
->id()] = $vote_type
->label();
}
return $options;
}
/**
* Add vote.
*
* @param \Drupal\Core\Entity\FieldableEntityInterface $entity
* @param int $rating
* @param string $vote_type
* @param int|null $uid
*
* @return \Drupal\votingapi\Entity\Vote
*/
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;
}
/**
* Delete vote.
*/
public function deleteVote() {
}
/**
* Get votes by criteria.
*
* @param array $criteria
* Associative array of criteria. Keys are:
* - entity_id: The entity id.
* - entity_type: The entity type.
* - type: Vote type.
* - user_id: The user id.
* - vote_source: The vote source.
*
* @return array
* Which contain vote ids.
*/
public function getVotesByCriteria(array $criteria) {
if (empty($criteria)) {
return [];
}
return $this->voteStorage
->loadByProperties($criteria);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
VoteManager:: |
protected | property | The current user. | |
VoteManager:: |
protected | property | The entity type manager. | |
VoteManager:: |
protected | property | The vote storage. | |
VoteManager:: |
public | function | Add vote. | |
VoteManager:: |
public | function | Delete vote. | |
VoteManager:: |
public | function | Get votes by criteria. | |
VoteManager:: |
public | function | Get vote types. | |
VoteManager:: |
public | function | Constructs a new VoteManager object. |