public function RateWidgetBase::getEntityForVoting in Rate 8.2
Returns a Vote entity.
Checks whether a vote was already done and if this vote should be reused instead of adding a new one.
Return value
\Drupal\votingapi\Entity\Vote The vote entity.
1 call to RateWidgetBase::getEntityForVoting()
- RateWidgetBase::getForm in src/
Plugin/ RateWidgetBase.php - Gets the widget form as configured for given parameters.
File
- src/
Plugin/ RateWidgetBase.php, line 251
Class
- RateWidgetBase
- Base class for Rate widget plugins.
Namespace
Drupal\rate\PluginCode
public function getEntityForVoting($entity_type, $entity_bundle, $entity_id, $vote_type, $value_type, $rate_widget, $settings, $user_id) {
$storage = $this->entityTypeManager
->getStorage('vote');
$vote_data = [
'entity_type' => $entity_type,
'entity_id' => $entity_id,
'type' => $vote_type,
'value_type' => $value_type,
'rate_widget' => $rate_widget,
];
$vote_data['user_id'] = !is_null($user_id) ? $user_id : $this->account
->id();
// Give other modules a chance to alter the data for vote creation.
$this->moduleHandler
->invokeAll('rate_vote_data_alter', [
&$vote_data,
$entity_type,
$entity_bundle,
$entity_id,
$rate_widget,
$settings,
$user_id,
]);
$vote = $storage
->create($vote_data);
$voting_settings = $settings
->get('voting');
$timestamp_offset = $this
->getWindow('user_window', $entity_type, $entity_bundle, $rate_widget, $voting_settings);
if ($this->account
->isAnonymous()) {
$vote_data['vote_source'] = hash('sha256', serialize($this->requestStack
->getCurrentRequest()
->getClientIp()));
$timestamp_offset = $this
->getWindow('anonymous_window', $entity_type, $entity_bundle, $rate_widget, $voting_settings);
}
$query = $this->entityTypeManager
->getStorage('vote')
->getQuery();
foreach ($vote_data as $key => $value) {
$query
->condition($key, $value);
}
// Check if rollover is 'Immediately' or value in seconds.
if ($timestamp_offset >= 0) {
$query
->condition('timestamp', time() - $timestamp_offset, '>');
}
$votes = $query
->execute();
if ($votes && count($votes) > 0) {
$vote = $storage
->load(array_shift($votes));
}
else {
// On a new vote, set value to NULL, so we can trigger on and store zero.
if ($vote
->isNew()) {
$vote
->setValue(NULL);
}
}
return $vote;
}