1

At some point in an update in the last few months, Drupal (7) started routinely triggering attached behaviours twice.

At first I thought "this is my fault, my behaviours should check to see if they've been run in this context already", and I set about using jQuery's once() (and other such solutions).

However I'm convinced now that this is a wider problem. e.g. the admin_menu module generates 2 admin menus.

Has anyone found this and the cause of it and a solution (beyond patching every module in use to check for duplicate calls)? I'm not sure where to start looking!

Nb. I've said "drupal" is doing this, I suppose it could be a contrib module that's causing the behaviour. I've seen it across several sites (I manage a dozen or so)

3
  • 1
    Drupal.attachBehaviors() (which executes all behaviors) is executed after every ajax load. It is built to be executed multiple times. This is why $.once is used, so that you don't re-run code unnecessarily.
    – Jaypan
    May 8, 2019 at 22:27
  • @Jaypan Yes but with a context for the DOM node being replaced by the ajax call. Drupal makes an initial call to attach behaviours for the HTMLDocument node on page load. The issue seems to be that it is calling attach behaviours twice for the HTMLDocument (root) context. It didn't used to do this. May 9, 2019 at 11:19
  • @artfulrobot on pages that have ajax, it will call twice unless you use .once(). So probably before there was no ajax on that page which explains why "It didn't used to do this."
    – No Sssweat
    May 9, 2019 at 11:20

1 Answer 1

1

I put a breakpoint on one script's attach behaviours and inspected the call stack both times it was called.

The first time was expected, from drupal.js, and the second turned out to be from a call in the EU Cookie Compliance module. This turned out to be a bug in the EU Cookie Compliance module.

Thanks for comments. I'll leave this here in case anyone else of the 70.5k users of that module experience the same issue!

Your Answer

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

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