You are here

public function VotingApiController::resetVote in Vote Up/Down 8

Reset a vote.

Parameters

$entity_id: EntityId of the referenced entity

$entity_type_id: EntityTypeId of the referenced entity

$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::resetVote'
vud.routing.yml in ./vud.routing.yml
vud.routing.yml

File

src/Controller/VotingApiController.php, line 103

Class

VotingApiController
Controller for voting.

Namespace

Drupal\vud\Controller

Code

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());
}