You are here

public static function Fivestar::userCanVote in Fivestar 8

Determines if a user can vote on content.

Parameters

array $element: Fivestar element data.

Return value

bool

1 call to Fivestar::userCanVote()
Fivestar::process in src/Element/Fivestar.php
Process callback: process fivestar element.

File

src/Element/Fivestar.php, line 254

Class

Fivestar
Provides a fivestar form element.

Namespace

Drupal\fivestar\Element

Code

public static function userCanVote(array $element) {
  if ($element['#show_static_result']) {
    return FALSE;
  }
  if ($element['#allow_revote']) {
    return TRUE;
  }

  // Check if user have votes in current entity type.
  $vote_ids = [];
  $current_user = \Drupal::currentUser();
  $entity_type = isset($element['#settings']['content_type']) ? $element['#settings']['content_type'] : NULL;
  $entity_id = isset($element['#settings']['content_id']) ? $element['#settings']['content_id'] : NULL;
  if (!$entity_type || !$entity_id) {
    $vote_ids = \Drupal::entityQuery('vote')
      ->condition('entity_type', $entity_type)
      ->condition('entity_id', $entity_id)
      ->condition('user_id', $current_user
      ->id())
      ->execute();
  }

  // If user voted before, return FALSE.
  if (empty($vote_ids)) {
    return FALSE;
  }

  // Check allowed own vote.
  if ($element['#allow_ownvote']) {
    return TRUE;
  }

  // Check that we have entity details, allow if not.
  if (!$entity_type || !$entity_id) {
    return TRUE;
  }
  $entity = \Drupal::entityTypeManager()
    ->getStorage($entity_type)
    ->load($entity_id);
  $owner_uid = $entity
    ->getOwner()
    ->id();
  return $owner_uid != $current_user
    ->id();
}