Comes up periodically that people want to alter the order of behaviors being run or only run a few of them. Instead of modifying Drupal.behaviors object we should have a way of controlling that explicitly (came up in #2362637: Editor.js attach/detach doesn't pass context all the way down). Having this would mean that the behavior system is justified and Drupal can't use events to do it's thing, see #1446166-39: Use JS events instead of Drupal.behaviors.

Introducing a new parameter, behaviorList, and a new function Drupal.behaviorList.

Drupal.attachBehaviors(document, drupalSettings, ['AJAX', 'editor', 'formUpdated', 'escapeAdmin']);

// Override default behavior ordering
Drupal.behaviorList = function (event, trigger) { return myCustomOrderedBehaviorsArray; };

Same for Drupal.detachBehaviors. behaviorList defaults to the whole list of behaviors. This can also be used to alter the order in which behaviors are run.

Beta phase evaluation

@TODO

Reference: https://www.drupal.org/core/beta-changes
Issue category Task
Issue priority Normal
Prioritized changes Main goal of issue is adding flexibility to drupal behaviors management.
  • Allow a supported way to alter behavior order and make the behavior order more predictable by having a Drupal.behaviorList
  • As we get more and more JS on pages, being able to selectively run behaviors (or exclude some) is good.
Disruption API addition, no API break.
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

nod_’s picture

Status: Needs review » Needs work

Umm actually having a Drupal.behaviorList function would be better, it can be overriden by contrib.

nod_’s picture

Status: Needs work » Needs review
FileSize
4.97 KB

Not quite ready yet, doc probably needs work.

nod_’s picture

FileSize
4.98 KB

Reroll

This would be a solution for the weight issue #1945262: Replace custom weights with dependencies in library declarations; introduce "before" and "after" for conditional ordering, make the after/before in the JS, not in the file ordering.

lauriii’s picture

Status: Needs review » Reviewed & tested by the community

I tested this manually and it does actually work! Also the code changes looks clean. Thanks nod_ for your hero coding on this issue!!

Wim Leers’s picture

Issue tags: +JavaScript
+++ b/core/misc/drupal.js
@@ -18,6 +18,21 @@ if (window.jQuery) {
+   * @param {String} event
...
+   * @param {String} [trigger]
...
+   * @return {Array}

Is this {Something} stuff now the norm/standard/rule?

nod_’s picture

Will be, yes. Once I get the the courage to work on #2182153: [Meta] Document Drupal JavaScript using JSDoc.

nod_’s picture

Wim Leers’s picture

Thanks for both #6 and #7!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 3: core-js-behaviorList-2367655-3.patch, failed testing.

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 3: core-js-behaviorList-2367655-3.patch, failed testing.

nod_’s picture

Status: Needs review » Needs work

The last submitted patch, 12: core-js-behaviorList-2367655-12.patch, failed testing.

nod_’s picture

Status: Needs work » Needs review

lauriii’s picture

Status: Needs review » Reviewed & tested by the community

Looks good!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 12: core-js-behaviorList-2367655-12.patch, failed testing.

Status: Needs work » Needs review
nod_’s picture

Status: Needs review » Reviewed & tested by the community
alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs issue summary update

This issue is a normal task so we need to outline how it fits within the allowable Drupal 8 beta criteria. Can someone add Drupal 8 beta phase evaluation template to the issue summary.

I've confirmed that no existing usages call attachBehaviors or detachBehaviors with an additional argument.

Manuel Garcia’s picture

Issue summary: View changes

Pasted in the template.

nod_’s picture

Issue summary: View changes
Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs issue summary update +Needs change record
alexpott’s picture

Status: Reviewed & tested by the community » Needs work

I'm not really sure how this issue passes the beta evaluation :( Also we need a CR if this is to be considered for commit anyway.

Going to discuss with the other committers.

nod_’s picture

At least I want Drupal.behaviorList function exposed so we can override it when we need to mess with behavior initialization. The behaviorList parameter is welcome but not required.

Only reason it didn't go through earlier was lack of review, it was ready 6 months ago despite my comment in #2, the additional changes needed to end up with #12 are cosmetic. I'll do a CR this afternoon.

nod_’s picture

Version: 8.0.x-dev » 8.1.x-dev
Status: Needs work » Needs review

Moving to 8.1.x

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Wim Leers’s picture

Issue tags: +Needs tests

We can now write JS tests.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Status: Needs review » Needs work

The last submitted patch, 12: core-js-behaviorList-2367655-12.patch, failed testing. View results

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

lucasantunes’s picture

Issue tags: -JavaScript +JavaScript

I'm sorry, but I don't get why don't we have events dispatched for every module attach/detach.

It would be fairly easy to run my code if I need module X to attach first, something like

$(context).once('mymodule').on('d-moduleX-attach', function () {
  // Code here.
});
nod_’s picture

Reroll :p

The issue is not to react on attach/detach, with what you're proposing the issue would be to change the order which the detach events are called for each "module", how do you make sure the "contextual" module detach before the "toolbar" module for example?

Using events means replacing the line behaviors[behaviorName].attach(context, settings); with something like $(context).trigger(`d-${behaviorName}-attach`, [context, settings]); and change all the behavior code ever written to use listeners. That doesn't solve the ordering problem I raised in the IS.

I still think it's a good idea even if I'm not entirely sold on the finer details of this implementation anymore but it's a quick reroll so why not.

nod_’s picture

Status: Needs work » Needs review
lauriii’s picture

Do we have any potential use cases for this? This is probably not something that many contrib projects would want to do but the current implementation would make it difficult for multiple contrib projects to override the behavior list. I'm also wondering what are the use cases for passing the behavior list for the when attaching behaviors? I believe you explained all of this to me ~6 years ago but unfortunately I can't recall any of that.

andypost’s picture

nod_’s picture

@lauriii 2020, meet @lauriii 2015: #2474019: Implement before and after behavior ordering :p

Thinking about it this issue is not really necessary, if we really need Proxy can be used for that. Doesn't work on IE11 but at this point you're better off with overriding attachbehaviors.

var behaviors = Drupal.behaviors; 
Drupal.behaviors = new Proxy(behaviors, {
  ownKeys: function (target) {
    return Object.keys(target);
  },
});

Like wim said in #2474019-2: Implement before and after behavior ordering would be best to take care of this at the library level.

lucasantunes’s picture

@nod_ Yeah, I think my question maybe should be another issue hehe... Now I got this issue right. Sorry about that!

I think the proposal is great, though. It's something I've been wanting for a long time, and will definitely be of great use. We often needed to make some ugly workarounds using either setTimeout/setInterval or a MutationObserver simply because we couldn't avoid a behavior being run before ours.

Patch looks good to me!

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
FileSize
165 bytes

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.