Skip to content

Commit 8c41778

Browse files
authored
Data dictionary indexes code refactoring (#4280)
1 parent 50d8da9 commit 8c41778

File tree

4 files changed

+48
-41
lines changed

4 files changed

+48
-41
lines changed

modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Various operations for the Index callbacks.
99
*/
1010
class IndexFieldCallbacks {
11+
1112
/**
1213
* Submit callback for the Index Add button.
1314
*/
@@ -141,11 +142,11 @@ public static function indexEditSubformCallback(array &$form, FormStateInterface
141142
*/
142143
public static function indexEditCallback(array &$form, FormStateInterface $form_state) {
143144
$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"] ?? [];
145+
$current_index_fields = $form['field_json_metadata']['widget'][0]['indexes']['fields']['data']['#rows'] ?? [];
146+
$current_index = $form['field_json_metadata']['widget'][0]['indexes']['data']['#rows'] ?? [];
147+
$current_dictionary_fields = $form['field_json_metadata']['widget'][0]['dictionary_fields']['data']['#rows'] ?? [];
147148
$op = $trigger['#op'];
148-
$op_index = explode("_", $trigger['#op']);
149+
$op_index = explode('_', $trigger['#op']);
149150
$currently_modifying_index_fields = $form_state->get('index_fields_being_modified') != NULL ? $form_state->get('index_fields_being_modified') : [];
150151
$currently_modifying_index = $form_state->get('index_being_modified') != NULL ? $form_state->get('index_being_modified') : [];
151152
$currently_modifying_dictionary_fields = $form_state->get('dictionary_fields_being_modified') != NULL ? $form_state->get('dictionary_fields_being_modified') : [];
@@ -226,8 +227,8 @@ public static function subIndexFormFieldAjax(array &$form, FormStateInterface $f
226227
}
227228

228229
/**
229-
* Ajax callback to return index fields fieldset with existing fields and Add
230-
* Field button.
230+
* Ajax callback to return index fields fieldset with existing fields and
231+
* Add Field button.
231232
*/
232233
public static function subIndexFormExistingFieldAjax(array &$form, FormStateInterface $form_state) {
233234
$form["field_json_metadata"]["widget"][0]["indexes"]["field_collection"]["group"]["index"]["fields"]["add_row_button"]['#access'] = TRUE;

modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static function editIndex($indexKey, $current_index, $index_being_modifie
7474

7575
$edit_index['group']['fields']['data'] = IndexFieldCreation::createIndexFieldsDataRows($current_index[$indexKeyExplode[2]]['fields'], $current_index[$indexKeyExplode[2]]['fields'], $current_index[$indexKeyExplode[2]]['fields'], $form_state);
7676

77-
$edit_index['update_index']['actions'] = self::createIndexActionFields($indexKey, $id );
77+
$edit_index['update_index']['actions'] = self::createIndexActionFields($indexKey, $id);
7878

7979
return $edit_index;
8080
}

modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php

+23-19
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class IndexFieldOperations {
1111
* Setting ajax elements when editing newly added index fields.
1212
*/
1313
public static function setIndexFieldsAjaxElementsOnAdd(array $indexFields) {
14-
if ($indexFields["data"]) {
14+
if ($indexFields['data']) {
1515
foreach ($indexFields['data']['#rows'] as $row => $data) {
1616
$edit_index_button = $indexFields['edit_index_buttons']['index_field_key_' . $row] ?? NULL;
1717
$edit_index_fields = $indexFields['edit_index_fields']['index_field_key_' . $row] ?? NULL;
@@ -35,16 +35,18 @@ public static function setIndexFieldsAjaxElementsOnAdd(array $indexFields) {
3535

3636
/**
3737
* Setting ajax elements when editing existing index fields.
38+
*
39+
* Currently, not being used as index fields cannot be edited once added.
3840
*/
3941
public static function setIndexFieldsAjaxElements(array $indexFields) {
40-
if ($indexFields["data"]) {
42+
if ($indexFields['data']) {
4143
foreach ($indexFields['data']['#rows'] as $row => $data) {
4244
$edit_index_fields_button = $indexFields['fields']['edit_index_fields_buttons']['index_field_key_' . $row] ?? NULL;
4345
$edit_index_fields = $indexFields['fields']['edit_index_fields']['index_field_key_' . $row] ?? NULL;
4446
// Setting the ajax fields if they exist.
4547
if ($edit_index_fields_button) {
46-
$indexFields["data"]["#rows"][$row] = array_merge($data, $edit_index_fields_button);
47-
unset($indexFields["fields"]["edit_index_fields_buttons"]['index_field_key_' . $row]);
48+
$indexFields['data']['#rows'][$row] = array_merge($data, $edit_index_fields_button);
49+
unset($indexFields['fields']['edit_index_fields_buttons']['index_field_key_' . $row]);
4850
}
4951
elseif ($edit_index_fields) {
5052
unset($indexFields['data']['#rows']['index_field_key_' . $row]);
@@ -118,14 +120,14 @@ public static function processIndexDataResults($index_results, $current_indexes,
118120
$index_results = $current_indexes;
119121
}
120122

121-
if (isset($index_values["field_json_metadata"][0]["indexes"]["field_collection"])) {
122-
$index_group = $index_values["field_json_metadata"][0]["indexes"]["field_collection"]["group"];
123+
if (isset($index_values['field_json_metadata'][0]['indexes']['field_collection'])) {
124+
$index_group = $index_values['field_json_metadata'][0]['indexes']['field_collection']['group'];
123125

124126
$data_index_pre = [
125127
[
126-
"description" => $index_group['index']["description"],
127-
"type" => $index_group['index']["type"],
128-
"fields" => $index_fields_data_results,
128+
'description' => $index_group['index']['description'],
129+
'type' => $index_group['index']['type'],
130+
'fields' => $index_fields_data_results,
129131
],
130132
];
131133
}
@@ -215,7 +217,7 @@ public static function createIndexFieldOptions($op_index, $index_data_results, $
215217
/**
216218
* Create edit and update fields for indexes.
217219
*/
218-
public static function createIndexOptions($op_index, $index_data_results, $index_being_modified, $index_field_being_modified, $element, $form_state) {
220+
public static function createIndexOptions($op_index, $index_data_results, $index_being_modified, $element, $form_state) {
219221
$current_indexes = $element['current_index'];
220222

221223
// Creating ajax buttons/fields to be placed in correct location later.
@@ -237,13 +239,13 @@ public static function createIndexOptions($op_index, $index_data_results, $index
237239
*/
238240
public static function checkIndexEditingField($indexKey, $op_index, $index_fields_being_modified) {
239241
$action_list = IndexFieldOperations::editIndexActions();
240-
$indexKeyExplode = explode("_", $indexKey);
241-
if (isset($op_index[0]) && in_array($op_index[0], $action_list) && array_key_exists($indexKeyExplode[3], $index_fields_being_modified)) {
242-
return TRUE;
243-
}
244-
else {
245-
return FALSE;
242+
if (isset($op_index[0]) && in_array($op_index[0], $action_list)) {
243+
$indexKeyExplode = explode('_', $indexKey);
244+
if (array_key_exists($indexKeyExplode[3], $index_fields_being_modified)) {
245+
return TRUE;
246+
}
246247
}
248+
return FALSE;
247249
}
248250

249251
/**
@@ -254,9 +256,11 @@ public static function checkIndexEditing($indexKey, $op_index, $index_being_modi
254256
$op_index_string = implode('_', $op_index);
255257
if (str_contains($op_index_string, 'edit_index_key')) {
256258
$action_list = IndexFieldOperations::editIndexActions();
257-
$indexKeyExplode = explode("_", $indexKey);
258-
if (isset($op_index[0]) && in_array($op_index[0], $action_list) && array_key_exists($indexKeyExplode[2], $index_being_modified)) {
259-
return TRUE;
259+
if (isset($op_index[0]) && in_array($op_index[0], $action_list)) {
260+
$indexKeyExplode = explode('_', $indexKey);
261+
if (array_key_exists($indexKeyExplode[2], $index_being_modified)) {
262+
return TRUE;
263+
}
260264
}
261265
}
262266
}

modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php

+17-15
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,37 @@ class DataDictionaryWidget extends WidgetBase implements TrustedCallbackInterfac
3030
* {@inheritdoc}
3131
*/
3232
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
33-
// Retrieve data-dictionary form_state values to be used for various operations.
34-
$dictionary_field_values = $form_state->get("new_dictionary_fields");
33+
// Retrieve data-dictionary form_state values to be used for various
34+
// operations.
35+
$dictionary_field_values = $form_state->get('new_dictionary_fields');
3536
$add_new_dictionary_field = $form_state->get('add_new_field');
3637
$current_dictionary_fields = $form_state->get('current_dictionary_fields');
37-
$dictionary_fields_being_modified = $form_state->get("dictionary_fields_being_modified") ?? NULL;
38+
$dictionary_fields_being_modified = $form_state->get('dictionary_fields_being_modified') ?? NULL;
3839

3940
// Retrieve indexes form_state values to be used for various operations.
4041
$current_indexes = $form_state->get('current_index');
4142
$index_values = $form_state->get('new_index');
4243
$add_new_index = $form_state->get('add_new_index');
43-
$index_being_modified = $form_state->get("index_being_modified") ?? NULL;
44+
$index_being_modified = $form_state->get('index_being_modified') ?? NULL;
4445

45-
// Retrieve index fields form_state values to be used for various operations.
46+
// Retrieve index fields form_state values to be used for various
47+
// operations.
4648
$current_index_fields = $form_state->get('current_index_fields');
47-
$index_field_values = $form_state->get("new_index_fields");
49+
$index_field_values = $form_state->get('new_index_fields');
4850
$add_index_field = $form_state->get('add_new_index_field');
49-
$index_fields_being_modified = $form_state->get("index_fields_being_modified") ?? NULL;
51+
$index_fields_being_modified = $form_state->get('index_fields_being_modified') ?? NULL;
5052

5153
// Retrieve triggered element to be used for various operations.
5254
$op = $form_state->getTriggeringElement()['#op'] ?? NULL;
53-
$op_index = isset($op) ? explode("_", $op) : NULL;
55+
$op_index = isset($op) ? explode('_', $op) : NULL;
5456

5557
// Retrieve form element item values.
5658
$field_json_metadata = !empty($items[0]->value) ? json_decode($items[0]->value, TRUE) : [];
5759

5860
// Retrieve initial data results from field JSON metadata.
59-
$data_results = $field_json_metadata["data"]["fields"] ?? [];
60-
$index_fields_results = $field_json_metadata["data"]["indexes"][0]["fields"] ?? [];
61-
$index_results = $field_json_metadata["data"]["indexes"] ?? [];
61+
$data_results = $field_json_metadata['data']['fields'] ?? [];
62+
$index_fields_results = $field_json_metadata['data']['indexes'][0]['fields'] ?? [];
63+
$index_results = $field_json_metadata['data']['indexes'] ?? [];
6264

6365
// Process data results.
6466
$data_results = FieldOperations::processDataResults($data_results, $current_dictionary_fields, $dictionary_field_values, $op);
@@ -89,18 +91,18 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
8991

9092
// Create dictionary fields/buttons for editing.
9193
$element['dictionary_fields'] = FieldOperations::createDictionaryFieldOptions($op_index, $data_results, $dictionary_fields_being_modified, $element['dictionary_fields']);
92-
$element['dictionary_fields']['add_row_button']['#access'] = $dictionary_fields_being_modified == NULL ? TRUE : FALSE;
94+
$element['dictionary_fields']['add_row_button']['#access'] = $dictionary_fields_being_modified == NULL;
9395

9496
// Create index fields/buttons for editing.
95-
$element['indexes'] = IndexFieldOperations::createIndexOptions($op_index, $index_data_results, $index_being_modified, $index_fields_being_modified, $element['indexes'], $form_state);
97+
$element['indexes'] = IndexFieldOperations::createIndexOptions($op_index, $index_data_results, $index_being_modified, $element['indexes'], $form_state);
9698

9799
// Create edit buttons and fields for index fields.
98100
if ($index_field_values || $current_index_fields) {
99-
$element["indexes"]["fields"] = IndexFieldOperations::createIndexFieldOptions($op_index, $index_fields_data_results, $index_fields_being_modified, $element['indexes']['fields']);
101+
$element['indexes']['fields'] = IndexFieldOperations::createIndexFieldOptions($op_index, $index_fields_data_results, $index_fields_being_modified, $element['indexes']['fields']);
100102
}
101103

102104
// Set access to index fields Add button when index fields being modified.
103-
$element['indexes']['add_row_button']['#access'] = $index_being_modified == NULL ? TRUE : FALSE;
105+
$element['indexes']['add_row_button']['#access'] = $index_being_modified == NULL;
104106

105107
// Get form entity.
106108
$form_object = $form_state->getFormObject();

0 commit comments

Comments
 (0)