From 5ed85a80c99bc379b8f8f0c71222dc539efc5871 Mon Sep 17 00:00:00 2001 From: Daniel Mundra Date: Mon, 16 Sep 2024 16:07:17 -0700 Subject: [PATCH 1/2] Adding code comments. Removed redundant code. Added TODOs. Moved code to match order in other functions. --- .../data_dictionary_widget.module | 16 ++- .../src/Fields/FieldCallbacks.php | 1 + .../src/Fields/FieldCreation.php | 1 - .../src/Indexes/IndexFieldButtons.php | 6 +- .../src/Indexes/IndexFieldCallbacks.php | 111 ++++++++++++++++-- 5 files changed, 115 insertions(+), 20 deletions(-) diff --git a/modules/data_dictionary_widget/data_dictionary_widget.module b/modules/data_dictionary_widget/data_dictionary_widget.module index 717ddb66dd..e4f8191300 100644 --- a/modules/data_dictionary_widget/data_dictionary_widget.module +++ b/modules/data_dictionary_widget/data_dictionary_widget.module @@ -102,7 +102,7 @@ function data_dictionary_widget_form_alter(&$form, &$form_state, $form_id) { } } - // If we are saving a data dictionary alter the submit. + // If we are saving a data dictionary alter the submit action. foreach (array_keys($form['actions']) as $action) { if ( isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit' @@ -112,11 +112,15 @@ function data_dictionary_widget_form_alter(&$form, &$form_state, $form_id) { } $form['#validate'][] = 'data_dictionary_widget_validate_unique_identifier'; + // Check for existing dictionary and index fields. $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; $current_index_fields = !empty($form["field_json_metadata"]["widget"][0]["indexes"]["current_index"]) ? $form["field_json_metadata"]["widget"][0]["indexes"]["current_index"] : NULL; // The form element render array prefers child keys to be stored as arrays with a #value property. if ($current_dictionary_fields) { + // Create an array to copy the formatted array structure. + $formatted_current_fields = []; + foreach ($current_dictionary_fields as $key => $value) { $keys = array_keys($value); $formatted_current_fields[$key] = []; @@ -142,14 +146,16 @@ function data_dictionary_widget_form_alter(&$form, &$form_state, $form_id) { $fieldValue = ['#value' => $fieldValue]; } } - } else { - // For non-'fields' keys, add '#value' key to the value - $value = ['#value' => $value]; + } + else { + // For non-'fields' keys, add '#value' key to the value + $value = ['#value' => $value]; } } } - $form["field_json_metadata"]["widget"][0]["indexes"]["current_index"] = $current_index_fields; + // Note the current_index_fields was modified in memory instead of copying the array. + $form["field_json_metadata"]["widget"][0]["indexes"]["current_index"] = $current_index_fields; } // Set the default value of the identifier field to a randomly generated uuid. diff --git a/modules/data_dictionary_widget/src/Fields/FieldCallbacks.php b/modules/data_dictionary_widget/src/Fields/FieldCallbacks.php index ce3ad613fc..e7c5198072 100644 --- a/modules/data_dictionary_widget/src/Fields/FieldCallbacks.php +++ b/modules/data_dictionary_widget/src/Fields/FieldCallbacks.php @@ -101,6 +101,7 @@ public static function addSubformCallback(array &$form, FormStateInterface $form $trigger = $form_state->getTriggeringElement(); $op = $trigger['#op']; $form_state->set('add_new_field', ''); + // Get the current fields data. $current_dictionary_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"]; $current_index = $form["field_json_metadata"]["widget"][0]['indexes']["data"]["#rows"]; $current_index_fields = $form["field_json_metadata"]["widget"][0]['indexes']["fields"]["data"]["#rows"] ?? []; diff --git a/modules/data_dictionary_widget/src/Fields/FieldCreation.php b/modules/data_dictionary_widget/src/Fields/FieldCreation.php index 2805d44f11..deb2f132d0 100644 --- a/modules/data_dictionary_widget/src/Fields/FieldCreation.php +++ b/modules/data_dictionary_widget/src/Fields/FieldCreation.php @@ -61,7 +61,6 @@ protected static function createField(string $field, array $field_json_metadata, '#type' => 'textfield', '#required' => TRUE, '#title' => t('Identifier'), - '#default_value' => $field_json_metadata['identifier'] ?? '', '#attributes' => ['readonly' => 'readonly'], '#default_value' => $identifier_uuid ?? '', '#description' => t('
This is the UUID of this Data Dictionary. To assign this data dictionary to a specific distribution use this URL.
', ['@url' => '/api/1/metastore/schemas/data-dictionary/items/' . $identifier_uuid]), diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php index 18afd6ec3d..3b6a2d181e 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php @@ -113,8 +113,12 @@ public static function editIndexButtons($indexKey) { public static function submitIndexFieldButton($location, $indexKey) { $callbackClass = $location == 'edit' ? 'indexEditSubformCallback' : 'indexAddSubformCallback'; $op = !empty($indexKey) ? 'update_' . $indexKey : 'add_index_field'; + // @TODO fix the 'Add ' to drop the space, this will need the test to be + // updated as well. $value = $location == 'edit' ? 'Save' : 'Add '; - $function = $location == 'edit' ? 'subIndexFormAjax' : 'subIndexFormAjax'; + // Index fields cannot be edited once submitted so we use the same function + // for both add and edit. + $function = 'subIndexFormAjax'; $edit_index_button = [ '#type' => 'submit', '#value' => $value, diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php index 5e3cea9a85..664a83ca46 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php @@ -13,8 +13,10 @@ class IndexFieldCallbacks { * Submit callback for the Index Add button. */ public static function indexAddSubformCallback(array &$form, FormStateInterface $form_state) { + // Get the button's trigger value. $trigger = $form_state->getTriggeringElement(); $op = $trigger['#op']; + // Get the current fields data. $current_dictionary_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"] ?? []; $current_index = $form["field_json_metadata"]["widget"][0]['indexes']["data"]["#rows"] ?? []; $current_index_fields = $form["field_json_metadata"]["widget"][0]['indexes']["fields"]["data"]["#rows"] ?? []; @@ -23,30 +25,49 @@ public static function indexAddSubformCallback(array &$form, FormStateInterface $form_state->set('current_index_fields', $current_index_fields); } + // If cancelling index field. if ($op === 'cancel_index_field') { + // Set the display to show the current index fields values. $form_state->set('cancel_index_field', TRUE); + // Hide the field collection. $form_state->set('add_new_index_field', ''); } + // If adding new index field, this is triggered when you click the button to + // 'Add' if ($op === 'add_new_index_field') { + // @TODO not being used so maybe removable or update the comment. $form_state->set('add_index_field', ''); + // Get the form fields for adding new index fields. $add_index_fields = IndexFieldAddCreation::addIndexFields($current_index_fields); + // Set the fields in the field collection. $form_state->set('add_new_index_field', $add_index_fields); + // @TODO not being used so maybe removable or update the comment. $form_state->set('index_added', FALSE); + // @TODO not being used so maybe removable or update the comment. $form_state->set('adding_new_index_fields', TRUE); } + // If saving new index field. if ($op === 'add_index_field') { + // @TODO not being used so maybe removable or update the comment. $form_state->set('add_new_index_field', ''); + // Get and save the entered values. $form_state->set('new_index_fields', $form_state->getUserInput()); + // @TODO not being used so maybe removable or update the comment. $form_state->set('add', TRUE); + // Set the display to show the entered values. $form_state->set('cancel_index_field', FALSE); + // @TODO not being used so maybe removable or update the comment. $form_state->set('adding_new_index_fields', FALSE); } + // Let's retain the fields that are already stored on the form, + // but aren't currently being modified. $form_state->set('current_dictionary_fields', $current_dictionary_fields); $form_state->set('current_index', $current_index); $form_state->set('current_index_fields', $current_index_fields); + // Let's rebuild the form. $form_state->setRebuild(); } @@ -54,41 +75,61 @@ public static function indexAddSubformCallback(array &$form, FormStateInterface * Submit callback for the Index Add button. */ public static function indexAddCallback(array &$form, FormStateInterface $form_state) { + // Get the button's trigger value. $trigger = $form_state->getTriggeringElement(); $op = $trigger['#op']; + // Get the current fields data. $current_dictionary_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"]; + $current_index = $form["field_json_metadata"]["widget"][0]["indexes"]["data"]["#rows"]; + $current_index_fields = $form["field_json_metadata"]["widget"][0]['indexes']["fields"]["data"]["#rows"] ?? NULL; + // Initialize the various field storage values. $form_state->set('add_new_index_field', ''); $form_state->set('new_index_fields', ''); $form_state->set('add_new_index', ''); + // @TODO not being used so maybe removable or update the comment. $form_state->set('adding_new_index_fields', FALSE); - $current_index = $form["field_json_metadata"]["widget"][0]["indexes"]["data"]["#rows"]; - $current_index_fields = $form["field_json_metadata"]["widget"][0]['indexes']["fields"]["data"]["#rows"] ?? NULL; if ($current_index) { $form_state->set('current_index', $current_index); } + // If cancelling index. if ($op === 'cancel_index') { + // Set the display to show the current index values. $form_state->set('cancel_index', TRUE); } + // If adding new index, this is triggered when you click the button to + // 'Add index' if ($op === 'add_new_index') { + // Get the form fields for adding new index. $add_new_index = IndexFieldAddCreation::addIndex(); + // Set the new_index values to empty. $form_state->set('new_index', ''); + // Set the fields in the field collection. $form_state->set('add_new_index', $add_new_index); } + // If saving new index. if ($op === 'add_index') { + // Empty the fields in the field collection. $form_state->set('add_new_index', ''); + // Get and save the entered values. $form_state->set('new_index', $form_state->getUserInput()); + // @TODO not being used so maybe removable or update the comment. $form_state->set('add', TRUE); + // @TODO not being used so maybe removable or update the comment. $form_state->set('index_added', TRUE); + // Set the display to show the entered values. $form_state->set('cancel_index', FALSE); } + // Let's retain the fields that are already stored on the form, + // but aren't currently being modified. $form_state->set('current_dictionary_fields', $current_dictionary_fields); $form_state->set('current_index', $current_index); $form_state->set('current_index_fields', $current_index_fields); + // Let's rebuild the form. $form_state->setRebuild(); } @@ -96,44 +137,63 @@ public static function indexAddCallback(array &$form, FormStateInterface $form_s * Submit callback for the Index Field Edit button. */ public static function indexEditSubformCallback(array &$form, FormStateInterface $form_state) { + // Get the button's trigger value. $trigger = $form_state->getTriggeringElement(); + $op = $trigger['#op']; + // The location of the index field is stored in the operation key. + // We split the key to get the index field location. + $op_index = explode('_', $trigger['#op']); + // Get the current fields data. $current_index_fields = $form["field_json_metadata"]["widget"][0]["indexes"]["fields"]["data"]["#rows"]; $current_dictionary_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"]; - $op = $trigger['#op']; - $op_index = explode("_", $trigger['#op']); $currently_modifying_index_fields = $form_state->get('index_fields_being_modified') != NULL ? $form_state->get('index_fields_being_modified') : []; $currently_modifying = $form_state->get('dictionary_fields_being_modified') != NULL ? $form_state->get('dictionary_fields_being_modified') : []; + // If the op (trigger) contains abort, + // We're canceling the field we're currently modifying so unset it. if (str_contains($op, 'abort')) { unset($currently_modifying_index_fields[$op_index[4]]); } + // If the op (trigger) contains delete, + // We're deleting the field we're editing so... if (str_contains($op, 'delete')) { + // Unset it from being currently modified. unset($currently_modifying_index_fields[$op_index[4]]); + // Remove the respective field/data from the form. unset($current_index_fields[$op_index[4]]); } + // If the op (trigger) contains update, + // We're saving the field we're editing so... if (str_contains($op, 'update')) { + // Get the entered data. $update_values = $form_state->getUserInput(); + // Unset the respective currently modifying field. unset($currently_modifying_index_fields[$op_index[4]]); + // Unset the respective field/data from the form. unset($current_index_fields[$op_index[4]]); + // Update the respective current field data with our new input data. $current_index_fields[$op_index[4]] = IndexFieldValues::updateIndexFieldValues($op_index[4], $update_values, $current_index_fields); + // Sort the current index fields data. ksort($current_index_fields); - - } - - if (str_contains($op, 'edit_index_key')) { - $currently_modifying_index_fields[$op_index[4]] = $current_index_fields[$op_index[4]]; } + // If the op (trigger) contains edit + // We're editing a specific field so... if (str_contains($op, 'edit_index_field')) { + // Set the field we're modifying to that field. $currently_modifying_index_fields[$op_index[4]] = $current_index_fields[$op_index[4]]; } + // Let's retain the fields that are being modified. $form_state->set('dictionary_fields_being_modified', $currently_modifying); $form_state->set('index_fields_being_modified', $currently_modifying_index_fields); + // Let's retain the fields that are already stored on the form, + // but aren't currently being modified. $form_state->set('current_index_fields', $current_index_fields); $form_state->set('current_dictionary_fields', $current_dictionary_fields); + // Let's rebuild the form. $form_state->setRebuild(); } @@ -141,51 +201,76 @@ public static function indexEditSubformCallback(array &$form, FormStateInterface * Submit callback for the Index Edit button. */ public static function indexEditCallback(array &$form, FormStateInterface $form_state) { + // Get the button's trigger value. $trigger = $form_state->getTriggeringElement(); + $op = $trigger['#op']; + // The location of the index is stored in the operation key. + // We split the key to get the index location. + $op_index = explode('_', $trigger['#op']); + // Get the current fields data. $current_index_fields = $form['field_json_metadata']['widget'][0]['indexes']['fields']['data']['#rows'] ?? []; $current_index = $form['field_json_metadata']['widget'][0]['indexes']['data']['#rows'] ?? []; $current_dictionary_fields = $form['field_json_metadata']['widget'][0]['dictionary_fields']['data']['#rows'] ?? []; - $op = $trigger['#op']; - $op_index = explode('_', $trigger['#op']); $currently_modifying_index_fields = $form_state->get('index_fields_being_modified') != NULL ? $form_state->get('index_fields_being_modified') : []; $currently_modifying_index = $form_state->get('index_being_modified') != NULL ? $form_state->get('index_being_modified') : []; $currently_modifying_dictionary_fields = $form_state->get('dictionary_fields_being_modified') != NULL ? $form_state->get('dictionary_fields_being_modified') : []; + // If the op (trigger) contains abort, + // We're canceling the index we're currently modifying so unset it. if (str_contains($op, 'abort_index_key')) { unset($currently_modifying_index[$op_index[3]]); } - + // We're canceling the index field we're currently modifying so unset it. if (str_contains($op, 'abort_index_field_key')) { unset($currently_modifying_index_fields[$op_index[4]]); } + // If the op (trigger) contains delete, + // We're deleting the index we're editing so... if (str_contains($op, 'delete_index_key')) { + // Unset it from being currently modified. unset($currently_modifying_index[$op_index[3]]); + // Remove the respective field/data from the form. unset($current_index[$op_index[3]]); } - + // We're deleting the index field we're editing so... if (str_contains($op, 'delete_index_field_key')) { + // Unset it from being currently modified. unset($currently_modifying_index_fields[$op_index[4]]); + // Remove the respective field/data from the form. unset($current_index_fields[$op_index[4]]); } + // If the op (trigger) contains update, + // We're saving the field we're editing so... if (str_contains($op, 'update')) { + // Get the entered data. $update_values = $form_state->getUserInput(); + // Update the respective current field data with our new input data. $current_index[$op_index[3]] = IndexFieldValues::updateIndexValues($op_index[3], $update_values, $current_index); + // Unset the respective field/data from the form. unset($currently_modifying_index[$op_index[3]]); + // Sort the current index data. ksort($current_index); } + // If the op (trigger) contains edit + // We're editing a specific field so... if (str_contains($op, 'edit')) { + // Set the field we're modifying to that field. $currently_modifying_index[$op_index[3]] = $current_index[$op_index[3]]; } + // Let's retain the fields that are being modified. $form_state->set('dictionary_fields_being_modified', $currently_modifying_dictionary_fields); $form_state->set('index_fields_being_modified', $currently_modifying_index_fields); $form_state->set('index_being_modified', $currently_modifying_index); + // Let's retain the fields that are already stored on the form, + // but aren't currently being modified. $form_state->set('current_index_fields', $current_index_fields); $form_state->set('current_index', $current_index); $form_state->set('current_dictionary_fields', $current_dictionary_fields); + // Let's rebuild the form. $form_state->setRebuild(); } From beb8bbad16647ffa8fc03efef5b8782d297ae26b Mon Sep 17 00:00:00 2001 From: Daniel Mundra Date: Tue, 17 Sep 2024 11:03:29 -0700 Subject: [PATCH 2/2] Added comments, removed unused parameters, updated quotes. --- .../src/Indexes/IndexFieldCallbacks.php | 6 +++--- .../src/Indexes/IndexFieldCreation.php | 2 +- .../src/Indexes/IndexFieldEditCreation.php | 7 ++++--- .../src/Indexes/IndexFieldOperations.php | 16 ++++++++++++---- .../src/Indexes/IndexValidation.php | 3 ++- .../Field/FieldWidget/DataDictionaryWidget.php | 1 + .../DataDictionaryWidgetBuildIndexesTest.php | 5 ++--- 7 files changed, 25 insertions(+), 15 deletions(-) diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php index 664a83ca46..a7a8df0ac5 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php @@ -305,15 +305,15 @@ public static function indexFormAjax(array &$form, FormStateInterface $form_stat } /** - * Ajax callback to return index fields fieldset with Add Field button. + * Ajax callback to return index fields fieldset with 'Add Field' button. */ public static function subIndexFormFieldAjax(array &$form, FormStateInterface $form_state) { return $form["field_json_metadata"]["widget"][0]["indexes"]["field_collection"]["group"]["index"]["fields"]; } /** - * Ajax callback to return index fields fieldset with existing fields and Add - * Field button. + * Ajax callback to return index fields fieldset with existing fields and 'Add + * Field' button. */ public static function subIndexFormExistingFieldAjax(array &$form, FormStateInterface $form_state) { $form["field_json_metadata"]["widget"][0]["indexes"]["field_collection"]["group"]["index"]["fields"]["add_row_button"]['#access'] = TRUE; diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php index 77622f6744..7dce342919 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php @@ -41,7 +41,7 @@ public static function createGeneralIndex($element, $current_indexes) { } /** - * Create data index data rows. + * Create data index fields data rows. */ public static function createIndexFieldsDataRows($index_field_values, $current_index_fields, $index_fields_data_results, $form_state) { if ($index_field_values) { diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php index 3f2bce1422..8e55088f99 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php @@ -10,8 +10,9 @@ class IndexFieldEditCreation { /** * Create edit index fields. */ - public static function editIndexFields($indexKey, $current_index_fields, $index_fields_being_modified) { - $id = $current_index_fields ? "field-json-metadata-index-fields" : "field-json-metadata-index-fields-new"; + public static function editIndexFields($indexKey, $current_index_fields) { + $id = $current_index_fields ? 'field-json-metadata-index-fields' : 'field-json-metadata-index-fields-new'; + // We split the key to get the index field location. $indexKeyExplode = explode("_", $indexKey); $edit_index_fields['name'] = [ '#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_ /** * Create edit index. */ - public static function editIndex($indexKey, $current_index, $index_being_modified, $form_state) { + public static function editIndex($indexKey, $current_index, $form_state) { $id = $current_index ? "field-json-metadata-index-new" : "field-json-metadata-index"; $indexKeyExplode = explode("_", $indexKey); diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php index a394fcc454..9f4e3e22da 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php @@ -18,6 +18,7 @@ public static function setIndexFieldsAjaxElementsOnAdd(array $indexFields) { // Setting the ajax fields if they exist. if ($edit_index_button) { $indexFields['data']['#rows'][$row] = array_merge($data, $edit_index_button); + // Remove the buttons so they don't show up twice. unset($indexFields['edit_index_buttons']['index_field_key_' . $row]); } elseif ($edit_index_fields) { @@ -25,6 +26,7 @@ public static function setIndexFieldsAjaxElementsOnAdd(array $indexFields) { $indexFields['data']['#rows'][$row]['field_collection'] = $edit_index_fields; // Remove the buttons so they don't show up twice. unset($indexFields['edit_index_fields']['index_field_key_' . $row]); + // Sort the current index data. ksort($indexFields['data']['#rows']); } } @@ -46,6 +48,7 @@ public static function setIndexFieldsAjaxElements(array $indexFields) { // Setting the ajax fields if they exist. if ($edit_index_fields_button) { $indexFields['data']['#rows'][$row] = array_merge($data, $edit_index_fields_button); + // Remove the buttons so they don't show up twice. unset($indexFields['fields']['edit_index_fields_buttons']['index_field_key_' . $row]); } elseif ($edit_index_fields) { @@ -53,6 +56,7 @@ public static function setIndexFieldsAjaxElements(array $indexFields) { $indexFields['data']['#rows'][$row]['field_collection'] = $edit_index_fields; // Remove the buttons so they don't show up twice. unset($indexFields['edit_index_fields']['index_field_key_' . $row]); + // Sort the current index fields data. ksort($indexFields['data']['#rows']); } } @@ -71,6 +75,7 @@ public static function setIndexAjaxElements(array $indexes) { // Setting the ajax fields if they exist. if ($edit_index_button) { $indexes['data']['#rows'][$row] = array_merge($data, $edit_index_button); + // Remove the buttons so they don't show up twice. unset($indexes['edit_index_buttons']['index_key_' . $row]); } elseif ($edit_index) { @@ -78,6 +83,7 @@ public static function setIndexAjaxElements(array $indexes) { $indexes['data']['#rows'][$row]['field_collection'] = $edit_index; // Remove the buttons so they don't show up twice. unset($indexes['edit_index']['index_key_' . $row]); + // Sort the current index data. ksort($indexes['data']['#rows']); } @@ -203,7 +209,7 @@ public static function createIndexFieldOptions($op_index, $index_data_results, $ // Creating ajax buttons/fields to be placed in correct location later. foreach ($index_data_results as $indexKey => $data) { if (self::checkIndexEditingField('index_field_key_' . $indexKey, $op_index, $index_fields_being_modified)) { - $element['edit_index_fields']['index_field_key_' . $indexKey] = IndexFieldEditCreation::editIndexFields('index_field_key_' . $indexKey, $current_index_fields, $index_fields_being_modified); + $element['edit_index_fields']['index_field_key_' . $indexKey] = IndexFieldEditCreation::editIndexFields('index_field_key_' . $indexKey, $current_index_fields); } else { $element['edit_index_buttons']['index_field_key_' . $indexKey]['edit_index_button'] = IndexFieldButtons::editIndexButtons('index_field_key_' . $indexKey); @@ -223,7 +229,7 @@ public static function createIndexOptions($op_index, $index_data_results, $index // Creating ajax buttons/fields to be placed in correct location later. foreach ($index_data_results as $indexKey => $data) { if (self::checkIndexEditing('index_key_' . $indexKey, $op_index, $index_being_modified)) { - $element['edit_index']['index_key_' . $indexKey] = IndexFieldEditCreation::editIndex('index_key_' . $indexKey, $current_indexes, $index_being_modified, $form_state); + $element['edit_index']['index_key_' . $indexKey] = IndexFieldEditCreation::editIndex('index_key_' . $indexKey, $current_indexes, $form_state); } else { $element['edit_index_buttons']['index_key_' . $indexKey]['edit_index_button'] = IndexFieldButtons::editIndexButtons('index_key_' . $indexKey); @@ -239,7 +245,8 @@ public static function createIndexOptions($op_index, $index_data_results, $index */ public static function checkIndexEditingField($indexKey, $op_index, $index_fields_being_modified) { $action_list = IndexFieldOperations::editIndexActions(); - $indexKeyExplode = explode("_", $indexKey); + // We split the key to get the index field location. + $indexKeyExplode = explode('_', $indexKey); if (isset($op_index[0]) && in_array($op_index[0], $action_list) && array_key_exists($indexKeyExplode[3], $index_fields_being_modified)) { return TRUE; } @@ -256,7 +263,8 @@ public static function checkIndexEditing($indexKey, $op_index, $index_being_modi $op_index_string = implode('_', $op_index); if (str_contains($op_index_string, 'edit_index_key')) { $action_list = IndexFieldOperations::editIndexActions(); - $indexKeyExplode = explode("_", $indexKey); + // We split the key to get the index location. + $indexKeyExplode = explode('_', $indexKey); if (isset($op_index[0]) && in_array($op_index[0], $action_list) && array_key_exists($indexKeyExplode[2], $index_being_modified)) { return TRUE; } diff --git a/modules/data_dictionary_widget/src/Indexes/IndexValidation.php b/modules/data_dictionary_widget/src/Indexes/IndexValidation.php index 253178a435..aa3774f4ce 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexValidation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexValidation.php @@ -45,7 +45,8 @@ public static function indexFieldsValidation(array $element, FormStateInterface public static function indexFieldVal(FormStateInterface $form_state, string $field_key, string $field_label) { $trigger = $form_state->getTriggeringElement(); $op = $trigger['#op']; - $op_index = explode("_", $op); + // We split the key to get the index field location. + $op_index = explode('_', $op); // Perform validation for update operation. if (str_contains($op, 'update')) { diff --git a/modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php b/modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php index 12a1cba1f2..e0c3849814 100644 --- a/modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php +++ b/modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php @@ -52,6 +52,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen // Retrieve triggered element to be used for various operations. $op = $form_state->getTriggeringElement()['#op'] ?? NULL; + // We split the key to get the index field location. $op_index = isset($op) ? explode('_', $op) : NULL; // Retrieve form element item values. diff --git a/modules/data_dictionary_widget/tests/src/Unit/DataDictionaryWidgetBuildIndexesTest.php b/modules/data_dictionary_widget/tests/src/Unit/DataDictionaryWidgetBuildIndexesTest.php index 14cf3f71fa..33f6f33617 100644 --- a/modules/data_dictionary_widget/tests/src/Unit/DataDictionaryWidgetBuildIndexesTest.php +++ b/modules/data_dictionary_widget/tests/src/Unit/DataDictionaryWidgetBuildIndexesTest.php @@ -539,14 +539,13 @@ public function testEditDataDictionaryIndexEdit() { ] ] ]; - $index_being_modified = $current_index; $formState = $this->createMock(FormStateInterface::class); $formState->expects($this->any()) ->method('get') ->willReturnOnConsecutiveCalls(FALSE); // Act - $edit_index = IndexFieldEditCreation::editIndex($indexKey, $current_index, $index_being_modified, $formState); + $edit_index = IndexFieldEditCreation::editIndex($indexKey, $current_index, $formState); // Assert $this->assertNotNull($edit_index); @@ -572,7 +571,7 @@ public function testEditDataDictionaryIndexEditFields() { ]; // Act - $edit_index_fields = IndexFieldEditCreation::editIndexFields($indexKey, $current_index_fields, null); + $edit_index_fields = IndexFieldEditCreation::editIndexFields($indexKey, $current_index_fields); // Assert $this->assertNotNull($edit_index_fields);