SuggestionsSpellCheck.php in Search API Spellcheck 8.3
File
src/Plugin/views/area/SuggestionsSpellCheck.php
View source
<?php
namespace Drupal\search_api_spellcheck\Plugin\views\area;
use Drupal\Core\Form\FormStateInterface;
use Drupal\search_api\Query\ResultSetInterface;
class SuggestionsSpellCheck extends DidYouMeanSpellCheck {
protected function defineOptions() {
$options = parent::defineOptions();
$options['search_api_spellcheck_collate']['default'] = FALSE;
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['search_api_spellcheck_count'] = [
'#default_value' => $this->options['search_api_spellcheck_count'] ?? TRUE,
'#title' => $this
->t('The amount of results to show.'),
'#type' => 'number',
];
}
public function render($empty = FALSE) {
if (!$this->options['search_api_spellcheck_hide_on_result'] || $empty) {
$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']));
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 [];
}
protected function getSuggestionLabel() {
return $this
->t('Spellcheck keyword variations:');
}
protected function combineArrays(array $suggestions) {
$odometer = array_fill(0, count($suggestions), 0);
$combined_suggestions[] = $this
->formCombination($odometer, $suggestions);
while ($this
->odometerIncrement($odometer, $suggestions)) {
$combined_suggestions[] = $this
->formCombination($odometer, $suggestions);
}
return $combined_suggestions;
}
protected function formCombination(array $odometer, array $suggestions) {
$output = '';
$count = count($odometer);
for ($i = 0; $i < $count; $i++) {
if ($i === 0) {
$output .= $suggestions[$i][$odometer[$i]];
}
else {
$output .= ' ' . $suggestions[$i][$odometer[$i]];
}
}
return $output;
}
protected function odometerIncrement(array &$odometer, array $suggestions) {
$count = count($odometer);
for ($i = $count - 1; $i >= 0; $i--) {
$max = count($suggestions[$i]) - 1;
if ($odometer[$i] + 1 <= $max) {
$odometer[$i]++;
return TRUE;
}
if ($i - 1 < 0) {
break;
}
$odometer[$i] = 0;
}
return FALSE;
}
}