Filter search results by current language

Paste this code into your custom module and your results of simple core search will be filtered by current language.

 

function MY_MODULE_query_node_access_alter(QueryAlterableInterface $query) {
  global $language;
  $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.
   
    $lang = $language->language;

    $db_and->condition($node . '.language', $lang, '=');
    $query->condition($db_and);
  }
}

 

Wiki Tags: 

Testimonials

With people from HalfPixel we always found best solution, for clients and for us.

Peter Potocar, BIgKids.sk

Are you around here?

When you`re not far, come visit us.

Contact