You are here

public function LeaveReviewAccessCheck::access in Reviews 1.0.x

Access check to determine if reviews are enabled and accessible.

Return value

\Drupal\Core\Access\AccessResultInterface The access result.

File

src/Access/LeaveReviewAccessCheck.php, line 37

Class

LeaveReviewAccessCheck
Checks access for displaying reviews pages.

Namespace

Drupal\reviews\Access

Code

public function access() {

  // Get the node ID being viewed.
  $node = \Drupal::routeMatch()
    ->getParameter('node');
  $config = \Drupal::config('reviews.settings');
  $reviews_enabled = $config
    ->get('reviews.enabled');

  // Check if multiple reviews are allowed and if not
  // check if the user has already reviewed this node.
  $previous_review = FALSE;
  if (!$config
    ->get('reviews.multiple') && $node instanceof \Drupal\node\NodeInterface) {
    $query = \Drupal::entityQuery('review')
      ->condition('node_id', $node
      ->id(), '=')
      ->condition('user_id', \Drupal::currentUser()
      ->id(), '=')
      ->execute();
    $previous_review = (bool) count($query);
  }

  // Return access allowed only if reviews are enabled and
  // the type (bundle) of the node being viewed is included
  // in the reviewable node types.
  return AccessResult::allowedif($reviews_enabled && !$previous_review);
}