You are here

public function DidYouMeanSpellCheck::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 AreaPluginBase::render

1 method overrides DidYouMeanSpellCheck::render()
SuggestionsSpellCheck::render in src/Plugin/views/area/SuggestionsSpellCheck.php
Render the area.

File

src/Plugin/views/area/DidYouMeanSpellCheck.php, line 96

Class

DidYouMeanSpellCheck
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;
      if (empty($spellcheck['collation']) && !empty($spellcheck['suggestions'])) {

        // Loop over the suggestions and replace the keys.
        foreach ($spellcheck['suggestions'] as $key => $values) {
          $new_keys = str_ireplace($key, $values[0], $new_keys);
        }
      }

      // Don't offer the identical search keys as "Did you mean".
      if ($new_keys !== $keys) {
        return [
          '#theme' => 'search_api_spellcheck_did_you_mean',
          '#label' => $this
            ->getSuggestionLabel(),
          '#link' => $this
            ->getSuggestionLink($new_keys, $filter_field_key),
        ];
      }
    }
  }
  return [];
}