You are here

public function SuggestionsSpellCheck::render in Search API Spellcheck 8.3

Render the area.

Parameters

bool $empty: (optional) Indicator if view result is empty or not. Defaults to FALSE.

Return value

array In any case we need a valid Drupal render array to return.

Overrides DidYouMeanSpellCheck::render

File

src/Plugin/views/area/SuggestionsSpellCheck.php, line 47

Class

SuggestionsSpellCheck
Provides an area for messages.

Namespace

Drupal\search_api_spellcheck\Plugin\views\area

Code

public function render($empty = FALSE) {
  if (!$this->options['search_api_spellcheck_hide_on_result'] || $empty) {

    /** @var ResultSetInterface $result */
    $result = $this->query
      ->getSearchApiResults();
    if ($spellcheck = $result
      ->getExtraData('search_api_spellcheck')) {
      $filter_field_key = $this
        ->getFilterFieldKey();
      $exposed_input = $this->view
        ->getExposedInput();
      $keys = $exposed_input[$filter_field_key] ?? '';
      $new_keys = $spellcheck['collation'] ?? $keys;
      $key_suggestions = [];
      if (empty($spellcheck['collation']) && !empty($spellcheck['suggestions'])) {
        $combined_suggestions[$new_keys] = $this
          ->combineArrays(array_values($spellcheck['suggestions']));

        // Loop over the combined suggestions and replace the keys.
        foreach ($combined_suggestions as $key => $values) {
          foreach ($values as $value) {
            if (!empty($key)) {
              $key_suggestions[] = str_ireplace($key, $value, $new_keys);
            }
          }
        }
      }
      else {
        return [];
      }
      if (empty($key_suggestions)) {
        return [];
      }
      $suggestions = [];
      foreach ($key_suggestions as $suggestion) {
        $suggestions[] = $this
          ->getSuggestionLink($suggestion, $filter_field_key);
      }
      return [
        '#theme' => 'search_api_spellcheck_suggestions',
        '#label' => $this
          ->getSuggestionLabel(),
        '#suggestions' => $suggestions,
      ];
    }
  }
  return [];
}