Skip to content

Instantly share code, notes, and snippets.

@vdchristelle
Created September 9, 2013 07:55
Show Gist options
  • Select an option

  • Save vdchristelle/6492650 to your computer and use it in GitHub Desktop.

Select an option

Save vdchristelle/6492650 to your computer and use it in GitHub Desktop.
Module adds 'language' to the search SQL query.
A fix for the localization lack of the search results.
Search module doesn\'t return localized search results, logically, as it is a side wide result.
However, in some cases we only want to see the localized results, therefore this module adds the language argument to the SQL query.
This is what you should do:
- Copy this module into your "../modules/custom" folder
- Activate the module
- And that's it, all the rest is handled by this piece of code!
And remember Confucius Says: "Never approach a bull from the front, a horse from the rear or a fool from any direction".
name = "Local search results"
description = "Module adds language to the search SQL query."
core = "7.x"
package = My customs
php = "5.2.4"
version = "7.x-1.0"
project = "localsearchresults"
dependencies[] = "search"
dependencies[] = "i18n"
<?php
/**
* @file
* localsearchresults code for your dupal 7 site
*/
/**
* Implements hook_help().
*/
function localsearchresults_help($path, $arg) {
switch ($path) {
case 'admin/help#localsearchresults':
//$output = file_get_contents(drupal_get_path('module', 'the_aim_custom') . '/README.txt');
//return nl2br($output);
return t('<h1>A fix for the localization lack of the search results.</h1>
<p>Search module doesn\'t return localized search results, logically, as it is a side wide result.</p>
<p>However, in some cases we only want to see the localized results, therefore this module adds the language argument to the SQL query.</p>
<h2>This is what you should do:</h2>
<ol>
<li>Copy this module into your modules/custom folder</li>
<li>Activate the module</li>
<li>And that\'s it, all the rest is handled by this piece of code!</li>
</ol>
<p>And remember Confucius Says: "Never approach a bull from the front, a horse from the rear or a fool from any direction".</p>
');
}
}
/**
* Localize your search results
* Implements hook_query_node_access_alter();
**/
function localsearchresults_query_node_access_alter(QueryAlterableInterface $query) {
$search = FALSE;
$node = FALSE;
// Even though we know the node alias is going to be "n", by checking for the
// search_index table we make sure we're on the search page. Omitting this step will
// break the default admin/content page.
foreach ($query->getTables() as $alias => $table) {
if ($table['table'] == 'search_index') {
$search = $alias;
}
elseif ($table['table'] == 'node') {
$node = $alias;
}
}
// Make sure we're on the search page.
if ($node && $search) {
$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