Problem/Motivation
conditional_fields.js adds additional comparators in Drupal.states.Dependent.comparisons
, like this:
Drupal.behaviors.statesModification = {
weight: -10,
attach: function (context, settings) {
if (Drupal.states) {
[..]
Drupal.states.Dependent.comparisons['Array'] = function (reference, value) {...}
[..]
Drupal.states.Dependent.comparisons.Object = function (reference, value) {...}
Unfortunately, in some cases, the conditions have to be evaluated _before_ this behavior fires.
This is because the weight: -10
does not really change the order of behaviors.
(unfortunately,
https://www.drupal.org/project/behavior_weights →
was not ported to D8/D8/D10)
In our case, this was when using slim_select with conditional_fields.
But it seems the problem is really independent of slim_select.
I used the js debugger to check the order of behaviors, and the Drupal.behaviors.statesModification was _not_ pushed to the start according to its weight.
Steps to reproduce
- Install slim_select, conditional_fields.
- New taxonomy vocab, create a few terms.
- New content type.
- New field, taxonomy reference, multiple value, widget = multi select (will be converted by slim_select)
- New field, text.
- New field condition, let the text field be visible dependent on a specific term.
- Visit the form to create new content of the type.
- Add the special term in the widget.
- (-> text field shows up)
- Fill the text field.
- Save.
- Edit again.
Expected:
When editing again, the text field is visible.
Actual:
When editing again, the text field is not visible.
Proposed resolution
We need to add the new comparators earlier in the request.
We can simply skip Drupal.behaviors and do this instead:
Add the comparators as soon as the js file is included.
Use (() => {...})()
for encapsulation. Or maybe don't.
(() => {
if (Drupal.states) {
[...]
Drupal.states.Dependent.comparisons['Array'] = function (reference, value) {...}
[...]
})();
Remaining tasks
User interface changes
API changes
Data model changes