You are here

public static function SuggestionHelper::insert in Autocomplete Search Suggestions 8

Same name and namespace in other branches
  1. 8.2 src/SuggestionHelper.php \Drupal\suggestion\SuggestionHelper::insert()
  2. 3.0.x src/SuggestionHelper.php \Drupal\suggestion\SuggestionHelper::insert()

Add a suggestion.

Parameters

string $txt: The title to index.

int $src: The bits to OR with the current bitmap.

int $qty: Default quantity.

Return value

int The number of suggestions inserted.

4 calls to SuggestionHelper::insert()
SuggestionAdminForm::setKeywords in src/Form/SuggestionAdminForm.php
Process all the priority suggestions submitted.
SuggestionHelper::index in src/SuggestionHelper.php
Create a suggestion index from content titles.
suggestion_node_insert in ./suggestion.module
Implements hook_node_insert().
suggestion_surfer_submit in ./suggestion.module
Custom submit function to add surfer suggestions.

File

src/SuggestionHelper.php, line 164

Class

SuggestionHelper
Provides helper methods for suggestions.

Namespace

Drupal\suggestion

Code

public static function insert($txt = '', $src = SuggestionStorage::CONTENT_BIT, $qty = NULL) {
  $count = 0;
  $max = self::getConfig('max');
  $txt = self::tokenize($txt, self::getConfig('min'));
  if (!$txt) {
    return 0;
  }
  $atoms = self::atomize($txt);
  foreach (array_keys(self::ngrams($atoms)) as $ngram) {
    if (strlen($ngram) > $max) {
      continue;
    }
    $count = str_word_count($ngram);
    $qty = is_numeric($qty) ? $qty + 1 : SuggestionStorage::getNgramQty($ngram) + 1;
    $src = SuggestionStorage::getBitmap($ngram, $src);
    $key = [
      'ngram' => $ngram,
    ];
    $fields = [
      'atoms' => $count,
      'density' => self::calculateDensity($src, $count, $qty),
      'qty' => $qty,
      'src' => $src,
    ];
    SuggestionStorage::mergeSuggestion($key, $fields);
    $count++;
  }
  return $count;
}