suggestion.module in Autocomplete Search Suggestions 8
Same filename and directory in other branches
Contains suggestion.module.
File
suggestion.moduleView source
<?php
/**
* @file
* Contains suggestion.module.
*/
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\suggestion\SuggestionHelper;
use Drupal\suggestion\SuggestionStorage;
/**
* Implements hook_cron().
*/
function suggestion_cron() {
if (!SuggestionHelper::getConfig('synced')) {
SuggestionHelper::index();
}
}
/**
* Implements hook_form_alter().
*/
function suggestion_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$autocomplete = SuggestionHelper::getConfig('autocomplete');
$field_name = !empty($autocomplete[$form_id]) ? $autocomplete[$form_id] : '';
if (!$field_name) {
return;
}
return SuggestionHelper::alterElement($form, $field_name);
}
/**
* Implements hook_help().
*/
function suggestion_help($route_name, CurrentRouteMatch $route_match) {
$x = [
'```php' => '<code>',
'```' => '</code>',
"\n" => "\n<br />",
];
switch ($route_name) {
case 'help.page.suggestion':
$txt = file_get_contents(drupal_get_path('module', 'suggestion') . '/README.md');
return str_replace(array_keys($x), array_values($x), Html::escape($txt));
}
return '';
}
/**
* Implements hook_node_insert().
*/
function suggestion_node_insert($node) {
$types = SuggestionHelper::types();
if (!empty($node->status) && !empty($node->type) && in_array($node->type, $types)) {
SuggestionHelper::insert($node->title, SuggestionStorage::CONTENT_BIT);
}
}
/**
* Implements hook_permission().
*/
function suggestion_permission() {
return [
'administer suggestion' => [
'title' => t('Administer the Suggestion Module'),
'description' => t('Perform administration tasks for the suggestion module.'),
],
];
}
/**
* Custom submit function to add surfer suggestions.
*/
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);
}
}
Functions
Name | Description |
---|---|
suggestion_cron | Implements hook_cron(). |
suggestion_form_alter | Implements hook_form_alter(). |
suggestion_help | Implements hook_help(). |
suggestion_node_insert | Implements hook_node_insert(). |
suggestion_permission | Implements hook_permission(). |
suggestion_surfer_submit | Custom submit function to add surfer suggestions. |