You are here

search_api_stats.module in Search API Stats 8

Same filename and directory in other branches
  1. 7 search_api_stats.module

File

search_api_stats.module
View source
<?php

/**
 * @file
 */
use Drupal\search_api\Query\QueryInterface;
use Drupal\Core\Database\Database;

/**
 * Alter a search query before it gets executed.
 *
 * The hook is invoked after all enabled processors have preprocessed the query.
 *
 * @param \Drupal\search_api\Query\QueryInterface $query
 *   The query that will be executed.
 */
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();
  }
}

/**
 * Implements hook_views_api().
 */
function search_api_stats_views_api() {
  return [
    'api' => 3.0,
    'path' => drupal_get_path('module', 'search_api_stats') . '/includes/views',
  ];
}

Functions

Namesort descending Description
search_api_stats_search_api_query_alter Alter a search query before it gets executed.
search_api_stats_views_api Implements hook_views_api().