4

I have a site with 4 languages and am using the builtin search to look for content on the site. How is it possible to restrict the search results only to the currently selected language?

I tried looking into Configuration > Search Pages, but do not see any way how to do that there.

  • Are you using solr search or normal search with some view ? – Shreya Shetty Sep 6 '16 at 7:42
  • I don't use a view for this. I am using the out-of-the-box search that you get on a fresh install of D8. The only way I found to modify the behavior of the search is either through Configuration > Search Pages or via twig templates. – Ondrej Sep 6 '16 at 8:34
7

The "Advanced" core search has this built in. If you prefer to do this from a theme - you could do it with form_alter. This basically hides the advanced search settings and sets current language as the only language to get results from, in the search block and search page.

Alternately you can just allow users to control which language they want to get results from and simply allow them to use advanced search.

Code for theme:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function hook_form_search_form_alter(&$form, &$form_state) {
  $form['help_link']['#access'] = FALSE;
  $form['advanced']['#access'] = FALSE;
  $form['basic']['keys']['#title'] = '';
  $manager = \Drupal::languageManager();
  $form['advanced']['lang-fieldset']['language']['#default_value'] = [$manager->getCurrentLanguage()->getId()];
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function hook_form_search_block_form_alter(&$form, &$form_state) {
  $manager = \Drupal::languageManager();

  $form['advanced-form'] = [
    '#type' => 'hidden',
    '#value' => 1,
  ];
  $form['f[0]'] = [
    '#type' => 'hidden',
    '#value' => 'language:' . $manager->getCurrentLanguage()->getId(),
  ];
}
  • Sounds good. I tried adding the code above to mytheme.theme, cleared the cache and rebuilt, but there's no change. Anything else I might have to do? – Ondrej Sep 7 '16 at 13:32
  • 1
    @Ondrej Replace hook with theme name in functions – googletorp Sep 9 '16 at 6:46
  • @googletorp thx for the suggestion, but changing "hook" with theme-name didn't work. it should be put in mytheme.theme, no? – jimi Apr 8 '17 at 13:46
  • @jimi I think you may be misunderstanding. You rename the functions in the above code. So instead of "hook_form_search_form_alter" it would be "somecustomthemename_form_search_form_alter" this worked for me. I used this code and it works fine for me. Thanks googletorp. – SuperTony Aug 16 '17 at 21:16
  • This actually works for the search block form! Thanks! Is there anywhere with documentation about adding advanced filters this way? I can't find ANYTHING. – ProGrammar Oct 27 '17 at 19:45
3

You can use simple way hook_query_TAG_alter. Example return node for current language.

<?php

use Drupal\Core\Database\Query\AlterableInterface;

/**
 * Implements hook_query_TAG_alter(): tag search_$type with $type node_search.
 */
function MYMODULE_query_search_node_search_alter(AlterableInterface $query) {
  $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  $query->condition('n.langcode', $language, '=');
}
  • Right, so there's really no inbuilt way to do this - at least I know that for sure now :) I'm not a module developer, so, can this be somehow done on the theme level (like in mytheme.theme maybe)? – Ondrej Sep 6 '16 at 12:19
  • Same question here, is this also possible on theme level? – 0711master Jan 6 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.