Problem/Motivation

When using the Entity Reference View Mode field widget, the AJAX handler attached to the entity_autocomplete element does not fire on initial page load when a user selects an autocomplete suggestion. The widget updates properly only after another AJAX-triggering action (e.g. changing the target entity type) causes the widget to rebuild.

This makes the widget appear unresponsive and prevents the view mode selector from updating until another AJAX event occurs.

Steps to reproduce

  1. Add a field using the Entity Reference View Mode widget to a content type.
  2. Edit a node and type into the autocomplete field.
  3. Select a result from the dropdown.
  4. Observe that the AJAX callback does not fire and the widget does not update.
  5. Now change the “target type” select field.
  6. The AJAX callback now fires as expected, and from this point onward the autocomplete AJAX works.

Proposed resolution

Fixes included in the attached patch:

Corrects the AJAX callback signature:

  • First argument is now array &$form
  • Second argument is FormStateInterface $form_state
  • Method changed to an instance method instead of static to match usage.

Updates the autocomplete event to a reliable one supported by Drupal core:

  • 'event' => 'autocompleteclose',
  • Minor cleanup of unused references inside the callback.

After applying the patch, the AJAX handler works correctly on first page load as expected.

Remaining tasks

  • Review patch
  • Test across different entity types and bundles
  • Commit if approved

Comments

adampacker created an issue.

  • 240a383c committed on 8.x-2.x
    fix: #3560757 AJAX callback not triggered on initial page load for...