0

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:

src\Form\Mycustomform.php

...
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',
    );
....

mymodule.routing.yml

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

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

\src\Controller\MyModuleController.php

...
class MyModuleController extends ControllerBase {

    public function autocomplete(Request $request)
    {
        $string = $request->query->get('q');
        $matches = array();
        \Drupal:logger('autocomplete')->notice($string);
        $query = \Drupal::entityQuery('node');
        $query->condition('type','customers');
        $query->condition('title', '%'.$string.'%', 'like');
        $query->sort('title');
        $query->range(0,20);
        $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.

2
  • 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
    Commented Apr 2, 2018 at 7:26
  • Thanks for your help. I have found the problem. The code is right. See my answer below.
    – briast
    Commented Apr 3, 2018 at 8:36

2 Answers 2

2

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.

0

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'),
        ),
    );
1
  • 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
    Commented Mar 30, 2018 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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