You are here

protected function NodeMatcher::buildEntityQuery in Linkit 8.5

Same name and namespace in other branches
  1. 8.4 src/Plugin/Linkit/Matcher/NodeMatcher.php \Drupal\linkit\Plugin\Linkit\Matcher\NodeMatcher::buildEntityQuery()

Builds an EntityQuery to get entities.

Parameters

string $search_string: Text to match the label against.

Return value

\Drupal\Core\Entity\Query\QueryInterface The EntityQuery object with the basic conditions and sorting applied to it.

Overrides EntityMatcher::buildEntityQuery

File

src/Plugin/Linkit/Matcher/NodeMatcher.php, line 85

Class

NodeMatcher
Provides specific linkit matchers for the node entity type.

Namespace

Drupal\linkit\Plugin\Linkit\Matcher

Code

protected function buildEntityQuery($search_string) {
  $query = parent::buildEntityQuery($search_string);
  if ($this->configuration['include_unpublished'] == FALSE) {
    $query
      ->condition('status', NodeInterface::PUBLISHED);
  }
  elseif (count($this->moduleHandler
    ->getImplementations('node_grants')) === 0) {
    if ($this->currentUser
      ->hasPermission('bypass node access') || $this->currentUser
      ->hasPermission('view any unpublished content')) {

      // User can see all content, no check necessary.
    }
    elseif ($this->currentUser
      ->hasPermission('view own unpublished content')) {

      // Users with "view own unpublished content" can see only their own.
      if ($this->configuration['include_unpublished'] == TRUE) {
        $or_condition = $query
          ->orConditionGroup()
          ->condition('status', NodeInterface::PUBLISHED)
          ->condition('uid', $this->currentUser
          ->id());
        $query
          ->condition($or_condition);
      }
    }
  }
  else {

    // All other users should only get published results.
    $query
      ->condition('status', NodeInterface::PUBLISHED);
  }
  return $query;
}