class SuggestionFactory in Search API Autocomplete 8
Provides factory methods for simpler creation of autocomplete suggestions.
Hierarchy
- class \Drupal\search_api_autocomplete\Suggestion\SuggestionFactory
Expanded class hierarchy of SuggestionFactory
See also
\Drupal\search_api_autocomplete\Suggestion\SuggestionInterface
4 files declare their use of SuggestionFactory
- LiveResults.php in src/
Plugin/ search_api_autocomplete/ suggester/ LiveResults.php - SuggestionFactoryTest.php in tests/
src/ Unit/ SuggestionFactoryTest.php - TestsHelper.php in src/
Tests/ TestsHelper.php - TestSuggester.php in tests/
search_api_autocomplete_test/ src/ Plugin/ search_api_autocomplete/ suggester/ TestSuggester.php
File
- src/
Suggestion/ SuggestionFactory.php, line 12
Namespace
Drupal\search_api_autocomplete\SuggestionView source
class SuggestionFactory {
/**
* The keywords input by the user so far.
*
* @var string|null
*/
protected $userInput;
/**
* Constructs a SuggestionFactory object.
*
* @param string|null $user_input
* (optional) The keywords input by the user so far.
*/
public function __construct($user_input = NULL) {
$this->userInput = $user_input;
}
/**
* Creates a suggestion based on the suggested keywords.
*
* @param string $suggested_keys
* The suggested keywords.
* @param int|null $results_count
* (optional) The estimated number of results.
*
* @return \Drupal\search_api_autocomplete\Suggestion\SuggestionInterface
* An autocomplete suggestion.
*/
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;
}
/**
* Creates a suggestion from a suggested suffix to the user input.
*
* @param string $suggestion_suffix
* The suggestion suffix.
* @param int|null $results_count
* (optional) The estimated number of results.
*
* @return \Drupal\search_api_autocomplete\Suggestion\SuggestionInterface
* An autocomplete suggestion.
*/
public function createFromSuggestionSuffix($suggestion_suffix, $results_count = NULL) {
$suggestion = new Suggestion();
$suggestion
->setUserInput($this->userInput);
$suggestion
->setSuggestionSuffix($suggestion_suffix);
if ($results_count !== NULL) {
$suggestion
->setResultsCount($results_count);
}
return $suggestion;
}
/**
* Creates a suggestion that redirects to the specified URL.
*
* @param \Drupal\Core\Url $url
* The URL to which this suggestion should redirect.
* @param string|null $label
* (optional) The label to set for the suggestion. Only makes sense if
* $render isn't given.
* @param array|null $render
* (optional) The render array that should be displayed for this suggestion.
*
* @return \Drupal\search_api_autocomplete\Suggestion\SuggestionInterface
* An autocomplete suggestion.
*/
public function createUrlSuggestion(Url $url, $label = NULL, array $render = NULL) {
$suggestion = new Suggestion(NULL, $url);
if ($label !== NULL) {
$suggestion
->setLabel($label);
}
if ($render !== NULL) {
$suggestion
->setRender($render);
}
return $suggestion;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
SuggestionFactory:: |
protected | property | The keywords input by the user so far. | |
SuggestionFactory:: |
public | function | Creates a suggestion based on the suggested keywords. | |
SuggestionFactory:: |
public | function | Creates a suggestion from a suggested suffix to the user input. | |
SuggestionFactory:: |
public | function | Creates a suggestion that redirects to the specified URL. | |
SuggestionFactory:: |
public | function | Constructs a SuggestionFactory object. |