Skip to content

[#26] Added the 'formIdAttributes' setting so Drupal forms send a stable 'form_id' across AJAX redraws. - #29

Merged
AlexSkrypnyk merged 2 commits into
mainfrom
feature/26-stable-form-id
Nov 16, 2026
Merged

AlexSkrypnyk merged 2 commits into
mainfrom
feature/26-stable-form-id

Conversation

@AlexSkrypnyk

@AlexSkrypnyk AlexSkrypnyk commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Closes #26

Summary

form_id now comes from the attributes listed in a new formIdAttributes setting, and the Drupal bundles default it to ['data-drupal-selector', 'id']. So a Drupal form keeps the same form_id on form_start and every field_complete, even after an AJAX response redraws it.

formParams() in src/core/dom.js sent the marked element's id as it was. Drupal's Html::getUniqueId() adds a random --<token> suffix to a form's id every time it's rebuilt, for example when an AJAX submission comes back with validation errors. So 1 attempt reported webform-submission-contact-paragraph-40-add-form and then webform-submission-contact-paragraph-40-add-form--sKry3dUurl4, and the 2 values didn't group together in GA4.

After this merges, Drupal sites send the data-drupal-selector value as form_id. Drupal derives that value from the form ID without the suffix, and a marked wrapper without one still sends its id. Standalone and ESM bundles keep the ['id'] default, so what they send doesn't change. form_name and the form_start deduplication, which keys on the name, aren't touched. Sites can override the list through drupalSettings.axel.formIdAttributes or window.axelSettings, and a value that isn't an array of strings throws when the instance is created.

Before / After

BEFORE (Drupal, AJAX redraw)

  <form id="contact-form"              data-drupal-selector="contact-form">
      form_start      form_id: contact-form
      field_complete  form_id: contact-form
                 |  AJAX response with a validation error
                 v
  <form id="contact-form--sKry3dUurl4" data-drupal-selector="contact-form">
      field_complete  form_id: contact-form--sKry3dUurl4   <- new value per redraw

AFTER

  formIdAttributes = ['data-drupal-selector', 'id']   (Drupal default)
                     ['id']                           (core default)

  <form id="contact-form"              data-drupal-selector="contact-form">
      form_start      form_id: contact-form
      field_complete  form_id: contact-form
                 |  AJAX response with a validation error
                 v
  <form id="contact-form--sKry3dUurl4" data-drupal-selector="contact-form">
      field_complete  form_id: contact-form                <- stable

Changes

Core

  • src/core/core.js: added formIdAttributes: ['id'] to DEFAULT_SETTINGS. The constructor throws when the value isn't an array of strings, the same way it already checks types and collectors.
  • src/core/dom.js: formParams(form, settings) reads form_id from the first attribute in settings.formIdAttributes that has a non-empty value. It still passes the same settings to resolveUrl() for form_destination. Both form event types already pass axel.settings, so their call sites don't change.

Drupal integration

  • src/integrations/drupal.js: attach() sets formIdAttributes to ['data-drupal-selector', 'id'] underneath drupalSettings.axel, so a site setting still wins.

Tests

  • tests/core/event-types/form-start.test.js: covers the first listed attribute winning, falling back when the first attribute is missing or empty, and sending no form_id when no attribute matches or the list is empty.
  • tests/core/event-types/field-complete.test.js: field_complete reads the same attributes.
  • tests/core/core.test.js: the constructor throws on a string and on a list that holds a non-string.
  • tests/integrations/drupal.test.js: checks the Drupal default and the drupalSettings override, and that a form redrawn with a new --token id sends the same form_id on form_start and field_complete.

Docs

  • README.md: the form_id bullet explains the Drupal behavior, and the settings table has a formIdAttributes row.
  • docs/architecture/README.md: the Drupal start paragraph and the form event type bullets cover the new setting.

@AlexSkrypnyk AlexSkrypnyk added the A2 Worker 2 label Nov 16, 2026
@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

  • Run on-demand review

This review includes 9 billable files and costs up to $2.25.

  • Ask an admin to make reviews automatic

Open in CodeRabbit

Reviews can continue after your included limit without a manual trigger. An admin must approve usage-based billing.

Or wait 29 minutes for your next included review.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available. Your 91 included PR review attempts over the past 7 days set your current allowance at 1 review per hour.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Team
  • Run ID: 01df2b1e-bb2c-475d-a812-68e4271a0570
Commits

Reviewing files that changed from the base of the PR and between fbee9ce and 8ac22b3.

Files selected for processing: 9

  • README.md
  • docs/architecture/README.md
  • src/core/core.js
  • src/core/dom.js
  • src/integrations/drupal.js
  • tests/core/core.test.js
  • tests/core/event-types/field-complete.test.js
  • tests/core/event-types/form-start.test.js
  • tests/integrations/drupal.test.js
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@AlexSkrypnyk
AlexSkrypnyk force-pushed the feature/26-stable-form-id branch from 509e8dd to 4564ec7 Compare November 16, 2026 09:30
@AlexSkrypnyk AlexSkrypnyk added the Needs review Pull request needs a review from assigned developers label Nov 16, 2026
@AlexSkrypnyk
AlexSkrypnyk force-pushed the feature/26-stable-form-id branch from 4564ec7 to 8ac22b3 Compare November 16, 2026 09:46
@AlexSkrypnyk
AlexSkrypnyk merged commit 852410f into main Nov 16, 2026
7 checks passed
@AlexSkrypnyk
AlexSkrypnyk deleted the feature/26-stable-form-id branch November 16, 2026 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A2 Worker 2 Needs review Pull request needs a review from assigned developers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keep 'form_id' stable when a Drupal AJAX response redraws a form

1 participant