From 2489108e3ebe2efadb021cae1e39dc3b1e319179 Mon Sep 17 00:00:00 2001 From: Kaise Lafrai Date: Tue, 14 May 2024 13:35:41 -0400 Subject: [PATCH] Added working cancel button for index, fixed errors appearing in log --- .../data_dictionary_widget.module | 39 +++++-- .../src/Fields/FieldCallbacks.php | 8 +- .../src/Indexes/IndexFieldAddCreation.php | 40 +++---- .../src/Indexes/IndexFieldButtons.php | 14 +-- .../src/Indexes/IndexFieldCallbacks.php | 30 ++++- .../src/Indexes/IndexFieldCreation.php | 51 ++++++--- .../src/Indexes/IndexFieldEditCreation.php | 28 ++++- .../src/Indexes/IndexFieldOperations.php | 103 ++++++++++++------ .../src/Indexes/IndexFieldValues.php | 4 +- .../FieldWidget/DataDictionaryWidget.php | 75 ++++++++++--- .../templates/custom-index-table.html.twig | 23 ++-- 11 files changed, 295 insertions(+), 120 deletions(-) diff --git a/modules/data_dictionary_widget/data_dictionary_widget.module b/modules/data_dictionary_widget/data_dictionary_widget.module index e6f463556d..50939bbc6c 100644 --- a/modules/data_dictionary_widget/data_dictionary_widget.module +++ b/modules/data_dictionary_widget/data_dictionary_widget.module @@ -113,21 +113,44 @@ function data_dictionary_widget_form_alter(&$form, &$form_state, $form_id) { if ($form_id == 'node_data_edit_form' || $form_id == 'node_data_form') { $form['#validate'][] = 'data_dictionary_widget_validate_unique_identifier'; - $current_fields = !empty($form["field_json_metadata"]["widget"][0]["dictionary_fields"]["current_fields"]) ? $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["current_fields"] : NULL; + $current_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_fields) { foreach ($current_fields as $key => $value) { - $keys = array_keys($value); - $formatted_current_fields[$key] = []; + $keys = array_keys($value); + $formatted_current_fields[$key] = []; + + foreach ($keys as $attr) { + $formatted_current_fields[$key][$attr] = [ + '#value' => $value[$attr] + ]; + } + } + + $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["current_dictionary_fields"] = $formatted_current_fields; + } - foreach ($keys as $attr) { - $formatted_current_fields[$key][$attr] = [ - '#value' => $value[$attr] - ]; + // The form element render array prefers child keys to be stored as arrays with a #value property. + if ($current_index_fields) { + foreach ($current_index_fields as &$item) { + foreach ($item as $key => &$value) { + if ($key === 'fields' && is_array($value)) { + foreach ($value as &$field) { + foreach ($field as $fieldKey => &$fieldValue) { + // Add '#value' key to each field + $fieldValue = ['#value' => $fieldValue]; + } + } + } else { + // For non-'fields' keys, add '#value' key to the value + $value = ['#value' => $value]; } + } } - $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["current_fields"] = $formatted_current_fields; + + $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 46569289f6..268f4beef4 100644 --- a/modules/data_dictionary_widget/src/Fields/FieldCallbacks.php +++ b/modules/data_dictionary_widget/src/Fields/FieldCallbacks.php @@ -41,7 +41,7 @@ public static function updateFormatOptions(array &$form, FormStateInterface $for public static function editSubformCallback(array &$form, FormStateInterface $form_state) { // Get the current fields data $current_dictionary_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"]; - $current_index_fields = $form["field_json_metadata"]["widget"][0]["indexes"]["index_fields"]["data"]["#rows"]; + $current_index_fields = $form["field_json_metadata"]["widget"][0]["indexes"]["fields"]["data"]["#rows"]; // Get the field index from the triggering op attribute // so we can use it to store the respective field later $op_index = explode("_", $form_state->getTriggeringElement()['#op']); @@ -100,10 +100,13 @@ public static function editSubformCallback(array &$form, FormStateInterface $for public static function addSubformCallback(array &$form, FormStateInterface $form_state) { $trigger = $form_state->getTriggeringElement(); $op = $trigger['#op']; - $current_index_fields = $form["field_json_metadata"]["widget"][0]["indexes"]["index_fields"]["data"]["#rows"]; + //$current_index_fields = $form["field_json_metadata"]["widget"][0]["indexes"]["fields"]["data"]["#rows"]; $form_state->set('add_new_field', ''); // $fields_being_added = $form_state->set('fields_being_added', ''); $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"]; + if ($current_dictionary_fields) { $form_state->set('current_dictionary_fields', $current_dictionary_fields); } @@ -123,6 +126,7 @@ public static function addSubformCallback(array &$form, FormStateInterface $form $form_state->set('cancel', FALSE); } $form_state->set('current_index_fields', $current_index_fields); + $form_state->set('current_index', $current_index); $form_state->setRebuild(); } diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php index efab72ff3a..fdcc628315 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php @@ -21,41 +21,43 @@ public static function addIndex() { ]; //$add_index['group']['indexes']['add_row_button'] = IndexFieldButtons::addIndexFieldButton(); - $add_index['group']['indexes']['index_description'] = [ - '#name' => 'field_json_metadata[0][index][field_collection][group][index_description]', + $add_index['group']['indexes']['description'] = [ + '#name' => 'field_json_metadata[0][index][field_collection][group][description]', '#description' => t('Description of index purpose or functionality.'), '#type' => 'textfield', + //'#required' => TRUE, '#title' => 'Name', ]; - $add_index['group']['indexes']['index_type'] = [ - '#name' => 'field_json_metadata[0][index][field_collection][group][index_type]', + $add_index['group']['indexes']['type'] = [ + '#name' => 'field_json_metadata[0][index][field_collection][group][type]', '#type' => 'select', '#description' => t('Index type.'), - //'#required' => TRUE, '#title' => 'Index Type', '#default_value' => 'index', '#op' => 'index_type', + '#required' => TRUE, '#options' => [ - 'string' => t('index'), - 'date' => t('fulltext'), + 'index' => t('index'), + 'fulltext' => t('fulltext'), ], ]; - $add_index['group']['indexes']['index_fields'] = [ + $add_index['group']['indexes']['fields'] = [ '#type' => 'fieldset', '#title' => t('Fields'), - '#prefix' => '
', + '#required' => TRUE, + '#prefix' => '
', '#suffix' => '
', - '#markup' => t('
One or more fields included in index. Must be keys from the fields object.
'), + '#markup' => t('
Test One or more fields included in index. Must be keys from the fields object.
'), ]; - $add_index['group']['indexes']['index_fields']['add_row_button'] = IndexFieldButtons::addIndexFieldButton(); + $add_index['group']['indexes']['fields']['add_row_button'] = IndexFieldButtons::addIndexFieldButton(); //$add_index['group']['indexes']['add_row_button'] = self::createIndexActionFields(); - $add_index['group']['indexes']['save_index']['add_row_button'] = IndexFieldButtons::submitIndexButton('add_index', NULL); - $add_index['group']['indexes']['cancel_index']['add_row_button'] = IndexFieldButtons::cancelIndexButton('cancel_index', NULL); + $add_index['group']['indexes']['save_index'] = IndexFieldButtons::submitIndexButton('add_index', NULL); + $add_index['group']['indexes']['cancel_index'] = IndexFieldButtons::cancelIndexButton('cancel_index', NULL); return $add_index; } @@ -68,7 +70,7 @@ public static function addIndexFields() { $add_index_fields['group'] = [ '#type' => 'fieldset', '#title' => t('Add new field'), - '#prefix' => '
', + '#prefix' => '
', '#suffix' => '
', ]; @@ -77,13 +79,13 @@ public static function addIndexFields() { // '#suffix' => '
', // ]; - $add_index_fields['group']['indexes']['index_fields']['name'] = [ - '#name' => 'field_json_metadata[0][index_fields][field_collection][group][name]', + $add_index_fields['group']['indexes']['fields']['name'] = [ + '#name' => 'field_json_metadata[0][fields][field_collection][group][name]', '#type' => 'textfield', '#title' => 'Name', ]; - $add_index_fields['group']['indexes']['index_fields']['length'] = self::createIndexFieldLengthField(); - $add_index_fields['group']['indexes']['index_fields']['actions'] = self::createIndexActionFields(); + $add_index_fields['group']['indexes']['fields']['length'] = self::createIndexFieldLengthField(); + $add_index_fields['group']['indexes']['fields']['actions'] = self::createIndexActionFields(); return $add_index_fields; } @@ -93,7 +95,7 @@ public static function addIndexFields() { */ private static function createIndexFieldLengthField() { return [ - '#name' => 'field_json_metadata[0][index_fields][field_collection][group][length]', + '#name' => 'field_json_metadata[0][fields][field_collection][group][length]', '#type' => 'number', '#title' => 'Length', ]; diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php index 4fe4fba5b7..301d5cbf4a 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php @@ -24,7 +24,7 @@ public static function addIndexFieldButton() { ], '#ajax' => [ 'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexformAjax', - 'wrapper' => 'field-json-metadata-dictionary-index-fields', + 'wrapper' => 'field-json-metadata-dictionary-index-fields-new', 'effect' => 'fade', ], '#limit_validation_errors' => [], @@ -104,7 +104,7 @@ public static function submitIndexFieldButton($location, $indexKey) { ], '#ajax' => [ 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexformAjax', - 'wrapper' => 'field-json-metadata-dictionary-index-fields', + 'wrapper' => 'field-json-metadata-dictionary-index-fields-new', 'effect' => 'fade', ], '#limit_validation_errors' => [], @@ -134,8 +134,8 @@ public static function submitIndexButton($location, $indexKey) { ], ], '#ajax' => [ - 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexformAjax', - 'wrapper' => 'field-json-metadata-dictionary-index', + 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexformAjax', + 'wrapper' => 'field-json-metadata-dictionary-indexes', 'effect' => 'fade', ], '#limit_validation_errors' => [], @@ -181,7 +181,7 @@ public static function cancelIndexFieldButton($location, $indexKey) { * Create Cancel button. */ public static function cancelIndexButton($location, $indexKey) { - $callbackClass = $location == 'edit' ? 'indexEditSubformCallback' : 'indexAddSubformCallback'; + $callbackClass = $location == 'edit' ? 'indexEditCallback' : 'indexAddCallback'; $op = $location == 'edit' && $indexKey ? 'abort_' . $indexKey : 'cancel_index'; $cancel_index_button = [ '#type' => 'submit', @@ -194,8 +194,8 @@ public static function cancelIndexButton($location, $indexKey) { ], ], '#ajax' => [ - 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexformAjax', - 'wrapper' => 'field-json-metadata-dictionary-index', + 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexformAjax', + 'wrapper' => 'field-json-metadata-dictionary-indexes', 'effect' => 'fade', ], '#limit_validation_errors' => [], diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php index 0d938d38cd..c1b3027900 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php @@ -15,10 +15,13 @@ public static function indexAddSubformCallback(array &$form, FormStateInterface $trigger = $form_state->getTriggeringElement(); $op = $trigger['#op']; $current_dictionary_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"]; - $form_state->set('add_new_index_field', ''); + //$form_state->set('add_index_field', ''); + //$form_state->set('add_new_index_field', ''); + //$form_state->set('new_index_fields', ''); // $fields_being_added = $form_state->set('fields_being_added', ''); - $current_index_fields = $form["field_json_metadata"]["widget"][0]['indexes']["index_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; + //$existing_index_fields = $current_index[0]["fields"]; if ($current_index_fields) { $form_state->set('current_index_fields', $current_index_fields); @@ -29,17 +32,23 @@ public static function indexAddSubformCallback(array &$form, FormStateInterface } if ($op === 'add_new_index_field') { + $form_state->set('add_index_field', ''); $add_index_fields = IndexFieldAddCreation::addIndexFields(); $form_state->set('add_new_index_field', $add_index_fields); + $form_state->set('index_added', FALSE); + $form_state->set('adding_new_index_fields', TRUE); } if ($op === 'add_index_field') { + $form_state->set('add_new_index_field', ''); $form_state->set('new_index_fields', $form_state->getUserInput()); $form_state->set('add', TRUE); $form_state->set('cancel_index_field', FALSE); + $form_state->set('adding_new_index_fields', FALSE); } $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); $form_state->setRebuild(); } @@ -51,11 +60,19 @@ public static function indexAddCallback(array &$form, FormStateInterface $form_s $trigger = $form_state->getTriggeringElement(); $op = $trigger['#op']; $current_dictionary_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"]; + $form_state->set('add_new_index_field', ''); + $form_state->set('new_index_fields', ''); $form_state->set('add_new_index', ''); + //$form_state->set('new_index', ''); + //$form_state->set('current_index_fields', ''); // $fields_being_added = $form_state->set('fields_being_added', ''); + $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); + //$form_state->set('current_index_fields', ''); } if ($op === 'cancel_index') { @@ -64,17 +81,22 @@ public static function indexAddCallback(array &$form, FormStateInterface $form_s if ($op === 'add_new_index') { $add_new_index = IndexFieldAddCreation::addIndex(); + $form_state->set('new_index', ''); + //$form_state->set('current_index_fields', $current_index_fields); $form_state->set('add_new_index', $add_new_index); } if ($op === 'add_index') { + $form_state->set('add_new_index', ''); $form_state->set('new_index', $form_state->getUserInput()); $form_state->set('add', TRUE); + $form_state->set('index_added', TRUE); $form_state->set('cancel_index', FALSE); } $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); $form_state->setRebuild(); } @@ -83,7 +105,7 @@ public static function indexAddCallback(array &$form, FormStateInterface $form_s */ public static function indexEditSubformCallback(array &$form, FormStateInterface $form_state) { $trigger = $form_state->getTriggeringElement(); - $current_index_fields = $form["field_json_metadata"]["widget"][0]["indexes"]["index_fields"]["data"]["#rows"]; + $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']); @@ -122,7 +144,7 @@ public static function indexEditSubformCallback(array &$form, FormStateInterface * Ajax callback. */ public static function subIndexformAjax(array &$form, FormStateInterface $form_state) { - return $form["field_json_metadata"]["widget"][0]["indexes"]["index_fields"]; + return $form["field_json_metadata"]["widget"][0]["indexes"]["fields"]; } /** diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php index 59f9b05a50..5d4e5d3a75 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php @@ -11,16 +11,23 @@ class IndexFieldCreation { */ public static function createGeneralIndexFields($element, $field_json_metadata, $current_index_fields, $new_index, $index_fields_being_modified) { - $element['indexes']['index_fields'] = [ + $element['indexes']['fields'] = [ '#access' => TRUE, '#type' => 'fieldset', '#title' => t('Fields'), - '#prefix' => '
', + '#prefix' => '
', '#suffix' => '
', '#markup' => t('
One or more fields included in index. Must be keys from the fields object.
'), ]; - $element['indexes']['index_fields']['current_index_fields'] = $current_index_fields; + // if ($new_index) { + // $element['indexes']['index_fields']['current_index_fields'] = []; + // } else { + // $element['indexes']['index_fields']['current_index_fields'] = $current_index_fields; + // } + + + return $element; } @@ -46,15 +53,15 @@ public static function createGeneralIndex($element, $field_json_metadata, $curre /** * Create data index data rows. */ - public static function createIndexFieldsDataRows($current_index_fields, $index_data_results, $form_state) { + public static function createNewIndexFieldsDataRows($current_index_fields, $index_fields_data_results, $index_data_results, $form_state) { return [ - '#access' => ((bool) $current_index_fields || (bool) $index_data_results), + '#access' => ((bool) $current_index_fields || (bool) $index_data_results || (bool) $index_fields_data_results), '#type' => 'table', '#header' => ['NAME', 'LENGTH'], - '#prefix' => '
', - '#suffix' => '
', - '#rows' => $form_state->get('cancel_index_field') ? $current_index_fields : ($index_data_results ?? []), + // '#prefix' => '
', + // '#suffix' => '
', + '#rows' => $form_state->get('cancel_index_field') ? $current_index_fields : ($index_fields_data_results ?? []), '#tree' => TRUE, '#theme' => 'custom_index_fields_table', ]; @@ -64,16 +71,34 @@ public static function createIndexFieldsDataRows($current_index_fields, $index_d /** * Create data index data rows. */ - public static function createIndexDataRows($current_indexes, $index_data_results, $form_state) { + public static function createIndexFieldsDataRows($index_added, $adding_new_index_fields, $index_field_values, $index_values, $current_index_fields, $index_fields_data_results, $index_data_results, $form_state) { + if ($index_field_values) { + return [ + '#access' => ((bool) $current_index_fields || (bool) $index_fields_data_results), + '#type' => 'table', + '#header' => ['NAME', 'LENGTH'], + '#prefix' => '
', + '#suffix' => '
', + '#rows' => $form_state->get('cancel_index_field') ? $current_index_fields : ($index_fields_data_results ?? []), + '#tree' => TRUE, + '#theme' => 'custom_index_fields_table', + ]; + } + } + /** + * Create data index data rows. + */ + public static function createIndexDataRows($current_indexes, $index_data, $form_state) { return [ - '#access' => ((bool) $current_indexes || (bool) $index_data_results), + '#access' => ((bool) $current_indexes || (bool) $index_data), '#type' => 'table', - '#header' => ['INDEX'], - '#rows' => $form_state->get('cancel_index') ? $current_indexes : ($index_data_results ?? []), + '#header' => ['NAME', 'TYPE', 'FIELDS'], + '#prefix' => '
', + '#suffix' => '
', + '#rows' => $form_state->get('cancel_index') ? $current_indexes : ($index_data ?? []), '#tree' => TRUE, '#theme' => 'custom_index_table', ]; - } } \ No newline at end of file diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php index ae7f34a78e..af61cefda7 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php @@ -13,13 +13,13 @@ class IndexFieldEditCreation { public static function editIndexFields($indexKey, $current_index_fields) { $indexKeyExplode = explode("_", $indexKey); $edit_index_fields['name'] = [ - '#name' => 'field_json_metadata[0][index_fields][data][' . $indexKeyExplode[3] . '][field_collection][name]', + '#name' => 'field_json_metadata[0][fields][data][' . $indexKeyExplode[3] . '][field_collection][name]', '#type' => 'textfield', '#value' => $current_index_fields[$indexKeyExplode[3]]['name'], '#title' => 'Name', ]; $edit_index_fields['length'] = [ - '#name' => 'field_json_metadata[0][index_fields][data]['. $indexKeyExplode[3] .'][field_collection][length]', + '#name' => 'field_json_metadata[0][fields][data]['. $indexKeyExplode[3] .'][field_collection][length]', '#type' => 'number', '#value' => $current_index_fields[$indexKeyExplode[3]]['length'], '#title' => 'Length', @@ -29,6 +29,30 @@ public static function editIndexFields($indexKey, $current_index_fields) { $edit_index_fields['update_index_field']['actions'] = self::createIndexActionFields($indexKey); return $edit_index_fields; + } + + /** + * Create edit fields for Data Dictionary Widget. + */ + public static function editIndex($indexKey, $current_index) { + $indexKeyExplode = explode("_", $indexKey); + $edit_index['name'] = [ + '#name' => 'field_json_metadata[0][index][data][' . $indexKeyExplode[3] . '][field_collection][name]', + '#type' => 'textfield', + '#value' => $current_index[$indexKeyExplode[3]]['name'], + '#title' => 'Name', + ]; + $edit_index['length'] = [ + '#name' => 'field_json_metadata[0][index][data]['. $indexKeyExplode[3] .'][field_collection][length]', + '#type' => 'number', + '#value' => $current_index[$indexKeyExplode[3]]['length'], + '#title' => 'Length', + ]; + + + $edit_index['update_index_field']['actions'] = self::createIndexActionFields($indexKey); + return $edit_index; + } /** diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php index 9df6f447cc..ec90a5b205 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php @@ -10,22 +10,23 @@ class IndexFieldOperations { * Setting ajax elements. */ public static function setIndexFieldsAjaxElements(array $dictionaryIndexFields) { - foreach ($dictionaryIndexFields['data']['#rows'] as $row => $data) { - $edit_index_button = $dictionaryIndexFields['edit_index_buttons']['index_field_key_' . $row] ?? NULL; - $edit_index_fields = $dictionaryIndexFields['edit_index_fields']['index_field_key_' . $row] ?? NULL; - // Setting the ajax fields if they exsist. - if ($edit_index_button) { - $dictionaryIndexFields['data']['#rows'][$row] = array_merge($data, $edit_index_button); - unset($dictionaryIndexFields['edit_index_buttons']['index_field_key_' . $row]); + if ($dictionaryIndexFields["data"]) { + foreach ($dictionaryIndexFields['data']['#rows'] as $row => $data) { + $edit_index_button = $dictionaryIndexFields['edit_index_buttons']['index_field_key_' . $row] ?? NULL; + $edit_index_fields = $dictionaryIndexFields['edit_index_fields']['index_field_key_' . $row] ?? NULL; + // Setting the ajax fields if they exsist. + if ($edit_index_button) { + $dictionaryIndexFields['data']['#rows'][$row] = array_merge($data, $edit_index_button); + unset($dictionaryIndexFields['edit_index_buttons']['index_field_key_' . $row]); + } + elseif ($edit_index_fields) { + unset($dictionaryIndexFields['data']['#rows']['index_field_key_' . $row]); + $dictionaryIndexFields['data']['#rows'][$row]['field_collection'] = $edit_index_fields; + // Remove the buttons so they don't show up twice. + unset($dictionaryIndexFields['edit_index_fields']['index_field_key_' . $row]); + ksort($dictionaryIndexFields['data']['#rows']); + } } - elseif ($edit_index_fields) { - unset($dictionaryIndexFields['data']['#rows']['index_field_key_' . $row]); - $dictionaryIndexFields['data']['#rows'][$row]['field_collection'] = $edit_index_fields; - // Remove the buttons so they don't show up twice. - unset($dictionaryIndexFields['edit_index_fields']['index_field_key_' . $row]); - ksort($dictionaryIndexFields['data']['#rows']); - } - } return $dictionaryIndexFields; @@ -36,19 +37,19 @@ public static function setIndexFieldsAjaxElements(array $dictionaryIndexFields) */ public static function setIndexAjaxElements(array $dictionaryIndexes) { foreach ($dictionaryIndexes['data']['#rows'] as $row => $data) { - $edit_index_button = $dictionaryIndexFields['edit_index_buttons']['index_field_key_' . $row] ?? NULL; - $edit_index_fields = $dictionaryIndexFields['edit_index_fields']['index_field_key_' . $row] ?? NULL; + $edit_index_button = $dictionaryIndexes['edit_index_buttons']['index_key_' . $row] ?? NULL; + $edit_index_fields = $dictionaryIndexes['edit_index_fields']['index_key_' . $row] ?? NULL; // Setting the ajax fields if they exsist. if ($edit_index_button) { - $dictionaryIndexFields['data']['#rows'][$row] = array_merge($data, $edit_index_button); - unset($dictionaryIndexFields['edit_index_buttons']['index_field_key_' . $row]); + $dictionaryIndexes['data']['#rows'][$row] = array_merge($data, $edit_index_button); + unset($dictionaryIndexes['edit_index_buttons']['index_key_' . $row]); } elseif ($edit_index_fields) { - unset($dictionaryIndexFields['data']['#rows']['index_field_key_' . $row]); - $dictionaryIndexFields['data']['#rows'][$row]['field_collection'] = $edit_index_fields; + unset($dictionaryIndexes['data']['#rows']['index_key_' . $row]); + $dictionaryIndexes['data']['#rows'][$row]['field_collection'] = $edit_index_fields; // Remove the buttons so they don't show up twice. - unset($dictionaryIndexFields['edit_index_fields']['index_field_key_' . $row]); - ksort($dictionaryIndexFields['data']['#rows']); + unset($dictionaryIndexes['edit_index_fields']['index_key_' . $row]); + ksort($dictionaryIndexes['data']['#rows']); } } @@ -59,13 +60,13 @@ public static function setIndexAjaxElements(array $dictionaryIndexes) { /** * Cleaning the data up. */ - public static function processIndexDataResults($index_data_results, $current_index_fields, $index_field_values, $op) { + public static function processIndexFieldsDataResults($index_data_results, $current_index_fields, $index_field_values, $op) { if (isset($current_index_fields)) { $index_data_results = $current_index_fields; } - if (isset($index_field_values["field_json_metadata"][0]["index_fields"]["field_collection"])) { - $index_field_group = $index_field_values["field_json_metadata"][0]["index_fields"]["field_collection"]["group"]; + if (isset($index_field_values["field_json_metadata"][0]["fields"]["field_collection"])) { + $index_field_group = $index_field_values["field_json_metadata"][0]["fields"]["field_collection"]["group"]; $data_index_fields_pre = [ [ @@ -82,6 +83,33 @@ public static function processIndexDataResults($index_data_results, $current_ind return $index_data_results; } + /** + * Cleaning the data up. + */ + public static function processIndexDataResults($index_results, $current_index, $index_values, $index_fields_data_results, $op) { + if (isset($current_index)) { + $index_results = $current_index; + } + + if (isset($index_values["field_json_metadata"][0]["index"]["field_collection"])) { + $index_group = $index_values["field_json_metadata"][0]["index"]["field_collection"]["group"]; + + $data_index_pre = [ + [ + "description" => $index_group["description"], + "type" => $index_group["type"], + "fields" => $index_fields_data_results, + ], + ]; + } + + if (isset($data_index_pre) && $op === "add_index") { + $index_results = isset($current_index) ? array_merge($current_index, $data_index_pre) : $data_index_pre; + } + + return $index_results; + } + /** * Return acceptable edit actions. */ @@ -98,15 +126,21 @@ public static function editIndexActions() { /** * Set the elements associated with adding a new field. */ - public static function setAddIndexFieldFormState($add_new_index_field, $element) { + public static function setAddIndexFieldFormState($add_new_index_field, $new_index_field, $element) { if ($add_new_index_field) { - $element['indexes']['index_fields']['field_collection'] = $add_new_index_field; - $element['indexes']['index_fields']['field_collection']['#access'] = TRUE; - $element['indexes']['index_fields']['add_row_button']['#access'] = FALSE; + $element['indexes']['fields']['#access'] = FALSE; + $element['indexes']['fields']['field_collection'] = $add_new_index_field; + $element['indexes']['fields']['field_collection']['#access'] = TRUE; + $element['indexes']['fields']['add_row_button']['#access'] = FALSE; $element['identifier']['#required'] = FALSE; $element['title']['#required'] = FALSE; - } + } + + + // else{ + // $element['indexes']['fields']['#access'] = FALSE; + // } return $element; } @@ -116,6 +150,7 @@ public static function setAddIndexFieldFormState($add_new_index_field, $element) public static function setAddIndexFormState($add_new_index, $element) { if ($add_new_index) { + //$element['indexes']['#access'] = FALSE; $element['indexes']['field_collection'] = $add_new_index; $element['indexes']['field_collection']['#access'] = TRUE; $element['indexes']['add_row_button']['#access'] = FALSE; @@ -151,11 +186,11 @@ public static function createDictionaryIndexOptions($op_index, $index_data_resul $current_indexes = $element['current_index']; // 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_indexes, $index_fields_being_modified); + if (self::checkIndexEditingField('index_key_' . $indexKey, $op_index, $index_fields_being_modified)) { + $element['edit_index']['index_key_' . $indexKey] = IndexFieldEditCreation::editIndex('index_key_' . $indexKey, $current_indexes, $index_fields_being_modified); } else { - $element['edit_index_buttons']['index_field_key_' . $indexKey]['edit_index_button'] = IndexFieldButtons::editIndexButtons('index_field_key_' . $indexKey); + $element['edit_index_buttons']['index_key_' . $indexKey]['edit_index_button'] = IndexFieldButtons::editIndexButtons('index_key_' . $indexKey); } } $element['add_row_button'] = IndexFieldButtons::addIndexButton(); diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php index 2f8e40291d..b3c881a3ec 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php @@ -10,8 +10,8 @@ class IndexFieldValues { * Return updated index field values after edit. */ public static function updateIndexFieldValues($field_index, $update_values, $current_index_fields) { - $name = $update_values['field_json_metadata'][0]['index_fields']['data'][$field_index]['field_collection']['name']; - $length = $update_values['field_json_metadata'][0]['index_fields']['data'][$field_index]['field_collection']['length']; + $name = $update_values['field_json_metadata'][0]['fields']['data'][$field_index]['field_collection']['name']; + $length = $update_values['field_json_metadata'][0]['fields']['data'][$field_index]['field_collection']['length']; return [ 'name' => $name, 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 fda14d71e8..6f97cbb5b9 100644 --- a/modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php +++ b/modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php @@ -12,6 +12,7 @@ use Drupal\Core\Entity\EntityFormInterface; use Drupal\data_dictionary_widget\Indexes\IndexFieldCreation; use Drupal\data_dictionary_widget\Indexes\IndexFieldOperations; +use PHPUnit\Framework\Constraint\IsTrue; /** * A data-dictionary widget. @@ -33,12 +34,14 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen $field_values = $form_state->get("new_dictionary_fields"); $index_values = $form_state->get('new_index'); $index_field_values = $form_state->get("new_index_fields"); + $index_added = $form_state->get("index_added"); + $adding_new_index_fields = $form_state->get('adding_new_index_fields'); $current_fields = $form_state->get('current_dictionary_fields'); $current_indexes = $form_state->get('current_index'); $current_index_fields = $form_state->get('current_index_fields'); - $fields_being_modified = $form_state->get("fields_being_modified") ?? NULL; + $fields_being_modified = $form_state->get("dictionary_fields_being_modified") ?? NULL; $index_fields_being_modified = $form_state->get("index_fields_being_modified") ?? NULL; $op = $form_state->getTriggeringElement()['#op'] ?? NULL; @@ -46,13 +49,24 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen $op_index = isset($form_state->getTriggeringElement()['#op']) ? explode("_", $form_state->getTriggeringElement()['#op']) : NULL; $data_results = $field_json_metadata ? $field_json_metadata["data"]["fields"] : []; - $index_data_results = $field_json_metadata ? $field_json_metadata["data"]["indexes"][0]["fields"] : []; + + // Combine current and new index fields + $index_fields_results = $field_json_metadata ? $field_json_metadata["data"]["indexes"][0]["fields"] : []; + $index_results = $field_json_metadata ? $field_json_metadata["data"]["indexes"] : []; + // Build the data_results array to display the rows in the data table. $data_results = FieldOperations::processDataResults($data_results, $current_fields, $field_values, $op); + // Build the index_field_data_results array to display the rows in the data table. + $index_fields_data_results = IndexFieldOperations::processIndexFieldsDataResults($index_fields_results, $current_index_fields, $index_field_values, $op); + // Build the index_data_results array to display the rows in the data table. - $index_data_results = IndexFieldOperations::processIndexDataResults($index_data_results, $current_index_fields, $index_field_values, $op); + $index_data_results = IndexFieldOperations::processIndexDataResults($index_results, $current_indexes, $index_values, $index_fields_data_results, $op); + + // if ($index_data_results) { + // unset($current_index_fields); + // } $element = FieldCreation::createGeneralFields($element, $field_json_metadata, $current_fields, $form_state); $element = IndexFieldCreation::createGeneralIndex($element, $field_json_metadata, $current_indexes, $form_state); @@ -69,13 +83,15 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen [$this, 'preRenderIndexForm'], ]; - $element['indexes']['index_fields']['#pre_render'] = [ + $element['indexes']['fields']['#pre_render'] = [ [$this, 'preRenderIndexFieldForm'], ]; $element['dictionary_fields']['data'] = FieldCreation::createDictionaryDataRows($current_fields, $data_results, $form_state); + //$element["indexes"]["data"]["#rows"][0]["index_fields"]; + //$element['indexes'][] = $index_data_results; $element['indexes']['data'] = IndexFieldCreation::createIndexDataRows($current_indexes, $index_data_results, $form_state); - $element['indexes']['index_fields']['data'] = IndexFieldCreation::createIndexFieldsDataRows($current_index_fields, $index_data_results, $form_state); + $element['indexes']['fields']['data'] = IndexFieldCreation::createIndexFieldsDataRows($index_added, $adding_new_index_fields, $index_field_values, $index_values, $current_index_fields, $index_fields_data_results, $index_data_results, $form_state); // Creating ajax buttons/fields to be placed in correct location later. $element['dictionary_fields'] = FieldOperations::createDictionaryFieldOptions($op_index, $data_results, $fields_being_modified, $element['dictionary_fields']); @@ -84,9 +100,11 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen // Creating ajax buttons/fields to be placed in correct location later for index fields. $element['indexes'] = IndexFieldOperations::createDictionaryIndexOptions($op_index, $index_data_results, $index_fields_being_modified, $element['indexes']); - $element["indexes"]["index_fields"] = IndexFieldOperations::createDictionaryIndexFieldOptions($op_index, $index_data_results, $index_fields_being_modified, $element['indexes']['index_fields']); - $element['indexes']['index_fields']['add_row_button']['#access'] = $index_field_values ? TRUE : FALSE; - + if ($index_field_values || $current_index_fields) { + $element["indexes"]["fields"] = IndexFieldOperations::createDictionaryIndexFieldOptions($op_index, $index_fields_data_results, $index_fields_being_modified, $element['indexes']['fields']); + } + $element['indexes']['fields']['add_row_button']['#access'] = $index_field_values ? TRUE : FALSE; + $form_object = $form_state->getFormObject(); if (!($form_object instanceof EntityFormInterface)) { return; @@ -97,8 +115,33 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen $form_entity->set('field_data_type', 'data-dictionary'); } $element = FieldOperations::setAddFormState($form_state->get('add_new_field'), $element); + + $element = IndexFieldOperations::setAddIndexFormState($form_state->get('add_new_index'), $element); - $element = IndexFieldOperations::setAddIndexFieldFormState($form_state->get('add_new_index_field'), $element); + + //if (empty($element['indexes'])) { + $element = IndexFieldOperations::setAddIndexFieldFormState($form_state->get('add_new_index_field'), $form_state->get('add_index_field'), $element); + //} + + // if ($form_state->get('add_new_index_field') || $form_state->get('new_index_fields')) { + // $element['indexes']['index_fields']['#access'] = TRUE; + // } else { + // $element['indexes']['index_fields']['#access'] = FALSE; + // } + + if ($form_state->get('add_new_index_field') || $form_state->get('new_index_fields')) { + $element['indexes']['fields']['#access'] = TRUE; + } else { + $element['indexes']['fields']['#access'] = FALSE; + } + + //$element['indexes']['fields']['#access'] = TRUE; + + // if ($adding_new_index_fields ||) { + // $element['indexes']['fields']['#access'] = TRUE; + // } + + return $element; } @@ -108,11 +151,11 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen */ public function massageFormValues(array $values, array $form, FormStateInterface $form_state) { $current_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"]; - $current_indexes = $form["field_json_metadata"]["widget"][0]["indexes"]["index_fields"]["data"]["#rows"]; + $current_indexes = $form["field_json_metadata"]["widget"][0]["indexes"]["data"]["#rows"]; //$current_indexes = isset($values[0]["indexes"]) ? json_decode($values[0]["indexes"]) : NULL; $field_collection = $values[0]['dictionary_fields']["field_collection"]["group"] ?? []; - $indexes_collection = $values[0]["indexes"]["index_fields"]["field_collection"]["group"] ?? []; + $indexes_collection = $values[0]["indexes"]["fields"]["field_collection"]["group"] ?? []; $fields_input = !empty($field_collection) ? [ [ @@ -126,8 +169,8 @@ public function massageFormValues(array $values, array $form, FormStateInterface $index_inputs = !empty($indexes_collection) ? [ [ - "name" => $indexes_collection["name"], - "length" => (int)$indexes_collection["length"], + "name" => $indexes_collection["indexes"]["fields"]["name"], + "length" => (int)$indexes_collection["indexes"]["fields"]["length"], ], ] : []; @@ -147,11 +190,7 @@ public function massageFormValues(array $values, array $form, FormStateInterface 'data' => [ 'title' => $values[0]['title'] ?? '', 'fields' => $fields, - 'indexes' => [ - [ - 'fields' => $indexes - ] - ], + 'indexes' => $indexes, ], ]; diff --git a/modules/data_dictionary_widget/templates/custom-index-table.html.twig b/modules/data_dictionary_widget/templates/custom-index-table.html.twig index ed092b89f8..fe13096e3f 100644 --- a/modules/data_dictionary_widget/templates/custom-index-table.html.twig +++ b/modules/data_dictionary_widget/templates/custom-index-table.html.twig @@ -8,25 +8,26 @@ {% for row in rows %} - {% if row.field_collection %} + {% if row.field_collection %} - + {{ row.field_collection }} - {% elseif row.name %} + {% elseif row.description %} - {{ row.name }} - {{ row.title }} + {{ row.description }} + {{ row.type }} -
Data Type: {{ row.type }}
-
Format: {{ row.format }}
- {% if row.description %} -
Description: {{ row.description }}
- {% endif %} + {% for field in row.fields %} +
    +
  • Field Name: {{ field.name }}
    +
    Field Length: {{ field.length }}
  • +
+ {% endfor %} - {{ row.edit_button }} + {{ row.edit_index_button }} {% endif %}