Multiple click event handlers in updateFormId

Created on 8 January 2025, 3 months ago

Problem/Motivation

The event listener in https://git.drupalcode.org/project/gin/-/blob/4.0.x/js/more_actions.js?r... might get added multiple times, which is an issue if e.g. a module wants to act on when the form is submitted. And even if the issue is not visible to users, the event should still be added only once per buttonSelector.

Steps to reproduce

Add this to a custom module's JS:

const testSubmit = (form) => {
    form.addEventListener('submit', (event) => {
      console.log(event);
    });
  }

  Drupal.behaviors.testSubmitEvents = {
    attach(context) {
      once(
        'test-submit-events',
        context.getElementsByClassName('node-article-edit-form')
      ).forEach(testSubmit);
    },
  };

...and make sure it is loaded.

  1. Now edit a node and save. You should see one event in the console.
  2. Edit the node again but this time do a couple of things that trigger AJAX (e.g. use autocomplete fields).
  3. Click save.
  4. You should get multiple events in the console, one per each AJAX request.

Proposed resolution

Use once() to make sure the event handler gets added only once per buttonSelector.

🐛 Bug report
Status

Active

Version

4.0

Component

Code

Created by

🇫🇮Finland Tuuuukka

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

Comments & Activities

Production build 0.71.5 2024