1
+ ( function ( $ , Drupal , once ) {
2
+ 'use strict' ;
3
+
4
+ Drupal . behaviors . organizationToggle = {
5
+ attach : function ( context , settings ) {
6
+ once ( 'organization-toggle' , '[name="field_access_organization[0][target_id]"]' , context ) . forEach ( function ( element ) {
7
+ var $orgField = $ ( element ) ;
8
+ var $institutionField = $ ( '[name="field_institution[0][value]"]' ) . closest ( '.field--name-field-institution' ) ;
9
+
10
+ function toggleInstitutionField ( ) {
11
+ var selectedValue = $orgField . val ( ) ;
12
+
13
+ // Check if the value contains "Other" and node ID 3695
14
+ // Autocomplete fields might have format like "Other (3695)"
15
+ if ( selectedValue === '3695' ||
16
+ ( selectedValue && selectedValue . includes ( '3695' ) ) ||
17
+ ( selectedValue && selectedValue . toLowerCase ( ) . includes ( 'other' ) ) ) {
18
+ $institutionField . show ( ) ;
19
+ $institutionField . find ( 'input' ) . prop ( 'required' , true ) ;
20
+ } else {
21
+ $institutionField . hide ( ) ;
22
+ $institutionField . find ( 'input' ) . prop ( 'required' , false ) ;
23
+ }
24
+ }
25
+
26
+ // Initial state
27
+ toggleInstitutionField ( ) ;
28
+
29
+ // Listen for changes - multiple events to catch all scenarios
30
+ $orgField . on ( 'change keyup input' , toggleInstitutionField ) ;
31
+
32
+ // Handle autocomplete selections
33
+ $orgField . on ( 'autocompleteselect autocompletechange' , function ( event , ui ) {
34
+ setTimeout ( toggleInstitutionField , 100 ) ;
35
+ } ) ;
36
+
37
+ // Also check periodically in case value changes via other means
38
+ setInterval ( function ( ) {
39
+ var currentValue = $orgField . val ( ) ;
40
+ if ( currentValue !== $orgField . data ( 'lastValue' ) ) {
41
+ $orgField . data ( 'lastValue' , currentValue ) ;
42
+ toggleInstitutionField ( ) ;
43
+ }
44
+ } , 500 ) ;
45
+ } ) ;
46
+ }
47
+ } ;
48
+
49
+ } ) ( jQuery , Drupal , once ) ;
0 commit comments