2

My Drupal 8 website is multilingual, and on one of the views I present a list of nodes (titles). On top of the page I need to implement a textfield with autocomplete function, where the user can start typing in the language, and then by choosing one the content on the page will be presented in chosen language.

I have started by creating a custom block:

public function build(){

$block = array(
  '#type' => 'textfield',
  '#title' => t('Choose language'),
  '#autocomplete_path' => '?????',
  '#cache' => array(
    'max-age' => 0,
  ),
);
return $block;

}

What should I write in '#autocomplete_path' => '?????' as the list of languages is not taxonomies. It is coming from Languages module. Could you please help me?

  • You are going to filter results in your views, it means you need to provide views filter by language instead of some block with autocomplete. – Alex Kuzava Jan 29 at 13:15
  • Yes, I have added such a filter, and chose: Expose filter in a block, now I am trying to customize it and represent as an autocomplete textfield – user91143 Jan 29 at 13:28
2

I think no need to create block of view programmatically, You can do it with Views Autocomplete Filters module and views Module:
Try the following steps how to create view with language filter, using autocomplete:

  1. Install Views Autocomplete Filters Module.
  2. Create View with Fields Display add the language field The language of the content or translation. and your additional fields: enter image description here

  3. Add Combine fields filter enter image description here

configure it like the following: enter image description here enter image description here

  1. the result will be like:

enter image description here enter image description here

  • Thanks a lot for your answer. It is exactly what I need. Just one more thing for me is that I have not-exposed filter criteria (Translation language = Content language selected for a page). And that's why I in the combined fields filter I can seacrh only for the current langauge of the page. Possibly I use wrong operator for the combined fields filter (Is equal to )? – user91143 Jan 29 at 15:02
  • It looks that this solution could be good as a seacrh function, but not as a language switcher – user91143 Jan 30 at 7:41
  • @user91143 as you says this answer solve your question consider check it as accepted to help another users to find the correct answer if they have a similar problem. – Adrian Cid Almaguer Jan 30 at 16:07
  • It looked like a solution for me from the first look, but in fact it didn't solve my problem. – user91143 Feb 4 at 11:30

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.