function search_api_stats_search_api_query_alter in Search API Stats 8
Same name and namespace in other branches
- 7 search_api_stats.module \search_api_stats_search_api_query_alter()
Alter a search query before it gets executed.
The hook is invoked after all enabled processors have preprocessed the query.
Parameters
\Drupal\search_api\Query\QueryInterface $query: The query that will be executed.
File
- ./
search_api_stats.module, line 18
Code
function search_api_stats_search_api_query_alter(QueryInterface &$query) {
$user = Drupal::currentUser();
$language = Drupal::languageManager()
->getCurrentLanguage();
$database = Database::getConnection();
$index = $query
->getIndex();
if (!empty($index)) {
$server = $index
->get('server');
}
if (empty($index) || empty($server)) {
return;
}
$originalKeys = $query
->getOriginalKeys();
$lowerOriginalKeys = mb_strtolower($originalKeys);
$keywords = trim($lowerOriginalKeys);
// To avoid to insert empty keywords value into database.
if (!empty($keywords)) {
$database
->insert('search_api_stats')
->fields([
's_name' => $server,
'i_name' => $index
->id(),
'timestamp' => \Drupal::time()
->getRequestTime(),
'uid' => $user
->id(),
'sid' => session_id(),
'keywords' => $keywords,
'filters' => '',
'sort' => '',
'language' => $language
->getId(),
])
->execute();
}
}