Skip to content

Instantly share code, notes, and snippets.

@opi
Created January 17, 2014 10:08
Show Gist options
  • Save opi/8470954 to your computer and use it in GitHub Desktop.
Save opi/8470954 to your computer and use it in GitHub Desktop.
Drupal 7 Search language and content type
<?php
/**
* Implements of hook_query_node_access_alter().
*/
function MYMODULE_query_node_access_alter(QueryAlterableInterface $query) {
$search = FALSE;
$node = FALSE;
foreach ($query->getTables() as $alias => $table) {
if ($table['table'] == 'search_index') {
$search = $alias;
}
elseif ($table['table'] == 'node') {
$node = $alias;
}
}
if ($node && $search) {
// Searchable node types
$allowed_type = array(
'content_type_1',
'content_type_2'
);
$query->condition($node . '.type', $allowed_type, 'IN');
// Search language
$db_and = db_and();
// I guess you *could* use global $language here instead but this is safer.
$language = i18n_language_interface();
$lang = $language->language;
$db_and->condition($node . '.language', $lang, '=');
$query->condition($db_and);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment