You are here

function search_api_test_views_search_api_query_alter in Search API 8

Implements hook_search_api_query_alter().

  • Prints the contents of the "search_api_retrieved_field_values" query option to the page (if present) so it can be checked by the testing code.
  • Optionally alters the query to include custom cacheability metadata, so that we can test if modules can alter the cacheability of search queries.

File

tests/search_api_test_views/search_api_test_views.module, line 21
Contains hook implementations for the Search API Views Test module.

Code

function search_api_test_views_search_api_query_alter(QueryInterface $query) {
  $fields = $query
    ->getOption('search_api_retrieved_field_values');
  if ($fields) {
    \Drupal::messenger()
      ->addStatus("'" . implode("' '", $fields) . "'");
  }
  $alter_cache_metadata = \Drupal::state()
    ->get('search_api_test_views.alter_query_cacheability_metadata', FALSE);
  if ($alter_cache_metadata && $query instanceof RefinableCacheableDependencyInterface) {

    // Alter in some imaginary cacheability metadata for testing.
    $query
      ->addCacheContexts([
      'search_api_test_context',
    ]);
    $query
      ->addCacheTags([
      'search_api:test_tag',
    ]);
    $query
      ->mergeCacheMaxAge(100);
  }
}