Skip to content

Commit f6f5384

Browse files
authored
Editable Index (#4239)
1 parent 9ec37cd commit f6f5384

13 files changed

+420
-144
lines changed

Diff for: modules/data_dictionary_widget/src/Fields/FieldCallbacks.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public static function updateFormatOptions(array &$form, FormStateInterface $for
4040
*/
4141
public static function editSubformCallback(array &$form, FormStateInterface $form_state) {
4242
// Get the current fields data
43-
$current_dictionary_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"];
44-
$current_index_fields = $form["field_json_metadata"]["widget"][0]["indexes"]["fields"]["data"]["#rows"];
43+
$current_dictionary_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"] ?? [];
44+
$current_index_fields = $form["field_json_metadata"]["widget"][0]["indexes"]["fields"]["data"]["#rows"] ?? [];
4545
// Get the field index from the triggering op attribute
4646
// so we can use it to store the respective field later
4747
$op_index = explode("_", $form_state->getTriggeringElement()['#op']);
@@ -103,7 +103,7 @@ public static function addSubformCallback(array &$form, FormStateInterface $form
103103
$form_state->set('add_new_field', '');
104104
$current_dictionary_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"];
105105
$current_index = $form["field_json_metadata"]["widget"][0]['indexes']["data"]["#rows"];
106-
$current_index_fields = $form["field_json_metadata"]["widget"][0]['indexes']["fields"]["data"]["#rows"];
106+
$current_index_fields = $form["field_json_metadata"]["widget"][0]['indexes']["fields"]["data"]["#rows"] ?? [];
107107

108108
if ($current_dictionary_fields) {
109109
$form_state->set('current_dictionary_fields', $current_dictionary_fields);

Diff for: modules/data_dictionary_widget/src/Fields/FieldOperations.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,9 @@ public static function checkFieldCollection($data_pre, $op) {
196196
/**
197197
* Set the elements associated with adding a new field.
198198
*/
199-
public static function setAddFormState($add_new_field, $element) {
199+
public static function setAddDictionaryFieldFormState($add_new_field, $element) {
200200
if ($add_new_field) {
201+
unset($element['dictionary_fields']["edit_buttons"]);
201202
$element['dictionary_fields']['field_collection'] = $add_new_field;
202203
$element['dictionary_fields']['field_collection']['#access'] = TRUE;
203204
$element['dictionary_fields']['add_row_button']['#access'] = FALSE;
@@ -207,6 +208,17 @@ public static function setAddFormState($add_new_field, $element) {
207208
return $element;
208209
}
209210

211+
/**
212+
* Set the elements associated with editing a dictionary field.
213+
*/
214+
public static function editDictionaryFieldFormState($edit_dictionary_field, $element) {
215+
if ($edit_dictionary_field) {
216+
unset($element['dictionary_fields']["edit_buttons"]);
217+
}
218+
219+
return $element;
220+
}
221+
210222
/**
211223
* Create edit and update fields where needed.
212224
*/

Diff for: modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
namespace Drupal\data_dictionary_widget\Indexes;
44

55
/**
6-
* Various operations for creating Data Dictionary Widget add fields.
6+
* Various operations for creating Index fields.
77
*/
88
class IndexFieldAddCreation {
99

1010
/**
11-
* Create add fields for Data Dictionary Widget.
11+
* Create fields for adding an index.
1212
*/
1313
public static function addIndex() {
1414
$add_index['#access'] = FALSE;
1515
$add_index['group'] = [
1616
'#type' => 'fieldset',
1717
'#title' => t('Index'),
1818
'#open' => TRUE,
19-
'#prefix' => '<div id = field-json-metadata-dictionary-index>',
19+
'#prefix' => '<div id = field-json-metadata-index>',
2020
'#suffix' => '</div>',
2121
'#element_validate' => [
2222
['\Drupal\data_dictionary_widget\Indexes\IndexValidation', 'indexFieldsValidation']
@@ -48,7 +48,7 @@ public static function addIndex() {
4848
$add_index['group']['index']['fields'] = [
4949
'#type' => 'fieldset',
5050
'#title' => t('Fields'),
51-
'#prefix' => '<div id = field-json-metadata-dictionary-index-fields>',
51+
'#prefix' => '<div id = field-json-metadata-index-fields>',
5252
'#suffix' => '</div>',
5353
'#markup' => t('<div class="claro-details__description">One or more fields included in index. Must be keys from the fields object.</div>'),
5454
'#attributes' => [
@@ -64,10 +64,10 @@ public static function addIndex() {
6464
}
6565

6666
/**
67-
* Create add fields for Data Dictionary Widget.
67+
* Create fields for adding an index field.
6868
*/
6969
public static function addIndexFields($current_index_fields) {
70-
$id = "field-json-metadata-dictionary-index-fields-new";
70+
$id = "field-json-metadata-index-fields-new";
7171
$add_index_fields['#access'] = FALSE;
7272
$add_index_fields['group'] = [
7373
'#type' => 'fieldset',

Diff for: modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php

+49-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Drupal\data_dictionary_widget\Indexes;
44

55
/**
6-
* Various operations for creating Data Dictionary Widget fields.
6+
* Various operations for index widget buttons.
77
*/
88
class IndexFieldButtons {
99

@@ -25,7 +25,7 @@ public static function addIndexFieldButton() {
2525
],
2626
'#ajax' => [
2727
'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexFormAjax',
28-
'wrapper' => 'field-json-metadata-dictionary-index-fields',
28+
'wrapper' => 'field-json-metadata-index-fields',
2929
'effect' => 'fade',
3030
],
3131
'#limit_validation_errors' => [],
@@ -50,7 +50,7 @@ public static function addIndexButton() {
5050
],
5151
'#ajax' => [
5252
'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexFormAjax',
53-
'wrapper' => 'field-json-metadata-dictionary-index',
53+
'wrapper' => 'field-json-metadata-index',
5454
'effect' => 'fade',
5555
],
5656
'#limit_validation_errors' => [],
@@ -61,10 +61,19 @@ public static function addIndexButton() {
6161
* Returns the edit buttons.
6262
*/
6363
public static function editIndexButtons($indexKey) {
64+
if (str_contains($indexKey, 'field')) {
65+
$callback = 'indexEditSubformCallback';
66+
$id = 'field-json-metadata-index-fields';
67+
$function = 'subIndexFormAjax';
68+
} else {
69+
$callback = 'indexEditCallback';
70+
$id = 'field-json-metadata-index';
71+
$function = 'indexFormAjax';
72+
}
6473
return [
6574
'#type' => 'image_button',
66-
'#name' => 'edit_index_' . $indexKey,
67-
'#id' => 'edit_index_' . $indexKey,
75+
'#name' => 'edit_' . $indexKey,
76+
'#id' => 'edit_' . $indexKey,
6877
'#access' => TRUE,
6978
'#op' => 'edit_' . $indexKey,
7079
'#src' => 'core/misc/icons/787878/cog.svg',
@@ -75,12 +84,12 @@ public static function editIndexButtons($indexKey) {
7584
'#submit' => [
7685
[
7786
'\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks',
78-
'indexEditSubformCallback',
87+
$callback,
7988
],
8089
],
8190
'#ajax' => [
82-
'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexFormAjax',
83-
'wrapper' => 'field-json-metadata-dictionary-index-fields',
91+
'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::' . $function,
92+
'wrapper' => $id,
8493
'effect' => 'fade',
8594
],
8695
'#limit_validation_errors' => [
@@ -96,6 +105,7 @@ public static function submitIndexFieldButton($location, $indexKey) {
96105
$callbackClass = $location == 'edit' ? 'indexEditSubformCallback' : 'indexAddSubformCallback';
97106
$op = !empty($indexKey) ? 'update_' . $indexKey : 'add_index_field';
98107
$value = $location == 'edit' ? 'Save' : 'Add ';
108+
$function = $location == 'edit' ? 'subIndexFormAjax' : 'subIndexFormAjax';
99109
$edit_index_button = [
100110
'#type' => 'submit',
101111
'#value' => $value,
@@ -107,8 +117,8 @@ public static function submitIndexFieldButton($location, $indexKey) {
107117
],
108118
],
109119
'#ajax' => [
110-
'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexFormAjax',
111-
'wrapper' => 'field-json-metadata-dictionary-index-fields',
120+
'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::' . $function,
121+
'wrapper' => 'field-json-metadata-index-fields',
112122
'effect' => 'fade',
113123
],
114124
'#limit_validation_errors' => [
@@ -151,7 +161,7 @@ public static function submitIndexButton($location, $indexKey) {
151161
],
152162
'#ajax' => [
153163
'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexFormAjax',
154-
'wrapper' => 'field-json-metadata-dictionary-index',
164+
'wrapper' => 'field-json-metadata-index',
155165
'effect' => 'fade',
156166
],
157167
'#limit_validation_errors' => [
@@ -170,7 +180,7 @@ public static function submitIndexButton($location, $indexKey) {
170180
* Create Cancel button.
171181
*/
172182
public static function cancelIndexFieldButton($location, $indexKey, $id) {
173-
$callbackId = ($id === 'field-json-metadata-dictionary-index-fields-new') ? 'subIndexFormExistingFieldAjax' : 'subIndexFormAjax';
183+
$callbackId = ($id === 'field-json-metadata-index-fields-new') ? 'subIndexFormExistingFieldAjax' : 'subIndexFormAjax';
174184
$callbackClass = $location == 'edit' ? 'indexEditSubformCallback' : 'indexAddSubformCallback';
175185
$op = $location == 'edit' && $indexKey ? 'abort_' . $indexKey : 'cancel_index_field';
176186
$cancel_index_button = [
@@ -217,7 +227,7 @@ public static function cancelIndexButton($location, $indexKey) {
217227
],
218228
'#ajax' => [
219229
'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexFormAjax',
220-
'wrapper' => 'field-json-metadata-dictionary-index',
230+
'wrapper' => 'field-json-metadata-index',
221231
'effect' => 'fade',
222232
],
223233
'#limit_validation_errors' => [],
@@ -232,7 +242,7 @@ public static function cancelIndexButton($location, $indexKey) {
232242
/**
233243
* Create Delete button.
234244
*/
235-
public static function deleteIndexButton($indexKey) {
245+
public static function deleteIndexFieldButton($indexKey) {
236246
return [
237247
'#type' => 'submit',
238248
'#name' => 'index_delete_' . $indexKey,
@@ -246,7 +256,31 @@ public static function deleteIndexButton($indexKey) {
246256
],
247257
'#ajax' => [
248258
'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexFormAjax',
249-
'wrapper' => 'field-json-metadata-dictionary-index-fields',
259+
'wrapper' => 'field-json-metadata-index-fields',
260+
'effect' => 'fade',
261+
],
262+
'#limit_validation_errors' => [],
263+
];
264+
}
265+
266+
/**
267+
* Create Delete button.
268+
*/
269+
public static function deleteIndexButton($indexKey) {
270+
return [
271+
'#type' => 'submit',
272+
'#name' => 'index_delete_' . $indexKey,
273+
'#value' => t('Delete index'),
274+
'#op' => 'delete_' . $indexKey,
275+
'#submit' => [
276+
[
277+
'\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks',
278+
'indexEditCallback',
279+
],
280+
],
281+
'#ajax' => [
282+
'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexFormAjax',
283+
'wrapper' => 'field-json-metadata-index',
250284
'effect' => 'fade',
251285
],
252286
'#limit_validation_errors' => [],

Diff for: modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php

+71-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Drupal\Core\Form\FormStateInterface;
66

77
/**
8-
* Various operations for the Data Dictionary Widget callbacks.
8+
* Various operations for the Index callbacks.
99
*/
1010
class IndexFieldCallbacks {
1111
/**
@@ -14,9 +14,9 @@ class IndexFieldCallbacks {
1414
public static function indexAddSubformCallback(array &$form, FormStateInterface $form_state) {
1515
$trigger = $form_state->getTriggeringElement();
1616
$op = $trigger['#op'];
17-
$current_dictionary_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"];
18-
$current_index = $form["field_json_metadata"]["widget"][0]['indexes']["data"]["#rows"];
19-
$current_index_fields = $form["field_json_metadata"]["widget"][0]['indexes']["fields"]["data"]["#rows"];
17+
$current_dictionary_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"] ?? [];
18+
$current_index = $form["field_json_metadata"]["widget"][0]['indexes']["data"]["#rows"] ?? [];
19+
$current_index_fields = $form["field_json_metadata"]["widget"][0]['indexes']["fields"]["data"]["#rows"] ?? [];
2020

2121
if ($current_index_fields) {
2222
$form_state->set('current_index_fields', $current_index_fields);
@@ -92,7 +92,7 @@ public static function indexAddCallback(array &$form, FormStateInterface $form_s
9292
}
9393

9494
/**
95-
* Submit callback for the Index Edit button.
95+
* Submit callback for the Index Field Edit button.
9696
*/
9797
public static function indexEditSubformCallback(array &$form, FormStateInterface $form_state) {
9898
$trigger = $form_state->getTriggeringElement();
@@ -121,7 +121,11 @@ public static function indexEditSubformCallback(array &$form, FormStateInterface
121121

122122
}
123123

124-
if (str_contains($op, 'edit')) {
124+
if (str_contains($op, 'edit_index_key')) {
125+
$currently_modifying_index_fields[$op_index[4]] = $current_index_fields[$op_index[4]];
126+
}
127+
128+
if (str_contains($op, 'edit_index_field')) {
125129
$currently_modifying_index_fields[$op_index[4]] = $current_index_fields[$op_index[4]];
126130
}
127131

@@ -132,11 +136,72 @@ public static function indexEditSubformCallback(array &$form, FormStateInterface
132136
$form_state->setRebuild();
133137
}
134138

139+
/**
140+
* Submit callback for the Index Edit button.
141+
*/
142+
public static function indexEditCallback(array &$form, FormStateInterface $form_state) {
143+
$trigger = $form_state->getTriggeringElement();
144+
$current_index_fields = $form["field_json_metadata"]["widget"][0]["indexes"]["fields"]["data"]["#rows"] ?? [];
145+
$current_index = $form["field_json_metadata"]["widget"][0]["indexes"]["data"]["#rows"] ?? [];
146+
$current_dictionary_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"] ?? [];
147+
$op = $trigger['#op'];
148+
$op_index = explode("_", $trigger['#op']);
149+
$currently_modifying_index_fields = $form_state->get('index_fields_being_modified') != NULL ? $form_state->get('index_fields_being_modified') : [];
150+
$currently_modifying_index = $form_state->get('index_being_modified') != NULL ? $form_state->get('index_being_modified') : [];
151+
$currently_modifying_dictionary_fields = $form_state->get('dictionary_fields_being_modified') != NULL ? $form_state->get('dictionary_fields_being_modified') : [];
152+
153+
if (str_contains($op, 'abort_index_key')) {
154+
unset($currently_modifying_index[$op_index[3]]);
155+
}
156+
157+
if (str_contains($op, 'abort_index_field_key')) {
158+
unset($currently_modifying_index_fields[$op_index[4]]);
159+
}
160+
161+
if (str_contains($op, 'delete_index_key')) {
162+
unset($currently_modifying_index[$op_index[3]]);
163+
unset($current_index[$op_index[3]]);
164+
}
165+
166+
if (str_contains($op, 'delete_index_field_key')) {
167+
unset($currently_modifying_index_fields[$op_index[4]]);
168+
unset($current_index_fields[$op_index[4]]);
169+
}
170+
171+
if (str_contains($op, 'update')) {
172+
$update_values = $form_state->getUserInput();
173+
$current_index[$op_index[3]] = IndexFieldValues::updateIndexValues($op_index[3], $update_values, $current_index);
174+
unset($currently_modifying_index[$op_index[3]]);
175+
//unset($current_index[$op_index[3]]);
176+
ksort($current_index);
177+
}
178+
179+
if (str_contains($op, 'edit')) {
180+
$currently_modifying_index[$op_index[3]] = $current_index[$op_index[3]];
181+
}
182+
183+
$form_state->set('dictionary_fields_being_modified', $currently_modifying_dictionary_fields);
184+
$form_state->set('index_fields_being_modified', $currently_modifying_index_fields);
185+
$form_state->set('index_being_modified', $currently_modifying_index);
186+
$form_state->set('current_index_fields', $current_index_fields );
187+
$form_state->set('current_index', $current_index);
188+
$form_state->set('current_dictionary_fields', $current_dictionary_fields);
189+
$form_state->setRebuild();
190+
}
191+
192+
/**
193+
* Ajax callback to return index fields.
194+
*/
195+
public static function subIndexEditFormAjax(array &$form, FormStateInterface $form_state) {
196+
return $form["field_json_metadata"]["widget"][0]["indexes"]["edit_index"]["index_key_0"]["group"]["fields"]["fields"];
197+
}
198+
135199
/**
136200
* Ajax callback to return index fields.
137201
*/
138202
public static function subIndexFormAjax(array &$form, FormStateInterface $form_state) {
139203
return $form["field_json_metadata"]["widget"][0]["indexes"]["fields"];
204+
//return $form["field_json_metadata"]["widget"][0]["indexes"]["edit_index"]["index_key_0"]["group"]["fields"]["fields"];
140205
}
141206

142207
/**

0 commit comments

Comments
 (0)