function hook_search_api_query_alter in Search API 8
Same name and namespace in other branches
- 7 search_api.api.php \hook_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.
Deprecated
in search_api:8.x-1.14 and is removed from search_api:2.0.0. Please use the "search_api.query_pre_execute" event instead.
See also
https://www.drupal.org/node/3059866
2 functions implement hook_search_api_query_alter()
Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.
- search_api_test_hooks_search_api_query_alter in tests/
search_api_test_hooks/ search_api_test_hooks.search_api.inc - Implements hook_search_api_query_alter().
- search_api_test_views_search_api_query_alter in tests/
search_api_test_views/ search_api_test_views.module - Implements hook_search_api_query_alter().
File
- ./
search_api.api.php, line 335 - Hooks provided by the Search API module.
Code
function hook_search_api_query_alter(\Drupal\search_api\Query\QueryInterface $query) {
// Do not run for queries with a certain tag.
if ($query
->hasTag('example_tag')) {
return;
}
// Otherwise, exclude the node with ID 10 from the search results.
$fields = $query
->getIndex()
->getFields();
foreach ($query
->getIndex()
->getDatasources() as $datasource_id => $datasource) {
if ($datasource
->getEntityTypeId() == 'node') {
if (isset($fields['nid'])) {
$query
->addCondition('nid', 10, '<>');
}
}
}
}