Skip to content

Commit 4b4cf50

Browse files
dmundraBeth Jacobson
authored and
Beth Jacobson
committed
Adding code comments. (#4289)
1 parent c7d7cfe commit 4b4cf50

10 files changed

+140
-34
lines changed

modules/data_dictionary_widget/data_dictionary_widget.module

+11-5
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ function data_dictionary_widget_form_alter(&$form, &$form_state, $form_id) {
102102
}
103103
}
104104

105-
// If we are saving a data dictionary alter the submit.
105+
// If we are saving a data dictionary alter the submit action.
106106
foreach (array_keys($form['actions']) as $action) {
107107
if ( isset($form['actions'][$action]['#type'])
108108
&& $form['actions'][$action]['#type'] === 'submit'
@@ -112,11 +112,15 @@ function data_dictionary_widget_form_alter(&$form, &$form_state, $form_id) {
112112
}
113113

114114
$form['#validate'][] = 'data_dictionary_widget_validate_unique_identifier';
115+
// Check for existing dictionary and index fields.
115116
$current_dictionary_fields = !empty($form["field_json_metadata"]["widget"][0]["dictionary_fields"]["current_dictionary_fields"]) ? $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["current_dictionary_fields"] : NULL;
116117
$current_index_fields = !empty($form["field_json_metadata"]["widget"][0]["indexes"]["current_index"]) ? $form["field_json_metadata"]["widget"][0]["indexes"]["current_index"] : NULL;
117118

118119
// The form element render array prefers child keys to be stored as arrays with a #value property.
119120
if ($current_dictionary_fields) {
121+
// Create an array to copy the formatted array structure.
122+
$formatted_current_fields = [];
123+
120124
foreach ($current_dictionary_fields as $key => $value) {
121125
$keys = array_keys($value);
122126
$formatted_current_fields[$key] = [];
@@ -142,14 +146,16 @@ function data_dictionary_widget_form_alter(&$form, &$form_state, $form_id) {
142146
$fieldValue = ['#value' => $fieldValue];
143147
}
144148
}
145-
} else {
146-
// For non-'fields' keys, add '#value' key to the value
147-
$value = ['#value' => $value];
149+
}
150+
else {
151+
// For non-'fields' keys, add '#value' key to the value
152+
$value = ['#value' => $value];
148153
}
149154
}
150155
}
151156

152-
$form["field_json_metadata"]["widget"][0]["indexes"]["current_index"] = $current_index_fields;
157+
// Note the current_index_fields was modified in memory instead of copying the array.
158+
$form["field_json_metadata"]["widget"][0]["indexes"]["current_index"] = $current_index_fields;
153159
}
154160

155161
// Set the default value of the identifier field to a randomly generated uuid.

modules/data_dictionary_widget/src/Fields/FieldCallbacks.php

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public static function addSubformCallback(array &$form, FormStateInterface $form
101101
$trigger = $form_state->getTriggeringElement();
102102
$op = $trigger['#op'];
103103
$form_state->set('add_new_field', '');
104+
// Get the current fields data.
104105
$current_dictionary_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"];
105106
$current_index = $form["field_json_metadata"]["widget"][0]['indexes']["data"]["#rows"];
106107
$current_index_fields = $form["field_json_metadata"]["widget"][0]['indexes']["fields"]["data"]["#rows"] ?? [];

modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,12 @@ public static function editIndexButtons($indexKey) {
113113
public static function submitIndexFieldButton($location, $indexKey) {
114114
$callbackClass = $location == 'edit' ? 'indexEditSubformCallback' : 'indexAddSubformCallback';
115115
$op = !empty($indexKey) ? 'update_' . $indexKey : 'add_index_field';
116+
// @TODO fix the 'Add ' to drop the space, this will need the test to be
117+
// updated as well.
116118
$value = $location == 'edit' ? 'Save' : 'Add ';
117-
$function = $location == 'edit' ? 'subIndexFormAjax' : 'subIndexFormAjax';
119+
// Index fields cannot be edited once submitted so we use the same function
120+
// for both add and edit.
121+
$function = 'subIndexFormAjax';
118122
$edit_index_button = [
119123
'#type' => 'submit',
120124
'#value' => $value,

modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php

+101-16
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ class IndexFieldCallbacks {
1313
* Submit callback for the Index Add button.
1414
*/
1515
public static function indexAddSubformCallback(array &$form, FormStateInterface $form_state) {
16+
// Get the button's trigger value.
1617
$trigger = $form_state->getTriggeringElement();
1718
$op = $trigger['#op'];
19+
// Get the current fields data.
1820
$current_dictionary_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"] ?? [];
1921
$current_index = $form["field_json_metadata"]["widget"][0]['indexes']["data"]["#rows"] ?? [];
2022
$current_index_fields = $form["field_json_metadata"]["widget"][0]['indexes']["fields"]["data"]["#rows"] ?? [];
@@ -23,169 +25,252 @@ public static function indexAddSubformCallback(array &$form, FormStateInterface
2325
$form_state->set('current_index_fields', $current_index_fields);
2426
}
2527

28+
// If cancelling index field.
2629
if ($op === 'cancel_index_field') {
30+
// Set the display to show the current index fields values.
2731
$form_state->set('cancel_index_field', TRUE);
32+
// Hide the field collection.
2833
$form_state->set('add_new_index_field', '');
2934
}
3035

36+
// If adding new index field, this is triggered when you click the button to
37+
// 'Add'
3138
if ($op === 'add_new_index_field') {
39+
// @TODO not being used so maybe removable or update the comment.
3240
$form_state->set('add_index_field', '');
41+
// Get the form fields for adding new index fields.
3342
$add_index_fields = IndexFieldAddCreation::addIndexFields($current_index_fields);
43+
// Set the fields in the field collection.
3444
$form_state->set('add_new_index_field', $add_index_fields);
45+
// @TODO not being used so maybe removable or update the comment.
3546
$form_state->set('index_added', FALSE);
47+
// @TODO not being used so maybe removable or update the comment.
3648
$form_state->set('adding_new_index_fields', TRUE);
3749
}
3850

51+
// If saving new index field.
3952
if ($op === 'add_index_field') {
53+
// @TODO not being used so maybe removable or update the comment.
4054
$form_state->set('add_new_index_field', '');
55+
// Get and save the entered values.
4156
$form_state->set('new_index_fields', $form_state->getUserInput());
57+
// @TODO not being used so maybe removable or update the comment.
4258
$form_state->set('add', TRUE);
59+
// Set the display to show the entered values.
4360
$form_state->set('cancel_index_field', FALSE);
61+
// @TODO not being used so maybe removable or update the comment.
4462
$form_state->set('adding_new_index_fields', FALSE);
4563
}
4664

65+
// Let's retain the fields that are already stored on the form,
66+
// but aren't currently being modified.
4767
$form_state->set('current_dictionary_fields', $current_dictionary_fields);
4868
$form_state->set('current_index', $current_index);
4969
$form_state->set('current_index_fields', $current_index_fields);
70+
// Let's rebuild the form.
5071
$form_state->setRebuild();
5172
}
5273

5374
/**
5475
* Submit callback for the Index Add button.
5576
*/
5677
public static function indexAddCallback(array &$form, FormStateInterface $form_state) {
78+
// Get the button's trigger value.
5779
$trigger = $form_state->getTriggeringElement();
5880
$op = $trigger['#op'];
81+
// Get the current fields data.
5982
$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.
6086
$form_state->set('add_new_index_field', '');
6187
$form_state->set('new_index_fields', '');
6288
$form_state->set('add_new_index', '');
89+
// @TODO not being used so maybe removable or update the comment.
6390
$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;
6691

6792
if ($current_index) {
6893
$form_state->set('current_index', $current_index);
6994
}
7095

96+
// If cancelling index.
7197
if ($op === 'cancel_index') {
98+
// Set the display to show the current index values.
7299
$form_state->set('cancel_index', TRUE);
73100
}
74101

102+
// If adding new index, this is triggered when you click the button to
103+
// 'Add index'
75104
if ($op === 'add_new_index') {
105+
// Get the form fields for adding new index.
76106
$add_new_index = IndexFieldAddCreation::addIndex();
107+
// Set the new_index values to empty.
77108
$form_state->set('new_index', '');
109+
// Set the fields in the field collection.
78110
$form_state->set('add_new_index', $add_new_index);
79111
}
80112

113+
// If saving new index.
81114
if ($op === 'add_index') {
115+
// Empty the fields in the field collection.
82116
$form_state->set('add_new_index', '');
117+
// Get and save the entered values.
83118
$form_state->set('new_index', $form_state->getUserInput());
119+
// @TODO not being used so maybe removable or update the comment.
84120
$form_state->set('add', TRUE);
121+
// @TODO not being used so maybe removable or update the comment.
85122
$form_state->set('index_added', TRUE);
123+
// Set the display to show the entered values.
86124
$form_state->set('cancel_index', FALSE);
87125
}
88126

127+
// Let's retain the fields that are already stored on the form,
128+
// but aren't currently being modified.
89129
$form_state->set('current_dictionary_fields', $current_dictionary_fields);
90130
$form_state->set('current_index', $current_index);
91131
$form_state->set('current_index_fields', $current_index_fields);
132+
// Let's rebuild the form.
92133
$form_state->setRebuild();
93134
}
94135

95136
/**
96137
* Submit callback for the Index Field Edit button.
97138
*/
98139
public static function indexEditSubformCallback(array &$form, FormStateInterface $form_state) {
140+
// Get the button's trigger value.
99141
$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.
100147
$current_index_fields = $form["field_json_metadata"]["widget"][0]["indexes"]["fields"]["data"]["#rows"];
101148
$current_dictionary_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"];
102-
$op = $trigger['#op'];
103-
$op_index = explode("_", $trigger['#op']);
104149
$currently_modifying_index_fields = $form_state->get('index_fields_being_modified') != NULL ? $form_state->get('index_fields_being_modified') : [];
105150
$currently_modifying = $form_state->get('dictionary_fields_being_modified') != NULL ? $form_state->get('dictionary_fields_being_modified') : [];
106151

152+
// If the op (trigger) contains abort,
153+
// We're canceling the field we're currently modifying so unset it.
107154
if (str_contains($op, 'abort')) {
108155
unset($currently_modifying_index_fields[$op_index[4]]);
109156
}
110157

158+
// If the op (trigger) contains delete,
159+
// We're deleting the field we're editing so...
111160
if (str_contains($op, 'delete')) {
161+
// Unset it from being currently modified.
112162
unset($currently_modifying_index_fields[$op_index[4]]);
163+
// Remove the respective field/data from the form.
113164
unset($current_index_fields[$op_index[4]]);
114165
}
115166

167+
// If the op (trigger) contains update,
168+
// We're saving the field we're editing so...
116169
if (str_contains($op, 'update')) {
170+
// Get the entered data.
117171
$update_values = $form_state->getUserInput();
172+
// Unset the respective currently modifying field.
118173
unset($currently_modifying_index_fields[$op_index[4]]);
174+
// Unset the respective field/data from the form.
119175
unset($current_index_fields[$op_index[4]]);
176+
// Update the respective current field data with our new input data.
120177
$current_index_fields[$op_index[4]] = IndexFieldValues::updateIndexFieldValues($op_index[4], $update_values, $current_index_fields);
178+
// Sort the current index fields data.
121179
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]];
127180
}
128181

182+
// If the op (trigger) contains edit
183+
// We're editing a specific field so...
129184
if (str_contains($op, 'edit_index_field')) {
185+
// Set the field we're modifying to that field.
130186
$currently_modifying_index_fields[$op_index[4]] = $current_index_fields[$op_index[4]];
131187
}
132188

189+
// Let's retain the fields that are being modified.
133190
$form_state->set('dictionary_fields_being_modified', $currently_modifying);
134191
$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.
135194
$form_state->set('current_index_fields', $current_index_fields);
136195
$form_state->set('current_dictionary_fields', $current_dictionary_fields);
196+
// Let's rebuild the form.
137197
$form_state->setRebuild();
138198
}
139199

140200
/**
141201
* Submit callback for the Index Edit button.
142202
*/
143203
public static function indexEditCallback(array &$form, FormStateInterface $form_state) {
204+
// Get the button's trigger value.
144205
$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.
145211
$current_index_fields = $form['field_json_metadata']['widget'][0]['indexes']['fields']['data']['#rows'] ?? [];
146212
$current_index = $form['field_json_metadata']['widget'][0]['indexes']['data']['#rows'] ?? [];
147213
$current_dictionary_fields = $form['field_json_metadata']['widget'][0]['dictionary_fields']['data']['#rows'] ?? [];
148-
$op = $trigger['#op'];
149-
$op_index = explode('_', $trigger['#op']);
150214
$currently_modifying_index_fields = $form_state->get('index_fields_being_modified') != NULL ? $form_state->get('index_fields_being_modified') : [];
151215
$currently_modifying_index = $form_state->get('index_being_modified') != NULL ? $form_state->get('index_being_modified') : [];
152216
$currently_modifying_dictionary_fields = $form_state->get('dictionary_fields_being_modified') != NULL ? $form_state->get('dictionary_fields_being_modified') : [];
153217

218+
// If the op (trigger) contains abort,
219+
// We're canceling the index we're currently modifying so unset it.
154220
if (str_contains($op, 'abort_index_key')) {
155221
unset($currently_modifying_index[$op_index[3]]);
156222
}
157-
223+
// We're canceling the index field we're currently modifying so unset it.
158224
if (str_contains($op, 'abort_index_field_key')) {
159225
unset($currently_modifying_index_fields[$op_index[4]]);
160226
}
161227

228+
// If the op (trigger) contains delete,
229+
// We're deleting the index we're editing so...
162230
if (str_contains($op, 'delete_index_key')) {
231+
// Unset it from being currently modified.
163232
unset($currently_modifying_index[$op_index[3]]);
233+
// Remove the respective field/data from the form.
164234
unset($current_index[$op_index[3]]);
165235
}
166-
236+
// We're deleting the index field we're editing so...
167237
if (str_contains($op, 'delete_index_field_key')) {
238+
// Unset it from being currently modified.
168239
unset($currently_modifying_index_fields[$op_index[4]]);
240+
// Remove the respective field/data from the form.
169241
unset($current_index_fields[$op_index[4]]);
170242
}
171243

244+
// If the op (trigger) contains update,
245+
// We're saving the field we're editing so...
172246
if (str_contains($op, 'update')) {
247+
// Get the entered data.
173248
$update_values = $form_state->getUserInput();
249+
// Update the respective current field data with our new input data.
174250
$current_index[$op_index[3]] = IndexFieldValues::updateIndexValues($op_index[3], $update_values, $current_index);
251+
// Unset the respective field/data from the form.
175252
unset($currently_modifying_index[$op_index[3]]);
253+
// Sort the current index data.
176254
ksort($current_index);
177255
}
178256

257+
// If the op (trigger) contains edit
258+
// We're editing a specific field so...
179259
if (str_contains($op, 'edit')) {
260+
// Set the field we're modifying to that field.
180261
$currently_modifying_index[$op_index[3]] = $current_index[$op_index[3]];
181262
}
182263

264+
// Let's retain the fields that are being modified.
183265
$form_state->set('dictionary_fields_being_modified', $currently_modifying_dictionary_fields);
184266
$form_state->set('index_fields_being_modified', $currently_modifying_index_fields);
185267
$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.
186270
$form_state->set('current_index_fields', $current_index_fields);
187271
$form_state->set('current_index', $current_index);
188272
$form_state->set('current_dictionary_fields', $current_dictionary_fields);
273+
// Let's rebuild the form.
189274
$form_state->setRebuild();
190275
}
191276

@@ -220,15 +305,15 @@ public static function indexFormAjax(array &$form, FormStateInterface $form_stat
220305
}
221306

222307
/**
223-
* Ajax callback to return index fields fieldset with Add Field button.
308+
* Ajax callback to return index fields fieldset with 'Add Field' button.
224309
*/
225310
public static function subIndexFormFieldAjax(array &$form, FormStateInterface $form_state) {
226311
return $form["field_json_metadata"]["widget"][0]["indexes"]["field_collection"]["group"]["index"]["fields"];
227312
}
228313

229314
/**
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.
232317
*/
233318
public static function subIndexFormExistingFieldAjax(array &$form, FormStateInterface $form_state) {
234319
$form["field_json_metadata"]["widget"][0]["indexes"]["field_collection"]["group"]["index"]["fields"]["add_row_button"]['#access'] = TRUE;

modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static function createGeneralIndex($element, $current_indexes) {
4141
}
4242

4343
/**
44-
* Create data index data rows.
44+
* Create data index fields data rows.
4545
*/
4646
public static function createIndexFieldsDataRows($index_field_values, $current_index_fields, $index_fields_data_results, $form_state) {
4747
if ($index_field_values) {

modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ class IndexFieldEditCreation {
1010
/**
1111
* Create edit index fields.
1212
*/
13-
public static function editIndexFields($indexKey, $current_index_fields, $index_fields_being_modified) {
14-
$id = $current_index_fields ? "field-json-metadata-index-fields" : "field-json-metadata-index-fields-new";
13+
public static function editIndexFields($indexKey, $current_index_fields) {
14+
$id = $current_index_fields ? 'field-json-metadata-index-fields' : 'field-json-metadata-index-fields-new';
15+
// We split the key to get the index field location.
1516
$indexKeyExplode = explode("_", $indexKey);
1617
$edit_index_fields['name'] = [
1718
'#name' => 'field_json_metadata[0][indexes][fields][edit_index_fields][' . $indexKeyExplode[3] . '][name]',
@@ -36,7 +37,7 @@ public static function editIndexFields($indexKey, $current_index_fields, $index_
3637
/**
3738
* Create edit index.
3839
*/
39-
public static function editIndex($indexKey, $current_index, $index_being_modified, $form_state) {
40+
public static function editIndex($indexKey, $current_index, $form_state) {
4041
$id = $current_index ? "field-json-metadata-index-new" : "field-json-metadata-index";
4142
$indexKeyExplode = explode("_", $indexKey);
4243

0 commit comments

Comments
 (0)