You are here

function suggestion_surfer_submit in Autocomplete Search Suggestions 8

Same name and namespace in other branches
  1. 8.2 suggestion.module \suggestion_surfer_submit()
  2. 7 suggestion.module \suggestion_surfer_submit()
  3. 3.0.x suggestion.module \suggestion_surfer_submit()

Custom submit function to add surfer suggestions.

1 string reference to 'suggestion_surfer_submit'
SuggestionHelper::alterElement in src/SuggestionHelper.php
Search the form recursivley for the field and add the autocomplete route.

File

./suggestion.module, line 80
Contains suggestion.module.

Code

function suggestion_surfer_submit($form, FormStateInterface $form_state) {
  $cfg = SuggestionHelper::getConfig();
  $form_id = $form_state
    ->getValue('form_id');
  $min_score = 1;
  $field_name = !empty($cfg->autocomplete[$form_id]) ? $cfg->autocomplete[$form_id] : '';
  if (!$field_name || !$form_state
    ->getValue($field_name)) {
    return;
  }
  $txt = SuggestionHelper::tokenize($form_state
    ->getValue($field_name), $cfg->min);
  if (!$txt) {
    return;
  }
  $atoms = SuggestionHelper::atomize($txt);
  $count = count($atoms);
  if ($count < $cfg->atoms_min || $count > $cfg->atoms_max) {
    return;
  }
  $ngram = implode(' ', $atoms);
  $qty = SuggestionStorage::getNgramQty($ngram);
  if ($qty) {
    $count = str_word_count($ngram);
    $src = SuggestionStorage::getBitmap($ngram, SuggestionStorage::SURFER_BIT);
    $key = [
      'ngram' => $ngram,
    ];
    $fields = [
      'atoms' => $count,
      'density' => SuggestionHelper::calculateDensity($src, $count, $qty + 1),
      'qty' => $qty + 1,
      'src' => $src,
    ];
    SuggestionStorage::mergeSuggestion($key, $fields);
    return;
  }
  $score = SuggestionStorage::getScore($atoms);
  if ($score >= $min_score) {
    SuggestionHelper::insert($ngram, SuggestionStorage::SURFER_BIT, $score);
  }
}