public function RateEntityVoteWidget::buildRateVotingWidget in Rate 8
Returns a renderable array of the updated vote totals.
Parameters
string $entity_id: The entity id.
string $entity_type_id: The entity type id.
string $bundle: The bundle id.
string|null $widget_type: (optional) The widget type. If NULL, it will use value from config.
Return value
array A renderable array.
File
- src/
RateEntityVoteWidget.php, line 81
Class
- RateEntityVoteWidget
- The rate.entity.vote_widget service.
Namespace
Drupal\rateCode
public function buildRateVotingWidget($entity_id, $entity_type_id, $bundle, $widget_type = NULL) {
$output = [];
$enabled_types_widgets = $this->config
->get('enabled_types_widgets');
if (isset($enabled_types_widgets[$entity_type_id]) && in_array($bundle, array_keys($enabled_types_widgets[$entity_type_id]))) {
if (empty($widget_type)) {
$widget_type = $enabled_types_widgets[$entity_type_id][$bundle]['widget_type'];
}
$rate_theme = 'rate_template_' . $widget_type;
$use_ajax = $this->config
->get('use_ajax');
/** @var \Drupal\votingapi\VoteStorageInterface $vote_storage */
$vote_storage = $this->entityTypeManager
->getStorage('vote');
$vote_ids = $vote_storage
->getUserVotes($this->accountProxy
->id(), NULL, $entity_type_id, $entity_id);
$has_voted = !empty($vote_ids) ? TRUE : FALSE;
$user_can_vote = $this->accountProxy
->hasPermission('cast rate vote on ' . $entity_type_id . ' of ' . $bundle);
// Get the current user voted.
$user_voted = NULL;
if ($has_voted && ($vote_id = reset($vote_ids))) {
/** @var \Drupal\votingapi\Entity\Vote $vote */
if ($vote = Vote::load($vote_id)) {
$user_voted = $vote
->getValue();
}
}
// Set the theme variables.
$output['rate_vote_widget'] = [
'#theme' => $rate_theme,
'#results' => $this->resultManager
->getResults($entity_type_id, $entity_id),
'#use_ajax' => $use_ajax,
'#can_vote' => $user_can_vote,
'#has_voted' => $has_voted,
'#user_voted' => $user_voted,
'#entity_id' => $entity_id,
'#entity_type_id' => $entity_type_id,
'#attributes' => [
'class' => [
'links',
'inline',
],
],
'#widget_type' => $widget_type,
'#cache' => [
'contexts' => [
'user',
],
'tags' => [
'vote:' . $bundle . ':' . $entity_id,
],
],
];
}
return $output;
}