Autocomplete function for textfield of Form API | Drupal 8

Autocomplete function for textfield of Form API

Submitted by editor on Tue, 09/12/2017 - 15:10

How to create an autocomplete text field using drupal 8 Form API ?

First, Create autocomplete controller and the route

Example of the controller : DefaultController.php


namespace Drupal\mymodule\Controller;

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

class DefaultController extends ControllerBase{
   * Returns response for the autocompletion.
  public function autocomplete(Request $request) {
    $matches = ['Value 1', 'Value 2', '... ...'];
    return new JsonResponse($matches);

Example of the route : mymodule.routing.yml

  path: '/mymodule/autocomplete'
    _controller: '\Drupal\mymodule\Controller\DefaultController::autocomplete'
    _access: 'TRUE'

Then add this route to the field

Example :

$form['myfield'] = [
  '#type' => 'textfield',
  '#title' => t('Autocomplete text field'),
  '#autocomplete_route_name' => 'mymodule.autocomplete',

See Entity Autocomplete From here


maya (not verified)

Tue, 01/23/2018 - 11:01


j'ai un array de  70326 valeurs,la recherche ne parche pas.

Pouvez vouz me conseiller 


youssouf (not verified)

Fri, 06/08/2018 - 11:04

How to retrieve data from solr index or database using your issue ?

Do you know how to simply add autocomplete search api on custom form programmatically !

thank you in advance !

R HILDAN M (not verified)

Fri, 12/14/2018 - 17:08

ling12 undangan pernikahan modern undangan pernikahan online undangan pernikahan undangan pernikahan amplop surat undangan pernikahan simple undangan pernikahan murah undangan pernikahan unik undangan pernikahan artis undangan pernikahan modern undangan pernikahan artis undangan pernikahan arab undangan pernikahan undangan pernikahan Toko Plat Bordes Toko Plat Bordes Pabrik bondek Pabrik besi unp undangan pernikahan cantik Distributor Besi Hollow Jual Besi Hollow Baja Toko besi hollow Distributor besi beton Sni Ulir Polos Agen Plat Bordes Agen Plat Bordes Pabrik Plat kapal besi baja bki krakatau steel Pabrik besi siku undangan pernikahan simple Distributor Wiremesh Harga Besi Hollow Baja Agen besi hollow Distributor besi beton ais Pabrik Plat Bordes Pabrik Plat Bordes Pabrik besi siku Pabrik Plat besi plat kapal undangan pernikahan mewah Distributor Besi Pipa Baja Schedule 80 Pabrik Besi Hollow Baja Pabrik besi hollow Distributor besi beton Distributor Wiremesh Besi Distributor Wiremesh Besi Pabrik pipa besi baja sch 40 sch 80 Pabrik besi h beam baja undangan pernikahan cute Distributor Besi Pipa Baja Schedule 40 Distributor Besi Beton Distributor pipa besi Harga Wiremesh Besi Harga Wiremesh Besi Pabrik Plat besi Pabrik besi cnp undangan pernikahan biru Distributor Pipa Besi Hitam Medium Supplier Besi Beton Harga pipa besi Jual Wiremesh Besi Jual Wiremesh Besi Pabrik besi h beam undangan pernikahan buatan sendiri Distributor Besi Wf Agen Besi Beton Jual pipa besi Supplier Wiremesh Besi Supplier Wiremesh Besi Pabrik besi wf

Add new comment