You are here

public function BasicTitleSearch::getResults in Fast Autocomplete 8

Return the results for the given key.

Parameters

\Drupal\fac\FacConfigInterface $fac_config: The FacConfig object to get the search results for.

string $langcode: The language code.

string $key: The query string to get results for.

Return value

array The result entity ids for the given key.

Overrides SearchBase::getResults

File

src/Plugin/Search/BasicTitleSearch.php, line 160

Class

BasicTitleSearch
Provides a basic node title search plugin.

Namespace

Drupal\fac\Plugin\Search

Code

public function getResults(FacConfigInterface $fac_config, $langcode, $key) {
  $results = [];
  $plugin_config = $fac_config
    ->getSearchPluginConfig();
  $query = $this->database
    ->select('node', 'n');
  $query
    ->fields('n', [
    'nid',
  ])
    ->condition('fd.title', '%' . $this->database
    ->escapeLike($key) . '%', 'LIKE')
    ->condition('fd.status', NodeInterface::PUBLISHED, '=');
  if ($plugin_config['language_filter']) {
    $langcodes = array_filter($plugin_config['langcode_includes']);
    $langcodes[] = $langcode;
    $query
      ->condition('n.langcode', $langcodes, 'IN');
  }
  if ($plugin_config['bundle_filter']) {
    $query
      ->condition('n.type', $plugin_config['bundle_filter'], 'IN');
  }
  $query
    ->orderBy('fd.title', 'asc')
    ->addTag('node_access')
    ->range(0, $fac_config
    ->getNumberOfResults());
  $query
    ->join('node_field_data', 'fd', 'n.vid = fd.vid');
  $entity_ids = $query
    ->execute()
    ->fetchCol();
  foreach ($entity_ids as $entity_id) {
    $results[] = [
      'entity_type' => 'node',
      'entity_id' => $entity_id,
    ];
  }
  return $results;
}