You are here

public static function SuggestionStorage::getAutocomplete in Autocomplete Search Suggestions 3.0.x

Same name and namespace in other branches
  1. 8.2 src/SuggestionStorage.php \Drupal\suggestion\SuggestionStorage::getAutocomplete()
  2. 8 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 \Drupal::database()
    ->queryRange($stmt, 0, (int) $limit, $args)
    ->fetchAllKeyed();
}