search_api_stats.module in Search API Stats 8
File
search_api_stats.module
View source
<?php
use Drupal\search_api\Query\QueryInterface;
use Drupal\Core\Database\Database;
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);
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();
}
}
function search_api_stats_views_api() {
return [
'api' => 3.0,
'path' => drupal_get_path('module', 'search_api_stats') . '/includes/views',
];
}