You are here

public static function SuggestionStorage::getAutocomplete in Autocomplete Search Suggestions 8

Same name and namespace in other branches
  1. 8.2 src/SuggestionStorage.php \Drupal\suggestion\SuggestionStorage::getAutocomplete()
  2. 3.0.x src/SuggestionStorage.php \Drupal\suggestion\SuggestionStorage::getAutocomplete()

Fetch a set of suggestions.

Parameters

string $ngram: The search string.

int $atoms: The number of atoms.

int $limit: The query limit.

Return value

array An array of suggestions.

1 call to SuggestionStorage::getAutocomplete()
SuggestionController::autoComplete in src/Controller/SuggestionController.php
AJAX search autocomplete callback.

File

src/SuggestionStorage.php, line 57

Class

SuggestionStorage
Database CRUD.

Namespace

Drupal\suggestion

Code

public static function getAutocomplete($ngram = '', $atoms = 0, $limit = 100) {
  $args = [
    ':ngram' => $ngram,
    ':atoms' => (int) $atoms,
  ];
  $stmt = "\n      SELECT\n        ngram,\n        ngram\n      FROM\n        {suggestion}\n      WHERE\n        ngram LIKE :ngram\n        AND src\n        AND atoms <= :atoms\n      ORDER BY\n        density DESC,\n        ngram ASC,\n        atoms ASC\n    ";
  return db_query_range($stmt, 0, (int) $limit, $args)
    ->fetchAllKeyed();
}