VotingApiController.php in Vote Up/Down 8
File
src/Controller/VotingApiController.php
View source
<?php
namespace Drupal\vud\Controller;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultAllowed;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Controller\ControllerBase;
use Drupal\votingapi\Entity\Vote;
use Drupal\votingapi\Entity\VoteType;
use Symfony\Component\HttpFoundation\RedirectResponse;
class VotingApiController extends ControllerBase {
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());
}
public function resetVote($entity_type_id, $entity_id, $widget_name, $js) {
$entity = $this
->entityTypeManager()
->getStorage($entity_type_id)
->load($entity_id);
$widget = \Drupal::service('plugin.manager.vud')
->createInstance($widget_name);
$voteTypeId = \Drupal::config('vud.settings')
->get('tag', 'vote');
$vote_storage = $this
->entityTypeManager()
->getStorage('vote');
$vote_storage
->deleteUserVotes($this
->currentUser()
->id(), $voteTypeId, $entity_type_id, $entity_id);
$this
->entityTypeManager()
->getViewBuilder($entity_type_id)
->resetCache([
$entity,
]);
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());
}
public function voteAccess() {
if (!vud_can_vote($this
->currentUser())) {
return AccessResult::forbidden();
}
else {
return AccessResultAllowed::allowed();
}
}
public function resetVoteAccess() {
if (!vud_can_reset_vote($this
->currentUser())) {
return AccessResult::forbidden();
}
else {
return AccessResultAllowed::allowed();
}
}
}