I have developed a module with a custom form but I don't know why ajax autocomplete doesn't work. The form works good, but in one of the input field I'd like to have a autocomplete function. The next is part of my code:


public function buildForm(array $form, FormStateInterface $form_state, $parameter = NULL) {
    $form['customer'] = array(
      '#type' => 'textfield',
      '#title' => t('Customer'),
      '#id' => 'inpCustomer',
      '#autocomplete_route_name' => 'mymodule.autocomplete',


  path: 'mycustompath'
    _form: '\Drupal\mymodule\Form\Mycustomform'
    _title: 'Records'
    _permission: mycustompermission

  path: 'autocomplete'
    _controller: '\Drupal\mymodule\Controller\MyModuleController::autocomplete'
    _format: json
    _permission: 'access content' 


class MyModuleController extends ControllerBase {

    public function autocomplete(Request $request)
        $string = $request->query->get('q');
        $matches = array();
        $query = \Drupal::entityQuery('node');
        $query->condition('title', '%'.$string.'%', 'like');
        $entity = $query->execute();
        $nodos = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($entity);
        foreach ($nodos as $row) {
            $matches[] = array('value' => $row->getTitle(), 'label' => $row->getTitle());

        return new JsonResponse($matches);

If I see drupal log, I never get a register as expected. But if I see conosole log, I don't see ajax request. How could I find where is the problem? I think there isn't an ajax call when I write some letters on the input field. I don't see the circle on the right of the input that is shown when autocomplete is working.

  • Can you try without the _format: json? Btw, I think you could still use entity_autocomplete for this, in combination with a custom selection plugin. You just need that plugin then where you override/customize the query. Using #type entity_autocomplete gives you a bunch of additional things like proper handling of default value/validation to make sure your users can really only select a valid node there.. just having the autocomplete means you still need to parse the string, validate it and so on. – Berdir Apr 2 '18 at 7:26
  • Thanks for your help. I have found the problem. The code is right. See my answer below. – briast Apr 3 '18 at 8:36

After some tests I have found the problem. My code is right and it works good. The problem was the template file input--textfield.html.twig. Input textfield needs some automplete classes to work and this template deleted them to add some other classes. The required classes are: form-autocomplete form-text ui-autocomplete-input. I have changed this template file to add a new class I need but without deleting the other, and then, autocomplete works.


I do not tested your code but I see that your custom form doing completely the same like entity_autocomplete form element https://www.drupal.org/node/2418529

$form['customer'] = array(
      '#type' => 'entity_autocomplete',
      '#title' => t('Customer'),
      '#id' => 'inpCustomer',
      '#target_type' => 'node',
      '#selection_settings' => array(
        'target_bundles' => array('customers'),
  • Thanks for your help. The autocomplete function I have shown is an extract. It is more complex to filter only some customers with some conditions, but thouse code is not important for my question. – briast Mar 30 '18 at 12:20

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.