Problem/Motivation
Sometimes behaviors have to be ordered. This could be done with the change we added in #2367655: Control the list of behaviors run by Drupal.attachBehaviors. Ordering the whole behavior list is not very intuitive when you just have to run the behavior before/after a specific behavior.
Behavior ordering is needed to be able to remove weights from library definitions: #1945262: Replace custom weights with dependencies in library declarations; introduce "before" and "after" for conditional ordering.
Proposed resolution
To make this easier to use we want to add a way to define a list of behaviors that should be ran before or after specific behavior.
Remaining tasks
-
User interface changes
-
API changes
Add a new keys to behavior object:
Drupal.behaviors.newBehavior = {
attach: function () {},
detach: function () {},
before: ['contextual'],
after: ['toolbar']
};
Comment | File | Size | Author |
---|---|---|---|
#20 | 2474019-nr-bot.txt | 168 bytes | needs-review-queue-bot |
#10 | implement_before_after_dependencies-2474019-10.patch | 6.04 KB | slydevil |
Comments
Comment #1
nod_Comment #2
Wim LeersWe shouldn't do this, but fix #1945262: Replace custom weights with dependencies in library declarations; introduce "before" and "after" for conditional ordering instead. Then you don't need to specify it in JS, but in the asset library metadata. Because then it'll continue to work when the dependency tree. Specifying this in the JS will not automatically be made aware of additional library dependencies/before/after metadata, but specifying it in the asset library metadata will.
Comment #3
nod_Have the before after suff in php based on files then?
I'm fine with it being in PHP. I'm actually a bit worried the whole resolution stuff in the js for before/after is going to require too much code.
Comment #4
Wim LeersYes.
core/drupal
depends oncore/drupalSettings
anyway. So we can pass the relevant asset library ordering information inwindow.drupalSettings
.Comment #10
slydevil CreditAttribution: slydevil commentedAlright, I took a stab at this. Added logic to the AssetResolver class to modify the javascript weights based on 'before' or 'after' dependencies.
I didn't take into account scenarios where there are both 'before' and 'after' dependencies and the weight of those dependencies is opposite of what it needs to be. For example, fileB has a 'before' dependency on fileA and an 'after' dependency of fileC:
fileA = 0.091
fileB = 0.092
fileC = 0.093
The expected order would be:
fileC = 0.091
fileB = 0.092
fileA = 0.093
However, the patch will apply the 'before' and then the 'after' without modifying the weight of the dependencies. So the actual order would be:
fileA = 0.091
fileC = 0.093
fileB = 0.0931
Coding this will be complex and I think this patch is a good start.
Comment #20
needs-review-queue-bot CreditAttribution: needs-review-queue-bot as a volunteer commentedThe 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.