Add event like autocompleteclose to trigger some custom AJAX

Created on 11 April 2025, 8 days ago

Problem/Motivation

I have a custom form with the below elements. I want to trigger an ajax when a value has been selected. The problem with "change" is that I loose the focus on the element and therefore, there is a bug. If I keep typing, the selected value is split ans disappear from the autocomplete field.

   $form['export_wrapper']['my_select'] = [
      '#type' => 'entity_autocomplete_tagify',
      '#target_type' => 'node',
      '#tags' => TRUE,
      '#selection_handler' => 'default',
      '#selection_settings' => [
        'target_bundles' => ['article'],
      ],
      '#match_operator' => 'CONTAINS',
      '#show_entity_id' => 1,
      '#ajax' => [
        'callback' => '::ajaxUpdateLinkField',
        'event' => 'change',
        'wrapper' => 'link-field-wrapper',
        'progress' => [
          'type' => 'throbber',
          'message' => $this->t('Updating link...'),
        ],
      ],
    ];

    $form['export_wrapper']['export_button'] = [
      '#type' => 'link',
      '#title' => $this->t('Export CSV'),
      '#url' => $url,
      '#attributes' => [
        'class' => ['button', 'button--primary'],
        'role' => 'button',
        'id' => 'link-field-wrapper',
      ],
    ];

Steps to reproduce

Create a custom form, start typing in the autocomplete field, select a value, (wait for the ajax to be triggered), keep typing and there is the bug.

Proposed resolution

Best solution would be to have a custom ajax event triggered when selecting value and keep the focus on the autocomplete field to keep typing and select another autocomplete item.

Feature request
Status

Active

Version

1.2

Component

Code

Created by

🇫🇷France pbonnefoi

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024