You are here

class Suggestion in Search API Autocomplete 8

Provides a value object meant to be used as result of suggestions.

Hierarchy

Expanded class hierarchy of Suggestion

1 file declares its use of Suggestion
search_api_autocomplete_test_hooks.search_api_autocomplete.inc in tests/search_api_autocomplete_test_hooks/search_api_autocomplete_test_hooks.search_api_autocomplete.inc
Implements all Search API Autocomplete hooks, for testing purposes.

File

src/Suggestion/Suggestion.php, line 10

Namespace

Drupal\search_api_autocomplete\Suggestion
View source
class Suggestion implements SuggestionInterface {

  /**
   * The keywords this suggestion will autocomplete to.
   *
   * @var string|null
   */
  protected $suggestedKeys;

  /**
   * A URL to which the suggestion should redirect.
   *
   * @var \Drupal\Core\Url|null
   */
  protected $url;

  /**
   * For special suggestions, some kind of HTML prefix describing them.
   *
   * @var string|null
   */
  protected $prefix;

  /**
   * The label to use for the suggestion.
   *
   * @var string|null
   */
  protected $label;

  /**
   * A suggested prefix for the entered input.
   *
   * @var string|null
   */
  protected $suggestionPrefix;

  /**
   * The input entered by the user. Defaults to $user_input.
   *
   * @var string|null
   */
  protected $userInput;

  /**
   * A suggested suffix for the entered input.
   *
   * @var string|null
   */
  protected $suggestionSuffix;

  /**
   * If available, the estimated number of results for these keys.
   *
   * @var int|null
   */
  protected $resultsCount;

  /**
   * If given, an HTML string or render array.
   *
   * @var array|null
   */
  protected $render;

  /**
   * Constructs a Suggestion object.
   *
   * @param string|null $suggested_keys
   *   (optional) The suggested keys.
   * @param \Drupal\Core\Url|null $url
   *   (optional) The URL to redirect to.
   * @param string|null $prefix
   *   (optional) The prefix for the suggestion.
   * @param string|null $label
   *   (optional) The label for the suggestion.
   * @param string|null $suggestion_prefix
   *   (optional) The suggested prefix.
   * @param string|null $user_input
   *   (optional) The user input.
   * @param string|null $suggestion_suffix
   *   (optional) The suggested suffix.
   * @param int|null $results_count
   *   (optional) The estimated number of results.
   * @param array|null $render
   *   (optional) The render array.
   */
  public function __construct($suggested_keys = NULL, Url $url = NULL, $prefix = NULL, $label = NULL, $suggestion_prefix = NULL, $user_input = NULL, $suggestion_suffix = NULL, $results_count = NULL, array $render = NULL) {
    $this->suggestedKeys = $suggested_keys;
    $this->url = $url;
    $this->prefix = $prefix;
    $this->label = $label;
    $this->suggestionPrefix = $suggestion_prefix;
    $this->userInput = $user_input;
    $this->suggestionSuffix = $suggestion_suffix;
    $this->resultsCount = $results_count;
    $this->render = $render;
  }

  /**
   * {@inheritdoc}
   */
  public function getSuggestedKeys() {
    if ($this->url) {
      return NULL;
    }
    if ($this->suggestedKeys) {
      return $this->suggestedKeys;
    }
    return $this->suggestionPrefix . $this->userInput . $this->suggestionSuffix;
  }

  /**
   * {@inheritdoc}
   */
  public function getUrl() {
    return $this->url;
  }

  /**
   * {@inheritdoc}
   */
  public function getPrefix() {
    return $this->prefix;
  }

  /**
   * {@inheritdoc}
   */
  public function getLabel() {
    return $this->label;
  }

  /**
   * {@inheritdoc}
   */
  public function getSuggestionPrefix() {
    return $this->suggestionPrefix;
  }

  /**
   * {@inheritdoc}
   */
  public function getUserInput() {
    return $this->userInput;
  }

  /**
   * {@inheritdoc}
   */
  public function getSuggestionSuffix() {
    return $this->suggestionSuffix;
  }

  /**
   * {@inheritdoc}
   */
  public function getResultsCount() {
    return $this->resultsCount;
  }

  /**
   * {@inheritdoc}
   */
  public function getRender() {
    return $this->render;
  }

  /**
   * {@inheritdoc}
   */
  public function setSuggestedKeys($suggestedKeys) {
    $this->suggestedKeys = $suggestedKeys;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setUrl($url) {
    $this->url = $url;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setPrefix($prefix) {
    $this->prefix = $prefix;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setLabel($label) {
    $this->label = $label;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setSuggestionPrefix($suggestion_prefix) {
    $this->suggestionPrefix = $suggestion_prefix;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setUserInput($user_input) {
    $this->userInput = $user_input;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setSuggestionSuffix($suggestion_suffix) {
    $this->suggestionSuffix = $suggestion_suffix;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setResultsCount($results) {
    $this->resultsCount = $results;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setRender($render) {
    $this->render = $render;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function toRenderable() {
    if (!empty($this->render)) {
      return $this->render;
    }
    return [
      '#theme' => 'search_api_autocomplete_suggestion',
      '#keys' => $this
        ->getSuggestedKeys(),
      '#url' => $this
        ->getUrl(),
      '#note' => $this
        ->getPrefix(),
      '#label' => $this
        ->getLabel(),
      '#results_count' => $this
        ->getResultsCount(),
      '#suggestion_prefix' => $this
        ->getSuggestionPrefix(),
      '#suggestion_suffix' => $this
        ->getSuggestionSuffix(),
      '#user_input' => $this
        ->getUserInput(),
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Suggestion::$label protected property The label to use for the suggestion.
Suggestion::$prefix protected property For special suggestions, some kind of HTML prefix describing them.
Suggestion::$render protected property If given, an HTML string or render array.
Suggestion::$resultsCount protected property If available, the estimated number of results for these keys.
Suggestion::$suggestedKeys protected property The keywords this suggestion will autocomplete to.
Suggestion::$suggestionPrefix protected property A suggested prefix for the entered input.
Suggestion::$suggestionSuffix protected property A suggested suffix for the entered input.
Suggestion::$url protected property A URL to which the suggestion should redirect.
Suggestion::$userInput protected property The input entered by the user. Defaults to $user_input.
Suggestion::getLabel public function Retrieves the label to use for the suggestion. Overrides SuggestionInterface::getLabel
Suggestion::getPrefix public function Retrieves the prefix for the suggestion. Overrides SuggestionInterface::getPrefix
Suggestion::getRender public function Returns the render array set for this suggestion. Overrides SuggestionInterface::getRender
Suggestion::getResultsCount public function Returns the estimated number of results for this suggestion. Overrides SuggestionInterface::getResultsCount
Suggestion::getSuggestedKeys public function Retrieves the keywords this suggestion will autocomplete to. Overrides SuggestionInterface::getSuggestedKeys
Suggestion::getSuggestionPrefix public function Retrieves the prefix suggested for the entered keys. Overrides SuggestionInterface::getSuggestionPrefix
Suggestion::getSuggestionSuffix public function A suggested suffix for the entered input. Overrides SuggestionInterface::getSuggestionSuffix
Suggestion::getUrl public function Retrieves the URL to which the suggestion should redirect. Overrides SuggestionInterface::getUrl
Suggestion::getUserInput public function The input entered by the user, if it should be included in the label. Overrides SuggestionInterface::getUserInput
Suggestion::setLabel public function Sets the label. Overrides SuggestionInterface::setLabel
Suggestion::setPrefix public function Sets the prefix. Overrides SuggestionInterface::setPrefix
Suggestion::setRender public function Sets the render array. Overrides SuggestionInterface::setRender
Suggestion::setResultsCount public function Sets the result count. Overrides SuggestionInterface::setResultsCount
Suggestion::setSuggestedKeys public function Sets the keys. Overrides SuggestionInterface::setSuggestedKeys
Suggestion::setSuggestionPrefix public function Sets the suggestion prefix. Overrides SuggestionInterface::setSuggestionPrefix
Suggestion::setSuggestionSuffix public function Sets the suggestion suffix. Overrides SuggestionInterface::setSuggestionSuffix
Suggestion::setUrl public function Sets the URL. Overrides SuggestionInterface::setUrl
Suggestion::setUserInput public function Sets the user input. Overrides SuggestionInterface::setUserInput
Suggestion::toRenderable public function Returns a render array representation of the object. Overrides RenderableInterface::toRenderable
Suggestion::__construct public function Constructs a Suggestion object.