NodeSelection.php in Multi-path autocomplete 8        
                          
                  
                        
  
  
  
  
File
  lib/Drupal/mpac/Plugin/mpac/selection/NodeSelection.php
  
    View source  
  <?php
namespace Drupal\mpac\Plugin\mpac\selection;
use Drupal\mpac\Annotation\MpacSelection;
use Drupal\Core\Annotation\Translation;
use Drupal\mpac\Plugin\mpac\selection\SelectionBase;
class NodeSelection extends SelectionBase {
  public function countMatchingItems($match = NULL, $match_operator = 'CONTAINS') {
    $query = $this
      ->buildEntityQuery($match, $match_operator);
    return $query
      ->count()
      ->execute();
  }
  public function getMatchingItems($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
    if (!isset($match)) {
      return array();
    }
    $query = $this
      ->buildEntityQuery($match, $match_operator);
    if ($limit > 0) {
      $query
        ->range(0, $limit);
    }
    $result = $query
      ->execute();
    if (empty($result)) {
      return array();
    }
    $matches = array();
    
    $entities = entity_load_multiple('node', $result);
    foreach ($entities as $entity_id => $entity) {
      $matches["node/{$entity_id}"] = check_plain($entity
        ->label());
    }
    return $matches;
  }
  
  private function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
    $target_type = 'node';
    $entity_info = entity_get_info($target_type);
    $query = \Drupal::entityQuery($target_type);
    if (isset($match) && isset($entity_info['entity_keys']['label'])) {
      $query
        ->condition($entity_info['entity_keys']['label'], $match, $match_operator);
    }
    
    $query
      ->addTag('node_access');
    
    if (!user_access('bypass node access') && !count(module_implements('node_grants'))) {
      $query
        ->condition('status', NODE_PUBLISHED);
    }
    return $query;
  }
}