Skip to content

Instantly share code, notes, and snippets.

@rafinskipg
Created November 2, 2012 13:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafinskipg/4001350 to your computer and use it in GitHub Desktop.
Save rafinskipg/4001350 to your computer and use it in GitHub Desktop.
Drupal 7 Default query search alter
function mymodule_query_alter(QueryAlterableInterface $query){
$is_search = FALSE;
foreach ($query->getTables() as $table) {
if ($table['table'] == 'search_index') {
$is_search = TRUE;
}
}
if ($is_search) {
global $language;
$db_or = db_or();
$db_or->condition('n.type', 'event', '=');
$db_or->condition('n.type', 'real_sitio', '=');
$query->condition($db_or);
$query->condition('n.language' , $language->language, '=');
}
}
@rafinskipg
Copy link
Author

This is a bit performance killer so there's a patch for drupal at http://drupal.org/node/1435834 that adds a hook for making the alter directly in the search query:

So finally it would look like:

function mymodule_search_query_search_node_alter(&$query) {
$query->condition('n.type', 'article', '=');
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment