public function VotingApiController::vote in Vote Up/Down 8
Cast a vote.
Parameters
$entity_id: EntityId of the referenced entity
$entity_type_id: EntityTypeId of the referenced entity
$vote_value: Value of vote to be stored.
$widget_name: Widget name.
string $js: Ajax is enabled? Not working now, core bug?
Return value
\Drupal\Core\Ajax\AjaxResponse|\Symfony\Component\HttpFoundation\RedirectResponse
1 string reference to 'VotingApiController::vote'
File
- src/
Controller/ VotingApiController.php, line 39
Class
- VotingApiController
- Controller for voting.
Namespace
Drupal\vud\ControllerCode
public function vote($entity_type_id, $entity_id, $vote_value, $widget_name, $js) {
$entity = $this
->entityTypeManager()
->getStorage($entity_type_id)
->load($entity_id);
$widget = \Drupal::service('plugin.manager.vud')
->createInstance($widget_name);
$vote_storage = $this
->entityTypeManager()
->getStorage('vote');
$voteTypeId = \Drupal::config('vud.settings')
->get('tag', 'vote');
$voteType = VoteType::load($voteTypeId);
$vote_storage
->deleteUserVotes($this
->currentUser()
->id(), $voteTypeId, $entity_type_id, $entity_id);
$this
->entityTypeManager()
->getViewBuilder($entity_type_id)
->resetCache([
$entity,
]);
$vote = Vote::create([
'type' => $voteTypeId,
]);
$vote
->setVotedEntityId($entity_id);
$vote
->setVotedEntityType($entity_type_id);
$vote
->setValueType($voteType
->getValueType());
$vote
->setValue($vote_value);
$vote
->save();
$this
->entityTypeManager()
->getViewBuilder($entity_type_id)
->resetCache([
$entity,
]);
$criteria = [
'entity_type' => $entity_type_id,
'entity_id' => $entity_id,
'value_type' => $voteTypeId,
];
if ($js == 'ajax') {
$response = new AjaxResponse();
$widget_element = $widget
->build($entity);
$response
->addCommand(new ReplaceCommand("#vud-widget-{$entity_type_id}-{$entity_id}", $widget_element));
return $response;
}
return new RedirectResponse($entity
->toUrl()
->toString());
}