public function SuggestionFactory::createFromSuggestedKeys in Search API Autocomplete 8
Creates a suggestion based on the suggested keywords.
Parameters
string $suggested_keys: The suggested keywords.
int|null $results_count: (optional) The estimated number of results.
Return value
\Drupal\search_api_autocomplete\Suggestion\SuggestionInterface An autocomplete suggestion.
File
- src/
Suggestion/ SuggestionFactory.php, line 42
Class
- SuggestionFactory
- Provides factory methods for simpler creation of autocomplete suggestions.
Namespace
Drupal\search_api_autocomplete\SuggestionCode
public function createFromSuggestedKeys($suggested_keys, $results_count = NULL) {
$suggestion = new Suggestion($suggested_keys);
$lowercase_input = mb_strtolower($this->userInput);
$lowercase_keys = mb_strtolower($suggested_keys);
$start_position = mb_strpos($lowercase_keys, $lowercase_input);
if ($start_position === FALSE) {
$suggestion
->setLabel($suggested_keys);
}
else {
if ($start_position) {
$prefix = mb_substr($suggested_keys, 0, $start_position);
$suggestion
->setSuggestionPrefix($prefix);
}
$input_length = mb_strlen($this->userInput);
$end_position = $start_position + $input_length;
if ($end_position < mb_strlen($suggested_keys)) {
$suffix = mb_substr($suggested_keys, $end_position);
$suggestion
->setSuggestionSuffix($suffix);
}
$suggestion
->setUserInput(mb_substr($suggested_keys, $start_position, $input_length));
}
if ($results_count !== NULL) {
$suggestion
->setResultsCount($results_count);
}
return $suggestion;
}