@@ -13,8 +13,10 @@ class IndexFieldCallbacks {
13
13
* Submit callback for the Index Add button.
14
14
*/
15
15
public static function indexAddSubformCallback (array &$ form , FormStateInterface $ form_state ) {
16
+ // Get the button's trigger value.
16
17
$ trigger = $ form_state ->getTriggeringElement ();
17
18
$ op = $ trigger ['#op ' ];
19
+ // Get the current fields data.
18
20
$ current_dictionary_fields = $ form ["field_json_metadata " ]["widget " ][0 ]["dictionary_fields " ]["data " ]["#rows " ] ?? [];
19
21
$ current_index = $ form ["field_json_metadata " ]["widget " ][0 ]['indexes ' ]["data " ]["#rows " ] ?? [];
20
22
$ current_index_fields = $ form ["field_json_metadata " ]["widget " ][0 ]['indexes ' ]["fields " ]["data " ]["#rows " ] ?? [];
@@ -23,169 +25,252 @@ public static function indexAddSubformCallback(array &$form, FormStateInterface
23
25
$ form_state ->set ('current_index_fields ' , $ current_index_fields );
24
26
}
25
27
28
+ // If cancelling index field.
26
29
if ($ op === 'cancel_index_field ' ) {
30
+ // Set the display to show the current index fields values.
27
31
$ form_state ->set ('cancel_index_field ' , TRUE );
32
+ // Hide the field collection.
28
33
$ form_state ->set ('add_new_index_field ' , '' );
29
34
}
30
35
36
+ // If adding new index field, this is triggered when you click the button to
37
+ // 'Add'
31
38
if ($ op === 'add_new_index_field ' ) {
39
+ // @TODO not being used so maybe removable or update the comment.
32
40
$ form_state ->set ('add_index_field ' , '' );
41
+ // Get the form fields for adding new index fields.
33
42
$ add_index_fields = IndexFieldAddCreation::addIndexFields ($ current_index_fields );
43
+ // Set the fields in the field collection.
34
44
$ form_state ->set ('add_new_index_field ' , $ add_index_fields );
45
+ // @TODO not being used so maybe removable or update the comment.
35
46
$ form_state ->set ('index_added ' , FALSE );
47
+ // @TODO not being used so maybe removable or update the comment.
36
48
$ form_state ->set ('adding_new_index_fields ' , TRUE );
37
49
}
38
50
51
+ // If saving new index field.
39
52
if ($ op === 'add_index_field ' ) {
53
+ // @TODO not being used so maybe removable or update the comment.
40
54
$ form_state ->set ('add_new_index_field ' , '' );
55
+ // Get and save the entered values.
41
56
$ form_state ->set ('new_index_fields ' , $ form_state ->getUserInput ());
57
+ // @TODO not being used so maybe removable or update the comment.
42
58
$ form_state ->set ('add ' , TRUE );
59
+ // Set the display to show the entered values.
43
60
$ form_state ->set ('cancel_index_field ' , FALSE );
61
+ // @TODO not being used so maybe removable or update the comment.
44
62
$ form_state ->set ('adding_new_index_fields ' , FALSE );
45
63
}
46
64
65
+ // Let's retain the fields that are already stored on the form,
66
+ // but aren't currently being modified.
47
67
$ form_state ->set ('current_dictionary_fields ' , $ current_dictionary_fields );
48
68
$ form_state ->set ('current_index ' , $ current_index );
49
69
$ form_state ->set ('current_index_fields ' , $ current_index_fields );
70
+ // Let's rebuild the form.
50
71
$ form_state ->setRebuild ();
51
72
}
52
73
53
74
/**
54
75
* Submit callback for the Index Add button.
55
76
*/
56
77
public static function indexAddCallback (array &$ form , FormStateInterface $ form_state ) {
78
+ // Get the button's trigger value.
57
79
$ trigger = $ form_state ->getTriggeringElement ();
58
80
$ op = $ trigger ['#op ' ];
81
+ // Get the current fields data.
59
82
$ current_dictionary_fields = $ form ["field_json_metadata " ]["widget " ][0 ]["dictionary_fields " ]["data " ]["#rows " ];
83
+ $ current_index = $ form ["field_json_metadata " ]["widget " ][0 ]["indexes " ]["data " ]["#rows " ];
84
+ $ current_index_fields = $ form ["field_json_metadata " ]["widget " ][0 ]['indexes ' ]["fields " ]["data " ]["#rows " ] ?? NULL ;
85
+ // Initialize the various field storage values.
60
86
$ form_state ->set ('add_new_index_field ' , '' );
61
87
$ form_state ->set ('new_index_fields ' , '' );
62
88
$ form_state ->set ('add_new_index ' , '' );
89
+ // @TODO not being used so maybe removable or update the comment.
63
90
$ form_state ->set ('adding_new_index_fields ' , FALSE );
64
- $ current_index = $ form ["field_json_metadata " ]["widget " ][0 ]["indexes " ]["data " ]["#rows " ];
65
- $ current_index_fields = $ form ["field_json_metadata " ]["widget " ][0 ]['indexes ' ]["fields " ]["data " ]["#rows " ] ?? NULL ;
66
91
67
92
if ($ current_index ) {
68
93
$ form_state ->set ('current_index ' , $ current_index );
69
94
}
70
95
96
+ // If cancelling index.
71
97
if ($ op === 'cancel_index ' ) {
98
+ // Set the display to show the current index values.
72
99
$ form_state ->set ('cancel_index ' , TRUE );
73
100
}
74
101
102
+ // If adding new index, this is triggered when you click the button to
103
+ // 'Add index'
75
104
if ($ op === 'add_new_index ' ) {
105
+ // Get the form fields for adding new index.
76
106
$ add_new_index = IndexFieldAddCreation::addIndex ();
107
+ // Set the new_index values to empty.
77
108
$ form_state ->set ('new_index ' , '' );
109
+ // Set the fields in the field collection.
78
110
$ form_state ->set ('add_new_index ' , $ add_new_index );
79
111
}
80
112
113
+ // If saving new index.
81
114
if ($ op === 'add_index ' ) {
115
+ // Empty the fields in the field collection.
82
116
$ form_state ->set ('add_new_index ' , '' );
117
+ // Get and save the entered values.
83
118
$ form_state ->set ('new_index ' , $ form_state ->getUserInput ());
119
+ // @TODO not being used so maybe removable or update the comment.
84
120
$ form_state ->set ('add ' , TRUE );
121
+ // @TODO not being used so maybe removable or update the comment.
85
122
$ form_state ->set ('index_added ' , TRUE );
123
+ // Set the display to show the entered values.
86
124
$ form_state ->set ('cancel_index ' , FALSE );
87
125
}
88
126
127
+ // Let's retain the fields that are already stored on the form,
128
+ // but aren't currently being modified.
89
129
$ form_state ->set ('current_dictionary_fields ' , $ current_dictionary_fields );
90
130
$ form_state ->set ('current_index ' , $ current_index );
91
131
$ form_state ->set ('current_index_fields ' , $ current_index_fields );
132
+ // Let's rebuild the form.
92
133
$ form_state ->setRebuild ();
93
134
}
94
135
95
136
/**
96
137
* Submit callback for the Index Field Edit button.
97
138
*/
98
139
public static function indexEditSubformCallback (array &$ form , FormStateInterface $ form_state ) {
140
+ // Get the button's trigger value.
99
141
$ trigger = $ form_state ->getTriggeringElement ();
142
+ $ op = $ trigger ['#op ' ];
143
+ // The location of the index field is stored in the operation key.
144
+ // We split the key to get the index field location.
145
+ $ op_index = explode ('_ ' , $ trigger ['#op ' ]);
146
+ // Get the current fields data.
100
147
$ current_index_fields = $ form ["field_json_metadata " ]["widget " ][0 ]["indexes " ]["fields " ]["data " ]["#rows " ];
101
148
$ current_dictionary_fields = $ form ["field_json_metadata " ]["widget " ][0 ]["dictionary_fields " ]["data " ]["#rows " ];
102
- $ op = $ trigger ['#op ' ];
103
- $ op_index = explode ("_ " , $ trigger ['#op ' ]);
104
149
$ currently_modifying_index_fields = $ form_state ->get ('index_fields_being_modified ' ) != NULL ? $ form_state ->get ('index_fields_being_modified ' ) : [];
105
150
$ currently_modifying = $ form_state ->get ('dictionary_fields_being_modified ' ) != NULL ? $ form_state ->get ('dictionary_fields_being_modified ' ) : [];
106
151
152
+ // If the op (trigger) contains abort,
153
+ // We're canceling the field we're currently modifying so unset it.
107
154
if (str_contains ($ op , 'abort ' )) {
108
155
unset($ currently_modifying_index_fields [$ op_index [4 ]]);
109
156
}
110
157
158
+ // If the op (trigger) contains delete,
159
+ // We're deleting the field we're editing so...
111
160
if (str_contains ($ op , 'delete ' )) {
161
+ // Unset it from being currently modified.
112
162
unset($ currently_modifying_index_fields [$ op_index [4 ]]);
163
+ // Remove the respective field/data from the form.
113
164
unset($ current_index_fields [$ op_index [4 ]]);
114
165
}
115
166
167
+ // If the op (trigger) contains update,
168
+ // We're saving the field we're editing so...
116
169
if (str_contains ($ op , 'update ' )) {
170
+ // Get the entered data.
117
171
$ update_values = $ form_state ->getUserInput ();
172
+ // Unset the respective currently modifying field.
118
173
unset($ currently_modifying_index_fields [$ op_index [4 ]]);
174
+ // Unset the respective field/data from the form.
119
175
unset($ current_index_fields [$ op_index [4 ]]);
176
+ // Update the respective current field data with our new input data.
120
177
$ current_index_fields [$ op_index [4 ]] = IndexFieldValues::updateIndexFieldValues ($ op_index [4 ], $ update_values , $ current_index_fields );
178
+ // Sort the current index fields data.
121
179
ksort ($ current_index_fields );
122
-
123
- }
124
-
125
- if (str_contains ($ op , 'edit_index_key ' )) {
126
- $ currently_modifying_index_fields [$ op_index [4 ]] = $ current_index_fields [$ op_index [4 ]];
127
180
}
128
181
182
+ // If the op (trigger) contains edit
183
+ // We're editing a specific field so...
129
184
if (str_contains ($ op , 'edit_index_field ' )) {
185
+ // Set the field we're modifying to that field.
130
186
$ currently_modifying_index_fields [$ op_index [4 ]] = $ current_index_fields [$ op_index [4 ]];
131
187
}
132
188
189
+ // Let's retain the fields that are being modified.
133
190
$ form_state ->set ('dictionary_fields_being_modified ' , $ currently_modifying );
134
191
$ form_state ->set ('index_fields_being_modified ' , $ currently_modifying_index_fields );
192
+ // Let's retain the fields that are already stored on the form,
193
+ // but aren't currently being modified.
135
194
$ form_state ->set ('current_index_fields ' , $ current_index_fields );
136
195
$ form_state ->set ('current_dictionary_fields ' , $ current_dictionary_fields );
196
+ // Let's rebuild the form.
137
197
$ form_state ->setRebuild ();
138
198
}
139
199
140
200
/**
141
201
* Submit callback for the Index Edit button.
142
202
*/
143
203
public static function indexEditCallback (array &$ form , FormStateInterface $ form_state ) {
204
+ // Get the button's trigger value.
144
205
$ trigger = $ form_state ->getTriggeringElement ();
206
+ $ op = $ trigger ['#op ' ];
207
+ // The location of the index is stored in the operation key.
208
+ // We split the key to get the index location.
209
+ $ op_index = explode ('_ ' , $ trigger ['#op ' ]);
210
+ // Get the current fields data.
145
211
$ current_index_fields = $ form ['field_json_metadata ' ]['widget ' ][0 ]['indexes ' ]['fields ' ]['data ' ]['#rows ' ] ?? [];
146
212
$ current_index = $ form ['field_json_metadata ' ]['widget ' ][0 ]['indexes ' ]['data ' ]['#rows ' ] ?? [];
147
213
$ current_dictionary_fields = $ form ['field_json_metadata ' ]['widget ' ][0 ]['dictionary_fields ' ]['data ' ]['#rows ' ] ?? [];
148
- $ op = $ trigger ['#op ' ];
149
- $ op_index = explode ('_ ' , $ trigger ['#op ' ]);
150
214
$ currently_modifying_index_fields = $ form_state ->get ('index_fields_being_modified ' ) != NULL ? $ form_state ->get ('index_fields_being_modified ' ) : [];
151
215
$ currently_modifying_index = $ form_state ->get ('index_being_modified ' ) != NULL ? $ form_state ->get ('index_being_modified ' ) : [];
152
216
$ currently_modifying_dictionary_fields = $ form_state ->get ('dictionary_fields_being_modified ' ) != NULL ? $ form_state ->get ('dictionary_fields_being_modified ' ) : [];
153
217
218
+ // If the op (trigger) contains abort,
219
+ // We're canceling the index we're currently modifying so unset it.
154
220
if (str_contains ($ op , 'abort_index_key ' )) {
155
221
unset($ currently_modifying_index [$ op_index [3 ]]);
156
222
}
157
-
223
+ // We're canceling the index field we're currently modifying so unset it.
158
224
if (str_contains ($ op , 'abort_index_field_key ' )) {
159
225
unset($ currently_modifying_index_fields [$ op_index [4 ]]);
160
226
}
161
227
228
+ // If the op (trigger) contains delete,
229
+ // We're deleting the index we're editing so...
162
230
if (str_contains ($ op , 'delete_index_key ' )) {
231
+ // Unset it from being currently modified.
163
232
unset($ currently_modifying_index [$ op_index [3 ]]);
233
+ // Remove the respective field/data from the form.
164
234
unset($ current_index [$ op_index [3 ]]);
165
235
}
166
-
236
+ // We're deleting the index field we're editing so...
167
237
if (str_contains ($ op , 'delete_index_field_key ' )) {
238
+ // Unset it from being currently modified.
168
239
unset($ currently_modifying_index_fields [$ op_index [4 ]]);
240
+ // Remove the respective field/data from the form.
169
241
unset($ current_index_fields [$ op_index [4 ]]);
170
242
}
171
243
244
+ // If the op (trigger) contains update,
245
+ // We're saving the field we're editing so...
172
246
if (str_contains ($ op , 'update ' )) {
247
+ // Get the entered data.
173
248
$ update_values = $ form_state ->getUserInput ();
249
+ // Update the respective current field data with our new input data.
174
250
$ current_index [$ op_index [3 ]] = IndexFieldValues::updateIndexValues ($ op_index [3 ], $ update_values , $ current_index );
251
+ // Unset the respective field/data from the form.
175
252
unset($ currently_modifying_index [$ op_index [3 ]]);
253
+ // Sort the current index data.
176
254
ksort ($ current_index );
177
255
}
178
256
257
+ // If the op (trigger) contains edit
258
+ // We're editing a specific field so...
179
259
if (str_contains ($ op , 'edit ' )) {
260
+ // Set the field we're modifying to that field.
180
261
$ currently_modifying_index [$ op_index [3 ]] = $ current_index [$ op_index [3 ]];
181
262
}
182
263
264
+ // Let's retain the fields that are being modified.
183
265
$ form_state ->set ('dictionary_fields_being_modified ' , $ currently_modifying_dictionary_fields );
184
266
$ form_state ->set ('index_fields_being_modified ' , $ currently_modifying_index_fields );
185
267
$ form_state ->set ('index_being_modified ' , $ currently_modifying_index );
268
+ // Let's retain the fields that are already stored on the form,
269
+ // but aren't currently being modified.
186
270
$ form_state ->set ('current_index_fields ' , $ current_index_fields );
187
271
$ form_state ->set ('current_index ' , $ current_index );
188
272
$ form_state ->set ('current_dictionary_fields ' , $ current_dictionary_fields );
273
+ // Let's rebuild the form.
189
274
$ form_state ->setRebuild ();
190
275
}
191
276
@@ -220,15 +305,15 @@ public static function indexFormAjax(array &$form, FormStateInterface $form_stat
220
305
}
221
306
222
307
/**
223
- * Ajax callback to return index fields fieldset with Add Field button.
308
+ * Ajax callback to return index fields fieldset with ' Add Field' button.
224
309
*/
225
310
public static function subIndexFormFieldAjax (array &$ form , FormStateInterface $ form_state ) {
226
311
return $ form ["field_json_metadata " ]["widget " ][0 ]["indexes " ]["field_collection " ]["group " ]["index " ]["fields " ];
227
312
}
228
313
229
314
/**
230
- * Ajax callback to return index fields fieldset with existing fields and Add
231
- * Field button.
315
+ * Ajax callback to return index fields fieldset with existing fields and ' Add
316
+ * Field' button.
232
317
*/
233
318
public static function subIndexFormExistingFieldAjax (array &$ form , FormStateInterface $ form_state ) {
234
319
$ form ["field_json_metadata " ]["widget " ][0 ]["indexes " ]["field_collection " ]["group " ]["index " ]["fields " ]["add_row_button " ]['#access ' ] = TRUE ;
0 commit comments