You are here

public function VotingApiReactionManager::lastReaction in Voting API Reaction 8

Load previous reaction of the user for certain field.

Parameters

\Drupal\votingapi\Entity\Vote $entity: Current vote entity.

array $settings: Field settings.

Return value

\Drupal\Core\Entity\EntityInterface Last reaction for current user.

File

src/VotingApiReactionManager.php, line 118

Class

VotingApiReactionManager
Manages reactions through Voting API entities.

Namespace

Drupal\votingapi_reaction

Code

public function lastReaction(Vote $entity, array $settings) {
  $query = $this->voteStorage
    ->getQuery()
    ->condition('entity_id', $entity
    ->getVotedEntityId())
    ->condition('entity_type', $entity
    ->getVotedEntityType())
    ->condition('field_name', $entity
    ->get('field_name')->value)
    ->condition('user_id', $this->currentUser
    ->id());
  if ($this->currentUser
    ->isAnonymous()) {

    // Filter by Cookie method.
    if (in_array(VotingApiReactionItemInterface::BY_COOKIES, $settings['anonymous_detection'])) {
      $query
        ->condition('id', $this
        ->recallReaction($entity));
    }

    // Filter by IP method.
    if (in_array(VotingApiReactionItemInterface::BY_IP, $settings['anonymous_detection'])) {
      $query
        ->condition('vote_source', Vote::getCurrentIp());
    }

    // Filter by rollover.
    $rollover = $settings['anonymous_rollover'];
    if ($rollover == VotingApiReactionItemInterface::VOTINGAPI_ROLLOVER) {
      $rollover = $this->configFactory
        ->get('votingapi.settings')
        ->get('anonymous_window');
    }
    if ($rollover != VotingApiReactionItemInterface::NEVER_ROLLOVER) {
      $query
        ->condition('timestamp', time() - $rollover, '>=');
    }
  }
  $ids = $query
    ->execute();
  return $this->voteStorage
    ->load(intval(array_pop($ids)));
}