How to have the autocomplete option with ajax in a text field? I am struggeling with creation of tetxfield with autocomplete option for my Drupal 8. My Drupal website is multilingual, and on one view page we present some content in Default language, by typing in the textfield some other language, I need to change the language of the content. For this I created a filter (Translation language = Content language selected for a page), and I want to expose it in a custom block. 

My custom block looks like following: 

&lt;?php
/**
 * @file
 * Contains \Drupal\filters\Plugin\Block\FilterLanguages.php
 */
namespace Drupal\filters\Plugin\Block;


use Drupal\Core\Block\BlockBase;

/**
 * @Block(
 *   id = "block_filters_languages",
 *   admin_label = @Translation("Filter for languages"),
 *   category = @Translation("Filters"),
 * )
 */
class FilterLanguages extends BlockBase {


  /**
   * {@inheritdoc}
   */

public function build(){

$form['language'] = array(
  '#type' =&gt; 'textfield',
  '#placeholder' =&gt; t('Choose language autocomplete'),
  '#autocomplete_route_name' =&gt; 'filters.autocomplete',

);

return $form;
  }
}


To provide autocompletion option, I have created routing file: 

filters.autocomplete:
  path: '/filters/autocomplete'
  defaults:
    _controller:'\Drupal\filters\Controller\AutocompleteController::handleAutocomplete'
 requirements:
    _access: 'TRUE'


And Controller: 
    

namespace Drupal\filters\Controller;

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Controller\ControllerBase;


class AutocompleteController extends ControllerBase{
  /**
   * Returns response for the autocompletion.
   */
  public function handleAutocomplete(Request $request) {

    $languages = \Drupal::languageManager()-&gt;getLanguages();
    return new JsonResponse($languages);
  }
}


I have oriented on the followin tutorial
But the autocompletion still doesnt work. Could you please help me to solve the problem?
You need to filter the user input and return the matches.

  public function handleAutocomplete(Request $request) {
    // Getting the user input.
    $string = $request-&gt;query-&gt;get('q');
    // Getting the languages.
    $languages = \Drupal::languageManager()-&gt;getLanguages();
    // Getting the matches.
    $matches = preg_grep("/$string/i", $languages);
    // Returning the matches.
    return new JsonResponse($matches);
  }