From ad69e647ca0c7409ce649358c6a11f35b0ff7be5 Mon Sep 17 00:00:00 2001 From: Kaise Lafrai Date: Mon, 29 Apr 2024 17:23:04 -0400 Subject: [PATCH 01/10] Initial commit of updates to include indexes and index fields to the data dictionary --- .../data_dictionary_widget.module | 16 ++ .../src/Fields/FieldCallbacks.php | 75 ++++-- .../src/Fields/FieldCreation.php | 11 +- .../src/Fields/FieldEditCreation.php | 36 +-- .../src/Fields/FieldOperations.php | 20 +- .../src/Fields/FieldValues.php | 2 +- .../src/Indexes/IndexFieldAddCreation.php | 112 +++++++++ .../src/Indexes/IndexFieldButtons.php | 233 ++++++++++++++++++ .../src/Indexes/IndexFieldCallbacks.php | 134 ++++++++++ .../src/Indexes/IndexFieldCreation.php | 79 ++++++ .../src/Indexes/IndexFieldEditCreation.php | 46 ++++ .../src/Indexes/IndexFieldOperations.php | 179 ++++++++++++++ .../src/Indexes/IndexFieldValues.php | 21 ++ .../FieldWidget/DataDictionaryWidget.php | 99 +++++++- .../custom-index-fields-table.html.twig | 28 +++ .../templates/custom-index-table.html.twig | 35 +++ 16 files changed, 1056 insertions(+), 70 deletions(-) create mode 100644 modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php create mode 100644 modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php create mode 100644 modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php create mode 100644 modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php create mode 100644 modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php create mode 100644 modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php create mode 100644 modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php create mode 100644 modules/data_dictionary_widget/templates/custom-index-fields-table.html.twig create mode 100644 modules/data_dictionary_widget/templates/custom-index-table.html.twig diff --git a/modules/data_dictionary_widget/data_dictionary_widget.module b/modules/data_dictionary_widget/data_dictionary_widget.module index f6a6f7a64a..7f127518eb 100644 --- a/modules/data_dictionary_widget/data_dictionary_widget.module +++ b/modules/data_dictionary_widget/data_dictionary_widget.module @@ -25,6 +25,22 @@ function data_dictionary_widget_theme($existing, $type, $theme, $path) { ], 'template' => 'custom-table', ], + 'custom_index_fields_table' => [ + 'variables' => [ + 'header' => [], + 'rows' => [], + 'attributes' => [], + ], + 'template' => 'custom-index-fields-table', + ], + 'custom_index_table' => [ + 'variables' => [ + 'header' => [], + 'rows' => [], + 'attributes' => [], + ], + 'template' => 'custom-index-table', + ], ]; } diff --git a/modules/data_dictionary_widget/src/Fields/FieldCallbacks.php b/modules/data_dictionary_widget/src/Fields/FieldCallbacks.php index a22bf5f1fe..279140365a 100644 --- a/modules/data_dictionary_widget/src/Fields/FieldCallbacks.php +++ b/modules/data_dictionary_widget/src/Fields/FieldCallbacks.php @@ -39,37 +39,58 @@ public static function updateFormatOptions(array &$form, FormStateInterface $for * Submit callback for the Edit button. */ public static function editSubformCallback(array &$form, FormStateInterface $form_state) { - $current_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"]; + // 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"]; + // 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']); - $currently_modifying = $form_state->get('fields_being_modified') != NULL ? $form_state->get('fields_being_modified') : []; - + // Get the fields we're currently modifying + $dictionary_fields_being_modified = $form_state->get('dictionary_fields_being_modified') != NULL ? $form_state->get('dictionary_fields_being_modified') : []; + $index_fields_being_modified = $form_state->get('index_fields_being_modified') != NULL ? $form_state->get('index_fields_being_modified') : []; + // If the op (trigger) containes abort, + // we're canceling the field we're currently modifying so unset it. if (str_contains($form_state->getTriggeringElement()['#op'], 'abort')) { - unset($currently_modifying[$op_index[1]]); + unset($dictionary_fields_being_modified[$op_index[1]]); } - + // If the op (trigger) contains delete, + // we're deleting the field we're editing so... if (str_contains($form_state->getTriggeringElement()['#op'], 'delete')) { - unset($currently_modifying[$op_index[1]]); - unset($current_fields[$op_index[1]]); + // Unset it from being currently modified. + unset($dictionary_fields_being_modified[$op_index[1]]); + // Remove the respective field/data from the form. + unset($current_dictionary_fields[$op_index[1]]); } - + // If the op (trigger) contains update, + // We're saving the field we're editing so... if (str_contains($form_state->getTriggeringElement()['#op'], 'update')) { - unset($currently_modifying[$op_index[1]]); - unset($current_fields[$op_index[1]]); - $current_fields[$op_index[1]] = FieldValues::updateValues($op_index[1], $form_state->getUserInput(), $current_fields); - ksort($current_fields); + // Unset the respective currently modifying field. + unset($dictionary_fields_being_modified[$op_index[1]]); + // Unset the respective field/data from the form. + unset($current_dictionary_fields[$op_index[1]]); + // Update the respective current field data with our new input data. + $current_dictionary_fields[$op_index[1]] = FieldValues::updateValues($op_index[1], $form_state->getUserInput(), $current_dictionary_fields); + // Sort the current fields data. + ksort($current_dictionary_fields); } - + // If the op (trigger) contains edit + // We're editing a specific field so... if (str_contains($form_state->getTriggeringElement()['#op'], 'edit')) { - $currently_modifying[$op_index[1]] = $current_fields[$op_index[1]]; + // Set the field we're modifying to that field. + $dictionary_fields_being_modified[$op_index[1]] = $current_dictionary_fields[$op_index[1]]; } - - // Re-index the current_fields array. - if ($current_fields) { - $current_fields = array_values($current_fields); + // Reindex the current_dictionary_fields array. + if ($current_dictionary_fields) { + $current_dictionary_fields = array_values($current_dictionary_fields); } - - $form_state->set('fields_being_modified', $currently_modifying); - $form_state->set('current_fields', $current_fields); + // Let's retain the fields that are being modified. + $form_state->set('index_fields_being_modified', $index_fields_being_modified); + $form_state->set('dictionary_fields_being_modified', $dictionary_fields_being_modified); + // 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_fields', $current_index_fields ); + // Let's rebuild the form. $form_state->setRebuild(); } @@ -79,10 +100,12 @@ 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"]; $form_state->set('add_new_field', ''); - $current_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"]; - if ($current_fields) { - $form_state->set('current_fields', $current_fields); + // $fields_being_added = $form_state->set('fields_being_added', ''); + $current_dictionary_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"]; + if ($current_dictionary_fields) { + $form_state->set('current_dictionary_fields', $current_dictionary_fields); } if ($op === 'cancel') { @@ -95,11 +118,11 @@ public static function addSubformCallback(array &$form, FormStateInterface $form } if ($op === 'add') { - $form_state->set('new_fields', $form_state->getUserInput()); + $form_state->set('new_dictionary_fields', $form_state->getUserInput()); $form_state->set('add', TRUE); $form_state->set('cancel', FALSE); } - + $form_state->set('current_index_fields', $current_index_fields); $form_state->setRebuild(); } diff --git a/modules/data_dictionary_widget/src/Fields/FieldCreation.php b/modules/data_dictionary_widget/src/Fields/FieldCreation.php index 14113aa497..ff3e11486e 100644 --- a/modules/data_dictionary_widget/src/Fields/FieldCreation.php +++ b/modules/data_dictionary_widget/src/Fields/FieldCreation.php @@ -12,7 +12,7 @@ class FieldCreation { /** * Create basic widget. */ - public static function createGeneralFields($element, $field_json_metadata, $current_fields, $form_state) { + public static function createGeneralFields($element, $field_json_metadata, $current_dictionary_fields, $form_state) { $element['identifier'] = self::createField('identifier', $field_json_metadata, $form_state); @@ -25,8 +25,7 @@ public static function createGeneralFields($element, $field_json_metadata, $curr '#suffix' => '', '#markup' => t('
A data dictionary for this resource, compliant with the Table Schema specification.
'), ]; - - $element['dictionary_fields']['current_fields'] = $current_fields; + $element['dictionary_fields']['current_dictionary_fields'] = $current_dictionary_fields; if (isset($field_json_metadata['data']['indexes'])) { $element['indexes'] = self::createField('indexes', $field_json_metadata, $form_state); @@ -84,13 +83,13 @@ protected static function createField(string $field, array $field_json_metadata, /** * Create data dictionary data rows. */ - public static function createDictionaryDataRows($current_fields, $data_results, $form_state) { + public static function createDictionaryDataRows($current_dictionary_fields, $data_results, $form_state) { return [ - '#access' => ((bool) $current_fields || (bool) $data_results), + '#access' => ((bool) $current_dictionary_fields || (bool) $data_results), '#type' => 'table', '#header' => ['NAME', 'TITLE', 'DETAILS'], - '#rows' => $form_state->get('cancel') ? $current_fields : ($data_results ?? []), + '#rows' => $form_state->get('cancel') ? $current_dictionary_fields : ($data_results ?? []), '#tree' => TRUE, '#theme' => 'custom_table', ]; diff --git a/modules/data_dictionary_widget/src/Fields/FieldEditCreation.php b/modules/data_dictionary_widget/src/Fields/FieldEditCreation.php index 78fee533bc..c6191d7da0 100644 --- a/modules/data_dictionary_widget/src/Fields/FieldEditCreation.php +++ b/modules/data_dictionary_widget/src/Fields/FieldEditCreation.php @@ -10,12 +10,12 @@ class FieldEditCreation { /** * Create edit fields for Data Dictionary Widget. */ - public static function editFields($key, $current_fields) { + public static function editFields($key, $current_dictionary_fields) { $edit_fields['name'] = [ '#name' => 'field_json_metadata[0][dictionary_fields][data][' . $key . '][field_collection][name]', '#type' => 'textfield', - '#value' => $current_fields[$key]['name'], + '#value' => $current_dictionary_fields[$key]['name'], '#required' => TRUE, '#title' => 'Name', '#description' => t('Machine name of the field/column in the data table.'), @@ -23,15 +23,15 @@ public static function editFields($key, $current_fields) { $edit_fields['title'] = [ '#name' => 'field_json_metadata[0][dictionary_fields][data][' . $key . '][field_collection][title]', '#type' => 'textfield', - '#value' => $current_fields[$key]['title'], + '#value' => $current_dictionary_fields[$key]['title'], '#required' => TRUE, '#title' => 'Title', '#description' => t('A human-readable title.'), ]; - $edit_fields['type'] = self::createType($key, $current_fields); - $edit_fields['format'] = self::createFormat($key, $current_fields); - $edit_fields['format_other'] = self::createFormatOther($key, $current_fields); - $edit_fields['description'] = self::createDescriptionField($key, $current_fields); + $edit_fields['type'] = self::createType($key, $current_dictionary_fields); + $edit_fields['format'] = self::createFormat($key, $current_dictionary_fields); + $edit_fields['format_other'] = self::createFormatOther($key, $current_dictionary_fields); + $edit_fields['description'] = self::createDescriptionField($key, $current_dictionary_fields); $edit_fields['update_field']['actions'] = self::createActionFields($key); return $edit_fields; @@ -41,14 +41,14 @@ public static function editFields($key, $current_fields) { /** * Create Type field. */ - private static function createType($key, $current_fields) { + private static function createType($key, $current_dictionary_fields) { return [ '#name' => 'field_json_metadata[0][dictionary_fields][data][' . $key . '][field_collection][type]', '#type' => 'select', '#required' => TRUE, '#title' => 'Data type', '#default_value' => 'string', - '#value' => $current_fields[$key]['type'], + '#value' => $current_dictionary_fields[$key]['type'], '#op' => 'format_' . $key, '#options' => FieldOperations::setTypeOptions(), '#ajax' => [ @@ -62,16 +62,16 @@ private static function createType($key, $current_fields) { /** * Create Format field. */ - private static function createFormat($key, $current_fields) { - $format_options = FieldOperations::generateFormats($current_fields[$key]['type'], "options"); - $value = in_array($current_fields[$key]['format'], $format_options, TRUE) ? $current_fields[$key]['format'] : 'other'; + private static function createFormat($key, $current_dictionary_fields) { + $format_options = FieldOperations::setFormatOptions($current_dictionary_fields[$key]['type']); + $value = in_array($current_dictionary_fields[$key]['format'], $format_options) ? $current_dictionary_fields[$key]['format'] : 'other'; return [ '#name' => 'field_json_metadata[0][dictionary_fields][data][' . $key . '][field_collection][format]', '#type' => 'select', '#required' => TRUE, '#title' => 'Format', '#default_value' => 'default', - '#description' => FieldOperations::generateFormats($current_fields[$key]['type'], "description"), + '#description' => FieldOperations::generateFormatDescription($current_dictionary_fields[$key]['type']), '#value' => $value, '#prefix' => '
', '#suffix' => '
', @@ -83,9 +83,9 @@ private static function createFormat($key, $current_fields) { /** * Create Format Other field. */ - private static function createFormatOther($key, $current_fields) { - $format_options = FieldOperations::generateFormats($current_fields[$key]['type'], "options"); - $value = !in_array($current_fields[$key]['format'], $format_options) ? $current_fields[$key]['format'] : NULL; + private static function createFormatOther($key, $current_dictionary_fields) { + $format_options = FieldOperations::setFormatOptions($current_dictionary_fields[$key]['type']); + $value = !in_array($current_dictionary_fields[$key]['format'], $format_options) ? $current_dictionary_fields[$key]['format'] : NULL; return [ '#name' => 'field_json_metadata[0][dictionary_fields][data][' . $key . '][field_collection][format_other]', @@ -119,11 +119,11 @@ private static function createActionFields($key) { /** * Create Description field. */ - private static function createDescriptionField($key, $current_fields) { + private static function createDescriptionField($key, $current_dictionary_fields) { return [ '#name' => 'field_json_metadata[0][dictionary_fields][data][' . $key . '][field_collection][description]', '#type' => 'textfield', - '#value' => $current_fields[$key]['description'], + '#value' => $current_dictionary_fields[$key]['description'], '#required' => TRUE, '#title' => 'Description', '#description' => t('Information about the field data.'), diff --git a/modules/data_dictionary_widget/src/Fields/FieldOperations.php b/modules/data_dictionary_widget/src/Fields/FieldOperations.php index f084842636..6ec0d9d714 100644 --- a/modules/data_dictionary_widget/src/Fields/FieldOperations.php +++ b/modules/data_dictionary_widget/src/Fields/FieldOperations.php @@ -112,9 +112,9 @@ public static function generateFormats($dataType, $property) { /** * Cleaning the data up. */ - public static function processDataResults($data_results, $current_fields, $field_values, $op) { - if (isset($current_fields)) { - $data_results = $current_fields; + public static function processDataResults($data_results, $current_dictionary_fields, $field_values, $op) { + if (isset($current_dictionary_fields)) { + $data_results = $current_dictionary_fields; } if (isset($field_values["field_json_metadata"][0]["dictionary_fields"]["field_collection"])) { @@ -134,7 +134,7 @@ public static function processDataResults($data_results, $current_fields, $field } if (isset($data_pre) && $op === "add") { - $data_results = isset($current_fields) ? array_merge($current_fields, $data_pre) : $data_pre; + $data_results = isset($current_dictionary_fields) ? array_merge($current_dictionary_fields, $data_pre) : $data_pre; } return $data_results; @@ -171,9 +171,9 @@ public static function setTypeOptions() { /** * Return true if field is being edited. */ - public static function checkEditingField($key, $op_index, $fields_being_modified) { + public static function checkEditingField($key, $op_index, $dictionary_fields_being_modified) { $action_list = FieldOperations::editActions(); - if (isset($op_index[0]) && in_array($op_index[0], $action_list) && array_key_exists($key, $fields_being_modified)) { + if (isset($op_index[0]) && in_array($op_index[0], $action_list) && array_key_exists($key, $dictionary_fields_being_modified)) { return TRUE; } else { @@ -210,12 +210,12 @@ public static function setAddFormState($add_new_field, $element) { /** * Create edit and update fields where needed. */ - public static function createDictionaryFieldOptions($op_index, $data_results, $fields_being_modified, $element) { - $current_fields = $element['current_fields']; + public static function createDictionaryFieldOptions($op_index, $data_results, $dictionary_fields_being_modified, $element) { + $current_dictionary_fields = $element['current_dictionary_fields']; // Creating ajax buttons/fields to be placed in correct location later. foreach ($data_results as $key => $data) { - if (self::checkEditingField($key, $op_index, $fields_being_modified)) { - $element['edit_fields'][$key] = FieldEditCreation::editFields($key, $current_fields, $fields_being_modified); + if (self::checkEditingField($key, $op_index, $dictionary_fields_being_modified)) { + $element['edit_fields'][$key] = FieldEditCreation::editFields($key, $current_dictionary_fields, $dictionary_fields_being_modified); } else { $element['edit_buttons'][$key]['edit_button'] = FieldButtons::editButtons($key); diff --git a/modules/data_dictionary_widget/src/Fields/FieldValues.php b/modules/data_dictionary_widget/src/Fields/FieldValues.php index f3207c12de..1122733966 100644 --- a/modules/data_dictionary_widget/src/Fields/FieldValues.php +++ b/modules/data_dictionary_widget/src/Fields/FieldValues.php @@ -10,7 +10,7 @@ class FieldValues { /** * Return updated field values after edit. */ - public static function updateValues($field_index, $update_values, $current_fields) { + public static function updateValues($field_index, $update_values, $current_dictionary_fields) { $format = $update_values['field_json_metadata'][0]['dictionary_fields']['data'][$field_index]['field_collection']['format']; $format_other = $update_values['field_json_metadata'][0]['dictionary_fields']['data'][$field_index]['field_collection']['format_other']; $name = $update_values['field_json_metadata'][0]['dictionary_fields']['data'][$field_index]['field_collection']['name']; diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php new file mode 100644 index 0000000000..efab72ff3a --- /dev/null +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php @@ -0,0 +1,112 @@ + 'fieldset', + '#title' => t('Index'), + '#open' => TRUE, + '#prefix' => '
', + '#suffix' => '
', + ]; + + //$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]', + '#description' => t('Description of index purpose or functionality.'), + '#type' => 'textfield', + '#title' => 'Name', + ]; + + $add_index['group']['indexes']['index_type'] = [ + '#name' => 'field_json_metadata[0][index][field_collection][group][index_type]', + '#type' => 'select', + '#description' => t('Index type.'), + //'#required' => TRUE, + '#title' => 'Index Type', + '#default_value' => 'index', + '#op' => 'index_type', + '#options' => [ + 'string' => t('index'), + 'date' => t('fulltext'), + ], + ]; + + $add_index['group']['indexes']['index_fields'] = [ + '#type' => 'fieldset', + '#title' => t('Fields'), + '#prefix' => '
', + '#suffix' => '
', + '#markup' => t('
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']['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); + + return $add_index; + } + + /** + * Create add fields for Data Dictionary Widget. + */ + public static function addIndexFields() { + $add_index_fields['#access'] = FALSE; + $add_index_fields['group'] = [ + '#type' => 'fieldset', + '#title' => t('Add new field'), + '#prefix' => '
', + '#suffix' => '
', + ]; + + // $add_index_fields['group']['indexes']['index_fields'] = [ + // '#prefix' => '
', + // '#suffix' => '
', + // ]; + + $add_index_fields['group']['indexes']['index_fields']['name'] = [ + '#name' => 'field_json_metadata[0][index_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(); + + return $add_index_fields; + } + + /** + * Create Description field. + */ + private static function createIndexFieldLengthField() { + return [ + '#name' => 'field_json_metadata[0][index_fields][field_collection][group][length]', + '#type' => 'number', + '#title' => 'Length', + ]; + } + + /** + * Create Action buttons. + */ + private static function createIndexActionFields() { + return [ + '#type' => 'actions', + 'save_index_settings' => IndexFieldButtons::submitIndexFieldButton('add', NULL), + 'cancel_index_settings' => IndexFieldButtons::cancelIndexFieldButton('cancel', NULL), + ]; + } +} \ No newline at end of file diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php new file mode 100644 index 0000000000..4fe4fba5b7 --- /dev/null +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php @@ -0,0 +1,233 @@ + 'submit', + '#value' => 'Add field', + '#access' => TRUE, + '#op' => 'add_new_index_field', + '#submit' => [ + [ + '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + 'indexAddSubformCallback', + ], + ], + '#ajax' => [ + 'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexformAjax', + 'wrapper' => 'field-json-metadata-dictionary-index-fields', + 'effect' => 'fade', + ], + '#limit_validation_errors' => [], + ]; + } + + /** + * Returns the add index button. + */ + public static function addIndexButton() { + return [ + '#type' => 'submit', + '#value' => 'Add index', + '#access' => TRUE, + '#op' => 'add_new_index', + '#submit' => [ + [ + '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + 'indexAddCallback', + ], + ], + '#ajax' => [ + 'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexformAjax', + 'wrapper' => 'field-json-metadata-dictionary-indexes', + 'effect' => 'fade', + ], + '#limit_validation_errors' => [], + ]; + } + + /** + * Returns the edit buttons. + */ + public static function editIndexButtons($indexKey) { + return [ + '#type' => 'image_button', + '#name' => 'edit_index_' . $indexKey, + '#id' => 'edit_index_' . $indexKey, + '#access' => TRUE, + '#op' => 'edit_' . $indexKey, + '#src' => 'core/misc/icons/787878/cog.svg', + '#attributes' => [ + 'class' => ['index-field-plugin-settings-edit'], + 'alt' => t('Edit index'), + ], + '#submit' => [ + [ + '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + 'indexEditSubformCallback', + ], + ], + '#ajax' => [ + 'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexformAjax', + 'wrapper' => 'field-json-metadata-dictionary-index-fields', + 'effect' => 'fade', + ], + '#limit_validation_errors' => [], + ]; + } + + /** + * Create Submit buttons. + */ + public static function submitIndexFieldButton($location, $indexKey) { + $callbackClass = $location == 'edit' ? 'indexEditSubformCallback' : 'indexAddSubformCallback'; + $op = !empty($indexKey) ? 'update_' . $indexKey : 'add_index_field'; + $value = $location == 'edit' ? 'Save' : 'Add '; + $edit_index_button = [ + '#type' => 'submit', + '#value' => $value, + '#op' => $op, + '#submit' => [ + [ + '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + $callbackClass, + ], + ], + '#ajax' => [ + 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexformAjax', + 'wrapper' => 'field-json-metadata-dictionary-index-fields', + 'effect' => 'fade', + ], + '#limit_validation_errors' => [], + ]; + + if ($location == 'edit') { + $edit_index_button['#name'] = 'update_' . $indexKey; + } + return $edit_index_button; + } + + /** + * Create Submit buttons. + */ + public static function submitIndexButton($location, $indexKey) { + $callbackClass = $location == 'edit' ? 'indexEditCallback' : 'indexAddCallback'; + $op = !empty($indexKey) ? 'update_' . $indexKey : 'add_index'; + $value = $location == 'edit' ? 'Save' : 'Submit Index'; + $edit_index_button = [ + '#type' => 'submit', + '#value' => $value, + '#op' => $op, + '#submit' => [ + [ + '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + $callbackClass, + ], + ], + '#ajax' => [ + 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexformAjax', + 'wrapper' => 'field-json-metadata-dictionary-index', + 'effect' => 'fade', + ], + '#limit_validation_errors' => [], + ]; + + if ($location == 'edit') { + $edit_index_button['#name'] = 'update_' . $indexKey; + } + return $edit_index_button; + } + + /** + * Create Cancel button. + */ + public static function cancelIndexFieldButton($location, $indexKey) { + $callbackClass = $location == 'edit' ? 'indexEditSubformCallback' : 'indexAddSubformCallback'; + $op = $location == 'edit' && $indexKey ? 'abort_' . $indexKey : 'cancel_index_field'; + $cancel_index_button = [ + '#type' => 'submit', + '#value' => t('Cancel'), + '#op' => $op, + '#submit' => [ + [ + '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + $callbackClass, + ], + ], + '#ajax' => [ + 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexformAjax', + 'wrapper' => 'field-json-metadata-dictionary-index-fields', + 'effect' => 'fade', + ], + '#limit_validation_errors' => [], + ]; + + if ($location == 'edit') { + $cancel_index_button['#name'] = 'cancel_update_' . $indexKey; + } + return $cancel_index_button; + } + + /** + * Create Cancel button. + */ + public static function cancelIndexButton($location, $indexKey) { + $callbackClass = $location == 'edit' ? 'indexEditSubformCallback' : 'indexAddSubformCallback'; + $op = $location == 'edit' && $indexKey ? 'abort_' . $indexKey : 'cancel_index'; + $cancel_index_button = [ + '#type' => 'submit', + '#value' => t('Cancel Index'), + '#op' => $op, + '#submit' => [ + [ + '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + $callbackClass, + ], + ], + '#ajax' => [ + 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexformAjax', + 'wrapper' => 'field-json-metadata-dictionary-index', + 'effect' => 'fade', + ], + '#limit_validation_errors' => [], + ]; + + if ($location == 'edit') { + $cancel_index_button['#name'] = 'cancel_update_' . $indexKey; + } + return $cancel_index_button; + } + + /** + * Create Delete button. + */ + public static function deleteIndexButton($indexKey) { + return [ + '#type' => 'submit', + '#name' => 'index_delete_' . $indexKey, + '#value' => t('Delete index field'), + '#op' => 'delete_' . $indexKey, + '#submit' => [ + [ + '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + 'indexEditSubformCallback', + ], + ], + '#ajax' => [ + 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexformAjax', + 'wrapper' => 'field-json-metadata-dictionary-index-fields', + 'effect' => 'fade', + ], + '#limit_validation_errors' => [], + ]; + } +} \ No newline at end of file diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php new file mode 100644 index 0000000000..0d938d38cd --- /dev/null +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php @@ -0,0 +1,134 @@ +getTriggeringElement(); + $op = $trigger['#op']; + $current_dictionary_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"]; + $form_state->set('add_new_index_field', ''); + // $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"]; + + if ($current_index_fields) { + $form_state->set('current_index_fields', $current_index_fields); + } + + if ($op === 'cancel_index_field') { + $form_state->set('cancel_index_field', TRUE); + } + + if ($op === 'add_new_index_field') { + $add_index_fields = IndexFieldAddCreation::addIndexFields(); + $form_state->set('add_new_index_field', $add_index_fields); + } + + if ($op === 'add_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('current_dictionary_fields', $current_dictionary_fields); + $form_state->set('current_index_fields', $current_index_fields); + $form_state->setRebuild(); + } + + /** + * Submit callback for the Index Add button. + */ + public static function indexAddCallback(array &$form, FormStateInterface $form_state) { + $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', ''); + // $fields_being_added = $form_state->set('fields_being_added', ''); + $current_index = $form["field_json_metadata"]["widget"][0]["indexes"]["data"]["#rows"]; + if ($current_index) { + $form_state->set('current_index', $current_index); + } + + if ($op === 'cancel_index') { + $form_state->set('cancel_index', TRUE); + } + + if ($op === 'add_new_index') { + $add_new_index = IndexFieldAddCreation::addIndex(); + $form_state->set('add_new_index', $add_new_index); + } + + if ($op === 'add_index') { + $form_state->set('new_index', $form_state->getUserInput()); + $form_state->set('add', 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->setRebuild(); + } + + /** + * Submit callback for the Index Edit button. + */ + 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_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 (str_contains($op, 'abort')) { + unset($currently_modifying_index_fields[$op_index[4]]); + } + + if (str_contains($op, 'delete')) { + unset($currently_modifying_index_fields[$op_index[4]]); + unset($current_index_fields[$op_index[4]]); + } + + if (str_contains($op, 'update')) { + $update_values = $form_state->getUserInput(); + unset($currently_modifying_index_fields[$op_index[4]]); + unset($current_index_fields[$op_index[4]]); + $current_index_fields[$op_index[4]] = IndexFieldValues::updateIndexFieldValues($op_index[4], $update_values, $current_index_fields ); + ksort($current_index_fields ); + } + + if (str_contains($op, 'edit')) { + $currently_modifying_index_fields[$op_index[4]] = $current_index_fields[$op_index[4]]; + } + + $form_state->set('dictionary_fields_being_modified', $currently_modifying); + $form_state->set('index_fields_being_modified', $currently_modifying_index_fields); + $form_state->set('current_index_fields', $current_index_fields ); + $form_state->set('current_dictionary_fields', $current_dictionary_fields ); + $form_state->setRebuild(); + } + + /** + * Ajax callback. + */ + public static function subIndexformAjax(array &$form, FormStateInterface $form_state) { + return $form["field_json_metadata"]["widget"][0]["indexes"]["index_fields"]; + } + + /** + * Ajax callback. + */ + public static function indexformAjax(array &$form, FormStateInterface $form_state) { + return $form["field_json_metadata"]["widget"][0]["indexes"]; + } +} \ No newline at end of file diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php new file mode 100644 index 0000000000..59f9b05a50 --- /dev/null +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php @@ -0,0 +1,79 @@ + TRUE, + '#type' => 'fieldset', + '#title' => t('Fields'), + '#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; + + return $element; + } + + /** + * Create basic widget. + */ + public static function createGeneralIndex($element, $field_json_metadata, $current_index, $index_fields_being_modified) { + + $element['indexes'] = [ + '#type' => 'fieldset', + '#title' => t('Data Dictionary Indexes'), + '#prefix' => '
', + '#suffix' => '
', + '#markup' => t('
One or more indexes.
'), + ]; + + $element['indexes']['current_index'] = $current_index; + + return $element; + } + + /** + * Create data index data rows. + */ + public static function createIndexFieldsDataRows($current_index_fields, $index_data_results, $form_state) { + + return [ + '#access' => ((bool) $current_index_fields || (bool) $index_data_results), + '#type' => 'table', + '#header' => ['NAME', 'LENGTH'], + '#prefix' => '
', + '#suffix' => '
', + '#rows' => $form_state->get('cancel_index_field') ? $current_index_fields : ($index_data_results ?? []), + '#tree' => TRUE, + '#theme' => 'custom_index_fields_table', + ]; + + } + + /** + * Create data index data rows. + */ + public static function createIndexDataRows($current_indexes, $index_data_results, $form_state) { + + return [ + '#access' => ((bool) $current_indexes || (bool) $index_data_results), + '#type' => 'table', + '#header' => ['INDEX'], + '#rows' => $form_state->get('cancel_index') ? $current_indexes : ($index_data_results ?? []), + '#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 new file mode 100644 index 0000000000..ae7f34a78e --- /dev/null +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php @@ -0,0 +1,46 @@ + 'field_json_metadata[0][index_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]', + '#type' => 'number', + '#value' => $current_index_fields[$indexKeyExplode[3]]['length'], + '#title' => 'Length', + ]; + + + $edit_index_fields['update_index_field']['actions'] = self::createIndexActionFields($indexKey); + return $edit_index_fields; + + } + + /** + * Create Action buttons. + */ + private static function createIndexActionFields($indexKey) { + return [ + '#type' => 'actions', + 'save_update' => IndexFieldButtons::submitIndexFieldButton('edit', $indexKey), + 'cancel_updates' => IndexFieldButtons::cancelIndexFieldButton('edit', $indexKey), + 'delete_field' => IndexFieldButtons::deleteIndexButton($indexKey), + ]; + } + +} \ No newline at end of file diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php new file mode 100644 index 0000000000..9df6f447cc --- /dev/null +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php @@ -0,0 +1,179 @@ + $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']); + } + + } + + return $dictionaryIndexFields; + } + + /** + * Setting ajax elements. + */ + 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; + // 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']); + } + + } + + return $dictionaryIndexes; + } + + /** + * Cleaning the data up. + */ + public static function processIndexDataResults($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"]; + + $data_index_fields_pre = [ + [ + "name" => $index_field_group["name"], + "length" => (int)$index_field_group["length"], + ], + ]; + } + + if (isset($data_index_fields_pre) && $op === "add_index_field") { + $index_data_results = isset($current_index_fields) ? array_merge($current_index_fields, $data_index_fields_pre) : $data_index_fields_pre; + } + + return $index_data_results; + } + + /** + * Return acceptable edit actions. + */ + public static function editIndexActions() { + return [ + 'format', + 'edit', + 'update', + 'abort', + 'delete', + ]; + } + + /** + * Set the elements associated with adding a new field. + */ + public static function setAddIndexFieldFormState($add_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['identifier']['#required'] = FALSE; + $element['title']['#required'] = FALSE; + } + return $element; + } + + /** + * Set the elements associated with adding a new field. + */ + public static function setAddIndexFormState($add_new_index, $element) { + if ($add_new_index) { + + $element['indexes']['field_collection'] = $add_new_index; + $element['indexes']['field_collection']['#access'] = TRUE; + $element['indexes']['add_row_button']['#access'] = FALSE; + $element['identifier']['#required'] = FALSE; + $element['title']['#required'] = FALSE; + } + return $element; + } + + /** + * Create edit and update fields where needed. + */ + public static function createDictionaryIndexFieldOptions($op_index, $index_data_results, $index_fields_being_modified, $element) { + $current_index_fields = $element['current_index_fields'] ?? NULL; + // 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); + } + else { + $element['edit_index_buttons']['index_field_key_' . $indexKey]['edit_index_button'] = IndexFieldButtons::editIndexButtons('index_field_key_' . $indexKey); + } + } + $element['add_row_button'] = IndexFieldButtons::addIndexFieldButton(); + + return $element; + } + + /** + * Create edit and update fields where needed. + */ + public static function createDictionaryIndexOptions($op_index, $index_data_results, $index_fields_being_modified, $element) { + $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); + } + else { + $element['edit_index_buttons']['index_field_key_' . $indexKey]['edit_index_button'] = IndexFieldButtons::editIndexButtons('index_field_key_' . $indexKey); + } + } + $element['add_row_button'] = IndexFieldButtons::addIndexButton(); + + return $element; + } + + /** + * Return true if field is being edited. + */ + public static function checkIndexEditingField($indexKey, $op_index, $index_fields_being_modified) { + $action_list = IndexFieldOperations::editIndexActions(); + $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; + } + else { + return FALSE; + } + } +} \ No newline at end of file diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php new file mode 100644 index 0000000000..2f8e40291d --- /dev/null +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php @@ -0,0 +1,21 @@ + $name, + 'length' => $length, + ]; + } +} \ No newline at end of file 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 ddf02d2771..fda14d71e8 100644 --- a/modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php +++ b/modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php @@ -10,6 +10,8 @@ use Drupal\data_dictionary_widget\Fields\FieldCreation; use Drupal\data_dictionary_widget\Fields\FieldOperations; use Drupal\Core\Entity\EntityFormInterface; +use Drupal\data_dictionary_widget\Indexes\IndexFieldCreation; +use Drupal\data_dictionary_widget\Indexes\IndexFieldOperations; /** * A data-dictionary widget. @@ -28,29 +30,63 @@ class DataDictionaryWidget extends WidgetBase implements TrustedCallbackInterfac * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { - $field_values = $form_state->get("new_fields"); - $current_fields = $form_state->get('current_fields'); + $field_values = $form_state->get("new_dictionary_fields"); + $index_values = $form_state->get('new_index'); + $index_field_values = $form_state->get("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; + $index_fields_being_modified = $form_state->get("index_fields_being_modified") ?? NULL; + $op = $form_state->getTriggeringElement()['#op'] ?? NULL; $field_json_metadata = !empty($items[0]->value) ? json_decode($items[0]->value, TRUE) : []; $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"] : []; // 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_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); + $element = FieldCreation::createGeneralFields($element, $field_json_metadata, $current_fields, $form_state); + $element = IndexFieldCreation::createGeneralIndex($element, $field_json_metadata, $current_indexes, $form_state); + + if ($index_field_values || $current_index_fields) { + $element = IndexFieldCreation::createGeneralIndexFields($element, $field_json_metadata, $current_index_fields, $form_state->get('add_new_index'), $form_state); + } $element['dictionary_fields']['#pre_render'] = [ [$this, 'preRenderForm'], ]; + $element['indexes']['#pre_render'] = [ + [$this, 'preRenderIndexForm'], + ]; + + $element['indexes']['index_fields']['#pre_render'] = [ + [$this, 'preRenderIndexFieldForm'], + ]; + $element['dictionary_fields']['data'] = FieldCreation::createDictionaryDataRows($current_fields, $data_results, $form_state); + $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); // 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']); $element['dictionary_fields']['add_row_button']['#access'] = $fields_being_modified == NULL ? TRUE : FALSE; + // 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; + $form_object = $form_state->getFormObject(); if (!($form_object instanceof EntityFormInterface)) { return; @@ -61,6 +97,8 @@ 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); return $element; } @@ -70,10 +108,13 @@ 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 = isset($values[0]["indexes"]) ? json_decode($values[0]["indexes"]) : NULL; + $field_collection = $values[0]['dictionary_fields']["field_collection"]["group"] ?? []; - $indexes = isset($values[0]["indexes"]) ? json_decode($values[0]["indexes"]) : NULL; + $indexes_collection = $values[0]["indexes"]["index_fields"]["field_collection"]["group"] ?? []; - $data_results = !empty($field_collection) ? [ + $fields_input = !empty($field_collection) ? [ [ "name" => $field_collection["name"], "title" => $field_collection["title"], @@ -83,18 +124,40 @@ public function massageFormValues(array $values, array $form, FormStateInterface ], ] : []; - $updated = array_merge($current_fields ?? [], $data_results); + $index_inputs = !empty($indexes_collection) ? [ + [ + "name" => $indexes_collection["name"], + "length" => (int)$indexes_collection["length"], + ], + ] : []; + + if (isset($fields_input)) { + $fields = array_merge($current_fields ?? [], $fields_input); + } + else { + $fields = $current_fields ?? []; + } + + + //$fields = array_merge($current_fields ?? [], $fields_input); + $indexes = array_merge($current_indexes ?? [], $index_inputs); $json_data = [ 'identifier' => $values[0]['identifier'] ?? '', 'data' => [ 'title' => $values[0]['title'] ?? '', - 'fields' => $updated, - 'indexes' => $indexes ?? [], + 'fields' => $fields, + 'indexes' => [ + [ + 'fields' => $indexes + ] + ], ], ]; - return json_encode($json_data); + $test = json_encode($json_data); + + return $test; } /** @@ -106,11 +169,29 @@ public function preRenderForm(array $dictionaryFields) { return FieldOperations::setAjaxElements($dictionaryFields); } + /** + * Prerender callback for the index form. + * + * Moves the buttons into the table. + */ + public function preRenderIndexFieldForm(array $dictionaryIndexFields) { + return IndexFieldOperations::setIndexFieldsAjaxElements($dictionaryIndexFields); + } + + /** + * Prerender callback for the index form. + * + * Moves the buttons into the table. + */ + public function preRenderIndexForm(array $dictionaryIndexes) { + return IndexFieldOperations::setIndexAjaxElements($dictionaryIndexes); + } + /** * {@inheritdoc} */ public static function trustedCallbacks() { - return ['preRenderForm']; + return ['preRenderForm', 'preRenderIndexFieldForm', 'preRenderIndexForm']; } } diff --git a/modules/data_dictionary_widget/templates/custom-index-fields-table.html.twig b/modules/data_dictionary_widget/templates/custom-index-fields-table.html.twig new file mode 100644 index 0000000000..1b401daf01 --- /dev/null +++ b/modules/data_dictionary_widget/templates/custom-index-fields-table.html.twig @@ -0,0 +1,28 @@ + + + + + + + + + + {% for row in rows %} + {% if row.field_collection %} + + + + {% elseif row.name %} + + + + + + {% endif %} + {% endfor %} + +
{{ header[0] }}{{ header[1] }}{{ header[2] }}
+ {{ row.field_collection }} +
{{ row.name }}{{ row.length }} + {{ row.edit_index_button }} +
diff --git a/modules/data_dictionary_widget/templates/custom-index-table.html.twig b/modules/data_dictionary_widget/templates/custom-index-table.html.twig new file mode 100644 index 0000000000..ed092b89f8 --- /dev/null +++ b/modules/data_dictionary_widget/templates/custom-index-table.html.twig @@ -0,0 +1,35 @@ + + + + + + + + + + {% for row in rows %} + {% if row.field_collection %} + + + + {% elseif row.name %} + + + + + + + {% endif %} + {% endfor %} + +
{{ header[0] }}{{ header[1] }}{{ header[2] }}
+ {{ row.field_collection }} +
{{ row.name }}{{ row.title }} +
Data Type: {{ row.type }}
+
Format: {{ row.format }}
+ {% if row.description %} +
Description: {{ row.description }}
+ {% endif %} +
+ {{ row.edit_button }} +
From 35f8e827829aff442beb6d6996447eb7628847fc Mon Sep 17 00:00:00 2001 From: Kaise Lafrai Date: Tue, 14 May 2024 13:35:41 -0400 Subject: [PATCH 02/10] 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 7f127518eb..894497652a 100644 --- a/modules/data_dictionary_widget/data_dictionary_widget.module +++ b/modules/data_dictionary_widget/data_dictionary_widget.module @@ -112,21 +112,44 @@ function data_dictionary_widget_form_alter(&$form, &$form_state, $form_id) { } $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 279140365a..94e074334a 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 %} From 3516bd656dacaa0910720f62f8e86f5483103fda Mon Sep 17 00:00:00 2001 From: Kaise Lafrai Date: Mon, 20 May 2024 14:17:47 -0400 Subject: [PATCH 03/10] Removed unused code, fixed index fields cancel button --- .../src/Fields/FieldCallbacks.php | 2 - .../src/Fields/FieldCreation.php | 2 - .../src/Fields/FieldOperations.php | 12 --- .../src/Indexes/IndexFieldAddCreation.php | 26 +++---- .../src/Indexes/IndexFieldButtons.php | 73 ++++++++++--------- .../src/Indexes/IndexFieldCallbacks.php | 37 ++++++---- .../src/Indexes/IndexFieldCreation.php | 55 ++++---------- .../src/Indexes/IndexFieldEditCreation.php | 1 - .../src/Indexes/IndexFieldOperations.php | 7 +- .../FieldWidget/DataDictionaryWidget.php | 45 ++---------- 10 files changed, 87 insertions(+), 173 deletions(-) diff --git a/modules/data_dictionary_widget/src/Fields/FieldCallbacks.php b/modules/data_dictionary_widget/src/Fields/FieldCallbacks.php index 94e074334a..1d608a7860 100644 --- a/modules/data_dictionary_widget/src/Fields/FieldCallbacks.php +++ b/modules/data_dictionary_widget/src/Fields/FieldCallbacks.php @@ -100,9 +100,7 @@ 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"]["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"]; diff --git a/modules/data_dictionary_widget/src/Fields/FieldCreation.php b/modules/data_dictionary_widget/src/Fields/FieldCreation.php index ff3e11486e..2805d44f11 100644 --- a/modules/data_dictionary_widget/src/Fields/FieldCreation.php +++ b/modules/data_dictionary_widget/src/Fields/FieldCreation.php @@ -13,9 +13,7 @@ class FieldCreation { * Create basic widget. */ public static function createGeneralFields($element, $field_json_metadata, $current_dictionary_fields, $form_state) { - $element['identifier'] = self::createField('identifier', $field_json_metadata, $form_state); - $element['title'] = self::createField('title', $field_json_metadata, $form_state); $element['dictionary_fields'] = [ diff --git a/modules/data_dictionary_widget/src/Fields/FieldOperations.php b/modules/data_dictionary_widget/src/Fields/FieldOperations.php index 6ec0d9d714..6b56165405 100644 --- a/modules/data_dictionary_widget/src/Fields/FieldOperations.php +++ b/modules/data_dictionary_widget/src/Fields/FieldOperations.php @@ -181,18 +181,6 @@ public static function checkEditingField($key, $op_index, $dictionary_fields_bei } } - /** - * Return true if field collection is present. - */ - public static function checkFieldCollection($data_pre, $op) { - if (isset($data_pre) && $op === "add") { - return TRUE; - } - else { - return FALSE; - } - } - /** * Set the elements associated with adding a new field. */ diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php index fdcc628315..886565e36f 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php @@ -20,12 +20,10 @@ public static function addIndex() { '#suffix' => '
', ]; - //$add_index['group']['indexes']['add_row_button'] = IndexFieldButtons::addIndexFieldButton(); $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', ]; @@ -47,15 +45,13 @@ public static function addIndex() { '#type' => 'fieldset', '#title' => t('Fields'), '#required' => TRUE, - '#prefix' => '
', + '#prefix' => '
', '#suffix' => '
', - '#markup' => t('
Test One or more fields included in index. Must be keys from the fields object.
'), + '#markup' => t('
One or more fields included in index. Must be keys from the fields object.
'), ]; $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'] = IndexFieldButtons::submitIndexButton('add_index', NULL); $add_index['group']['indexes']['cancel_index'] = IndexFieldButtons::cancelIndexButton('cancel_index', NULL); @@ -65,27 +61,25 @@ public static function addIndex() { /** * Create add fields for Data Dictionary Widget. */ - public static function addIndexFields() { + public static function addIndexFields($current_index_fields) { + $id = $current_index_fields ? "field-json-metadata-dictionary-index-fields-new" : "field-json-metadata-dictionary-index-fields"; $add_index_fields['#access'] = FALSE; $add_index_fields['group'] = [ '#type' => 'fieldset', '#title' => t('Add new field'), - '#prefix' => '
', + '#prefix' => "
", '#suffix' => '
', + '#markup' => t('
Add a single index field. Must be keys from the fields object.
'), ]; - // $add_index_fields['group']['indexes']['index_fields'] = [ - // '#prefix' => '
', - // '#suffix' => '
', - // ]; - $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']['fields']['length'] = self::createIndexFieldLengthField(); - $add_index_fields['group']['indexes']['fields']['actions'] = self::createIndexActionFields(); + $add_index_fields['group']['indexes']['fields']['actions'] = self::createIndexActionFields($id); return $add_index_fields; } @@ -104,11 +98,11 @@ private static function createIndexFieldLengthField() { /** * Create Action buttons. */ - private static function createIndexActionFields() { + private static function createIndexActionFields($id) { return [ '#type' => 'actions', 'save_index_settings' => IndexFieldButtons::submitIndexFieldButton('add', NULL), - 'cancel_index_settings' => IndexFieldButtons::cancelIndexFieldButton('cancel', NULL), + 'cancel_index_settings' => IndexFieldButtons::cancelIndexFieldButton('cancel', NULL, $id), ]; } } \ No newline at end of file diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php index 301d5cbf4a..be0f5cfe0a 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php @@ -23,8 +23,8 @@ public static function addIndexFieldButton() { ], ], '#ajax' => [ - 'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexformAjax', - 'wrapper' => 'field-json-metadata-dictionary-index-fields-new', + 'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexFormAjax', + 'wrapper' => 'field-json-metadata-dictionary-index-fields', 'effect' => 'fade', ], '#limit_validation_errors' => [], @@ -41,13 +41,13 @@ public static function addIndexButton() { '#access' => TRUE, '#op' => 'add_new_index', '#submit' => [ - [ - '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', - 'indexAddCallback', - ], + [ + '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + 'indexAddCallback', + ], ], '#ajax' => [ - 'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexformAjax', + 'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexFormAjax', 'wrapper' => 'field-json-metadata-dictionary-indexes', 'effect' => 'fade', ], @@ -77,7 +77,7 @@ public static function editIndexButtons($indexKey) { ], ], '#ajax' => [ - 'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexformAjax', + 'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexFormAjax', 'wrapper' => 'field-json-metadata-dictionary-index-fields', 'effect' => 'fade', ], @@ -97,14 +97,14 @@ public static function submitIndexFieldButton($location, $indexKey) { '#value' => $value, '#op' => $op, '#submit' => [ - [ - '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', - $callbackClass, - ], + [ + '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + $callbackClass, + ], ], '#ajax' => [ - 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexformAjax', - 'wrapper' => 'field-json-metadata-dictionary-index-fields-new', + 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexFormAjax', + 'wrapper' => 'field-json-metadata-dictionary-index-fields', 'effect' => 'fade', ], '#limit_validation_errors' => [], @@ -128,13 +128,13 @@ public static function submitIndexButton($location, $indexKey) { '#value' => $value, '#op' => $op, '#submit' => [ - [ - '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', - $callbackClass, - ], + [ + '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + $callbackClass, + ], ], '#ajax' => [ - 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexformAjax', + 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexFormAjax', 'wrapper' => 'field-json-metadata-dictionary-indexes', 'effect' => 'fade', ], @@ -150,7 +150,8 @@ public static function submitIndexButton($location, $indexKey) { /** * Create Cancel button. */ - public static function cancelIndexFieldButton($location, $indexKey) { + public static function cancelIndexFieldButton($location, $indexKey, $id) { + $callbackId = ($id === 'field-json-metadata-dictionary-index-fields-new') ? 'subIndexFormExistingFieldAjax' : 'subIndexFormFieldAjax'; $callbackClass = $location == 'edit' ? 'indexEditSubformCallback' : 'indexAddSubformCallback'; $op = $location == 'edit' && $indexKey ? 'abort_' . $indexKey : 'cancel_index_field'; $cancel_index_button = [ @@ -158,14 +159,14 @@ public static function cancelIndexFieldButton($location, $indexKey) { '#value' => t('Cancel'), '#op' => $op, '#submit' => [ - [ - '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', - $callbackClass, - ], + [ + '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + $callbackClass, + ], ], '#ajax' => [ - 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexformAjax', - 'wrapper' => 'field-json-metadata-dictionary-index-fields', + 'callback' => "Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::$callbackId", + 'wrapper' => $id, 'effect' => 'fade', ], '#limit_validation_errors' => [], @@ -188,13 +189,13 @@ public static function cancelIndexButton($location, $indexKey) { '#value' => t('Cancel Index'), '#op' => $op, '#submit' => [ - [ - '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', - $callbackClass, - ], + [ + '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + $callbackClass, + ], ], '#ajax' => [ - 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexformAjax', + 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexFormAjax', 'wrapper' => 'field-json-metadata-dictionary-indexes', 'effect' => 'fade', ], @@ -217,13 +218,13 @@ public static function deleteIndexButton($indexKey) { '#value' => t('Delete index field'), '#op' => 'delete_' . $indexKey, '#submit' => [ - [ - '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', - 'indexEditSubformCallback', - ], + [ + '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + 'indexEditSubformCallback', + ], ], '#ajax' => [ - 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexformAjax', + 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexFormAjax', 'wrapper' => 'field-json-metadata-dictionary-index-fields', 'effect' => 'fade', ], diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php index c1b3027900..eb9e5565fd 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php @@ -15,13 +15,8 @@ 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_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 = $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"]; + $current_index_fields = $form["field_json_metadata"]["widget"][0]['indexes']["fields"]["data"]["#rows"]; if ($current_index_fields) { $form_state->set('current_index_fields', $current_index_fields); @@ -33,7 +28,7 @@ public static function indexAddSubformCallback(array &$form, FormStateInterface if ($op === 'add_new_index_field') { $form_state->set('add_index_field', ''); - $add_index_fields = IndexFieldAddCreation::addIndexFields(); + $add_index_fields = IndexFieldAddCreation::addIndexFields($current_index_fields); $form_state->set('add_new_index_field', $add_index_fields); $form_state->set('index_added', FALSE); $form_state->set('adding_new_index_fields', TRUE); @@ -63,16 +58,12 @@ public static function indexAddCallback(array &$form, FormStateInterface $form_s $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') { @@ -82,7 +73,6 @@ 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); } @@ -141,16 +131,31 @@ public static function indexEditSubformCallback(array &$form, FormStateInterface } /** - * Ajax callback. + * Ajax callback to return index fields. */ - public static function subIndexformAjax(array &$form, FormStateInterface $form_state) { + public static function subIndexFormAjax(array &$form, FormStateInterface $form_state) { return $form["field_json_metadata"]["widget"][0]["indexes"]["fields"]; } /** - * Ajax callback. + * Ajax callback to return indexes. */ - public static function indexformAjax(array &$form, FormStateInterface $form_state) { + public static function indexFormAjax(array &$form, FormStateInterface $form_state) { return $form["field_json_metadata"]["widget"][0]["indexes"]; } + + /** + * 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"]["indexes"]["fields"]; + } + + /** + * 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"]["indexes"]["fields"]["add_row_button"]['#access'] = TRUE; + return $form["field_json_metadata"]["widget"][0]["indexes"]["field_collection"]["group"]["indexes"]["fields"]["add_row_button"]; + } } \ No newline at end of file diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php index 5d4e5d3a75..22562a66b4 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php @@ -12,22 +12,13 @@ class IndexFieldCreation { public static function createGeneralIndexFields($element, $field_json_metadata, $current_index_fields, $new_index, $index_fields_being_modified) { $element['indexes']['fields'] = [ - '#access' => TRUE, - '#type' => 'fieldset', - '#title' => t('Fields'), - '#prefix' => '
', - '#suffix' => '
', - '#markup' => t('
One or more fields included in index. Must be keys from the fields object.
'), - ]; - - // if ($new_index) { - // $element['indexes']['index_fields']['current_index_fields'] = []; - // } else { - // $element['indexes']['index_fields']['current_index_fields'] = $current_index_fields; - // } - - - + '#access' => TRUE, + '#type' => 'fieldset', + '#title' => t('Fields'), + '#prefix' => '
', + '#suffix' => '
', + '#markup' => t('
One or more fields included in index. Must be keys from the fields object.
'), + ]; return $element; } @@ -38,12 +29,12 @@ public static function createGeneralIndexFields($element, $field_json_metadata, public static function createGeneralIndex($element, $field_json_metadata, $current_index, $index_fields_being_modified) { $element['indexes'] = [ - '#type' => 'fieldset', - '#title' => t('Data Dictionary Indexes'), - '#prefix' => '
', - '#suffix' => '
', - '#markup' => t('
One or more indexes.
'), - ]; + '#type' => 'fieldset', + '#title' => t('Data Dictionary Indexes'), + '#prefix' => '
', + '#suffix' => '
', + '#markup' => t('
Adding indexes to your datastore tables can improve response times from common queries.
'), + ]; $element['indexes']['current_index'] = $current_index; @@ -53,32 +44,12 @@ public static function createGeneralIndex($element, $field_json_metadata, $curre /** * Create data index data rows. */ - 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 || (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 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', diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php index af61cefda7..a9567001ff 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php @@ -25,7 +25,6 @@ public static function editIndexFields($indexKey, $current_index_fields) { '#title' => 'Length', ]; - $edit_index_fields['update_index_field']['actions'] = self::createIndexActionFields($indexKey); return $edit_index_fields; diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php index ec90a5b205..bb9d2ec179 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php @@ -137,10 +137,6 @@ public static function setAddIndexFieldFormState($add_new_index_field, $new_inde $element['title']['#required'] = FALSE; } - - // else{ - // $element['indexes']['fields']['#access'] = FALSE; - // } return $element; } @@ -149,14 +145,13 @@ public static function setAddIndexFieldFormState($add_new_index_field, $new_inde */ 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; $element['identifier']['#required'] = FALSE; $element['title']['#required'] = FALSE; } + return $element; } 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 6f97cbb5b9..2e0376814b 100644 --- a/modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php +++ b/modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php @@ -12,7 +12,6 @@ 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. @@ -54,7 +53,6 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen $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); @@ -63,17 +61,9 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen // Build the index_data_results array to display the rows in the data table. $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); - - if ($index_field_values || $current_index_fields) { - $element = IndexFieldCreation::createGeneralIndexFields($element, $field_json_metadata, $current_index_fields, $form_state->get('add_new_index'), $form_state); - } + $element = IndexFieldCreation::createGeneralIndexFields($element, $field_json_metadata, $current_index_fields, $form_state->get('add_new_index'), $form_state); $element['dictionary_fields']['#pre_render'] = [ [$this, 'preRenderForm'], @@ -88,8 +78,6 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen ]; $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']['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); @@ -114,35 +102,18 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen if ($form_entity instanceof FieldableEntityInterface) { $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'), $form_state->get('add_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; - // } - + // Display index fields only when new index fields are being created. 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; } @@ -152,8 +123,6 @@ 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"]["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"]["fields"]["field_collection"]["group"] ?? []; @@ -181,8 +150,6 @@ public function massageFormValues(array $values, array $form, FormStateInterface $fields = $current_fields ?? []; } - - //$fields = array_merge($current_fields ?? [], $fields_input); $indexes = array_merge($current_indexes ?? [], $index_inputs); $json_data = [ @@ -194,9 +161,7 @@ public function massageFormValues(array $values, array $form, FormStateInterface ], ]; - $test = json_encode($json_data); - - return $test; + return json_encode($json_data); } /** From 4e9882ebff1688529460b71911b7ad6e03f780cc Mon Sep 17 00:00:00 2001 From: Kaise Lafrai Date: Fri, 24 May 2024 14:53:21 -0400 Subject: [PATCH 04/10] Added validation for index fields form, refactored code, refactored tests --- .../css/dataDictionaryWidget.css | 5 + .../data_dictionary_widget.module | 6 +- .../src/Fields/FieldEditCreation.php | 6 +- .../src/Fields/FieldOperations.php | 34 ++++--- .../src/Indexes/IndexFieldAddCreation.php | 35 ++++--- .../src/Indexes/IndexFieldButtons.php | 16 ++- .../src/Indexes/IndexFieldCallbacks.php | 14 ++- .../src/Indexes/IndexFieldCreation.php | 18 ++-- .../src/Indexes/IndexFieldEditCreation.php | 11 ++- .../src/Indexes/IndexFieldOperations.php | 28 +++--- .../src/Indexes/IndexValidation.php | 33 +++++++ .../FieldWidget/DataDictionaryWidget.php | 97 +++++++++---------- .../Unit/DataDictionaryWidgetBuildTest.php | 93 ++++++++++++++---- 13 files changed, 261 insertions(+), 135 deletions(-) create mode 100644 modules/data_dictionary_widget/src/Indexes/IndexValidation.php diff --git a/modules/data_dictionary_widget/css/dataDictionaryWidget.css b/modules/data_dictionary_widget/css/dataDictionaryWidget.css index c0bdf5d4fd..4a92b21683 100644 --- a/modules/data_dictionary_widget/css/dataDictionaryWidget.css +++ b/modules/data_dictionary_widget/css/dataDictionaryWidget.css @@ -1,3 +1,8 @@ +.index-fields-form.error { + border-width: var(--input--error-border-size); + border-color: var(--input--error-border-color); +} + .table { display: table; width: auto; diff --git a/modules/data_dictionary_widget/data_dictionary_widget.module b/modules/data_dictionary_widget/data_dictionary_widget.module index 894497652a..717ddb66dd 100644 --- a/modules/data_dictionary_widget/data_dictionary_widget.module +++ b/modules/data_dictionary_widget/data_dictionary_widget.module @@ -112,12 +112,12 @@ function data_dictionary_widget_form_alter(&$form, &$form_state, $form_id) { } $form['#validate'][] = 'data_dictionary_widget_validate_unique_identifier'; - $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_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_fields) { - foreach ($current_fields as $key => $value) { + if ($current_dictionary_fields) { + foreach ($current_dictionary_fields as $key => $value) { $keys = array_keys($value); $formatted_current_fields[$key] = []; diff --git a/modules/data_dictionary_widget/src/Fields/FieldEditCreation.php b/modules/data_dictionary_widget/src/Fields/FieldEditCreation.php index c6191d7da0..b20e6927f0 100644 --- a/modules/data_dictionary_widget/src/Fields/FieldEditCreation.php +++ b/modules/data_dictionary_widget/src/Fields/FieldEditCreation.php @@ -63,7 +63,7 @@ private static function createType($key, $current_dictionary_fields) { * Create Format field. */ private static function createFormat($key, $current_dictionary_fields) { - $format_options = FieldOperations::setFormatOptions($current_dictionary_fields[$key]['type']); + $format_options = FieldOperations::generateFormats($current_dictionary_fields[$key]['type'], "options"); $value = in_array($current_dictionary_fields[$key]['format'], $format_options) ? $current_dictionary_fields[$key]['format'] : 'other'; return [ '#name' => 'field_json_metadata[0][dictionary_fields][data][' . $key . '][field_collection][format]', @@ -71,7 +71,7 @@ private static function createFormat($key, $current_dictionary_fields) { '#required' => TRUE, '#title' => 'Format', '#default_value' => 'default', - '#description' => FieldOperations::generateFormatDescription($current_dictionary_fields[$key]['type']), + '#description' => FieldOperations::generateFormats($current_dictionary_fields[$key]['type'], "description"), '#value' => $value, '#prefix' => '
', '#suffix' => '
', @@ -84,7 +84,7 @@ private static function createFormat($key, $current_dictionary_fields) { * Create Format Other field. */ private static function createFormatOther($key, $current_dictionary_fields) { - $format_options = FieldOperations::setFormatOptions($current_dictionary_fields[$key]['type']); + $format_options = FieldOperations::generateFormats($current_dictionary_fields[$key]['type'], "options"); $value = !in_array($current_dictionary_fields[$key]['format'], $format_options) ? $current_dictionary_fields[$key]['format'] : NULL; return [ diff --git a/modules/data_dictionary_widget/src/Fields/FieldOperations.php b/modules/data_dictionary_widget/src/Fields/FieldOperations.php index 6b56165405..523f296a63 100644 --- a/modules/data_dictionary_widget/src/Fields/FieldOperations.php +++ b/modules/data_dictionary_widget/src/Fields/FieldOperations.php @@ -112,9 +112,9 @@ public static function generateFormats($dataType, $property) { /** * Cleaning the data up. */ - public static function processDataResults($data_results, $current_dictionary_fields, $field_values, $op) { - if (isset($current_dictionary_fields)) { - $data_results = $current_dictionary_fields; + public static function processDataResults($data_results, $current_fields, $field_values, $op) { + if (isset($current_fields)) { + $data_results = $current_fields; } if (isset($field_values["field_json_metadata"][0]["dictionary_fields"]["field_collection"])) { @@ -134,7 +134,7 @@ public static function processDataResults($data_results, $current_dictionary_fie } if (isset($data_pre) && $op === "add") { - $data_results = isset($current_dictionary_fields) ? array_merge($current_dictionary_fields, $data_pre) : $data_pre; + $data_results = isset($current_fields) ? array_merge($current_fields, $data_pre) : $data_pre; } return $data_results; @@ -171,9 +171,21 @@ public static function setTypeOptions() { /** * Return true if field is being edited. */ - public static function checkEditingField($key, $op_index, $dictionary_fields_being_modified) { + public static function checkEditingField($key, $op_index, $fields_being_modified) { $action_list = FieldOperations::editActions(); - if (isset($op_index[0]) && in_array($op_index[0], $action_list) && array_key_exists($key, $dictionary_fields_being_modified)) { + if (isset($op_index[0]) && in_array($op_index[0], $action_list) && array_key_exists($key, $fields_being_modified)) { + return TRUE; + } + else { + return FALSE; + } + } + + /** + * Return true if field collection is present. + */ + public static function checkFieldCollection($data_pre, $op) { + if (isset($data_pre) && $op === "add") { return TRUE; } else { @@ -198,12 +210,12 @@ public static function setAddFormState($add_new_field, $element) { /** * Create edit and update fields where needed. */ - public static function createDictionaryFieldOptions($op_index, $data_results, $dictionary_fields_being_modified, $element) { - $current_dictionary_fields = $element['current_dictionary_fields']; + public static function createDictionaryFieldOptions($op_index, $data_results, $fields_being_modified, $element) { + $current_fields = $element['current_dictionary_fields']; // Creating ajax buttons/fields to be placed in correct location later. foreach ($data_results as $key => $data) { - if (self::checkEditingField($key, $op_index, $dictionary_fields_being_modified)) { - $element['edit_fields'][$key] = FieldEditCreation::editFields($key, $current_dictionary_fields, $dictionary_fields_being_modified); + if (self::checkEditingField($key, $op_index, $fields_being_modified)) { + $element['edit_fields'][$key] = FieldEditCreation::editFields($key, $current_fields, $fields_being_modified); } else { $element['edit_buttons'][$key]['edit_button'] = FieldButtons::editButtons($key); @@ -355,4 +367,4 @@ public static function resetFieldValues(array &$form, FormStateInterface $form_s } } -} +} \ No newline at end of file diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php index 886565e36f..f68636f772 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php @@ -18,17 +18,21 @@ public static function addIndex() { '#open' => TRUE, '#prefix' => '
', '#suffix' => '
', + '#element_validate' => [ + ['\Drupal\data_dictionary_widget\Indexes\IndexValidation', 'indexFieldsValidation'] + ], ]; - $add_index['group']['indexes']['description'] = [ - '#name' => 'field_json_metadata[0][index][field_collection][group][description]', + $add_index['group']['index']['description'] = [ + '#name' => 'field_json_metadata[0][indexes][field_collection][group][index][description]', '#description' => t('Description of index purpose or functionality.'), '#type' => 'textfield', '#title' => 'Name', + '#required' => TRUE, ]; - $add_index['group']['indexes']['type'] = [ - '#name' => 'field_json_metadata[0][index][field_collection][group][type]', + $add_index['group']['index']['type'] = [ + '#name' => 'field_json_metadata[0][indexes][field_collection][group][index][type]', '#type' => 'select', '#description' => t('Index type.'), '#title' => 'Index Type', @@ -41,19 +45,22 @@ public static function addIndex() { ], ]; - $add_index['group']['indexes']['fields'] = [ + $add_index['group']['index']['fields'] = [ '#type' => 'fieldset', '#title' => t('Fields'), '#required' => TRUE, '#prefix' => '
', '#suffix' => '
', '#markup' => t('
One or more fields included in index. Must be keys from the fields object.
'), + '#attributes' => [ + 'class' => ['index-fields-form'], + ], ]; - $add_index['group']['indexes']['fields']['add_row_button'] = IndexFieldButtons::addIndexFieldButton(); + $add_index['group']['index']['fields']['add_row_button'] = IndexFieldButtons::addIndexFieldButton(); - $add_index['group']['indexes']['save_index'] = IndexFieldButtons::submitIndexButton('add_index', NULL); - $add_index['group']['indexes']['cancel_index'] = IndexFieldButtons::cancelIndexButton('cancel_index', NULL); + $add_index['group']['index']['save_index'] = IndexFieldButtons::submitIndexButton('add_index', NULL); + $add_index['group']['index']['cancel_index'] = IndexFieldButtons::cancelIndexButton('cancel_index', NULL); return $add_index; } @@ -72,14 +79,15 @@ public static function addIndexFields($current_index_fields) { '#markup' => t('
Add a single index field. Must be keys from the fields object.
'), ]; - $add_index_fields['group']['indexes']['fields']['name'] = [ - '#name' => 'field_json_metadata[0][fields][field_collection][group][name]', + $add_index_fields['group']['index']['fields']['name'] = [ + '#name' => 'field_json_metadata[0][indexes][fields][field_collection][group][index][fields][name]', '#type' => 'textfield', '#title' => 'Name', + '#required' => TRUE, ]; - $add_index_fields['group']['indexes']['fields']['length'] = self::createIndexFieldLengthField(); - $add_index_fields['group']['indexes']['fields']['actions'] = self::createIndexActionFields($id); + $add_index_fields['group']['index']['fields']['length'] = self::createIndexFieldLengthField(); + $add_index_fields['group']['index']['fields']['actions'] = self::createIndexActionFields($id); return $add_index_fields; } @@ -89,9 +97,10 @@ public static function addIndexFields($current_index_fields) { */ private static function createIndexFieldLengthField() { return [ - '#name' => 'field_json_metadata[0][fields][field_collection][group][length]', + '#name' => 'field_json_metadata[0][indexes][fields][field_collection][group][index][fields][length]', '#type' => 'number', '#title' => 'Length', + '#required' => TRUE, ]; } diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php index be0f5cfe0a..28ab44c494 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php @@ -2,6 +2,7 @@ namespace Drupal\data_dictionary_widget\Indexes; +use Drupal\Core\Form\FormStateInterface; /** * Various operations for creating Data Dictionary Widget fields. */ @@ -81,7 +82,9 @@ public static function editIndexButtons($indexKey) { 'wrapper' => 'field-json-metadata-dictionary-index-fields', 'effect' => 'fade', ], - '#limit_validation_errors' => [], + '#limit_validation_errors' => [ + ['field_json_metadata', 0, 'indexes', 'field_collection', 'group', 'index', 'type'], + ], ]; } @@ -107,7 +110,10 @@ public static function submitIndexFieldButton($location, $indexKey) { 'wrapper' => 'field-json-metadata-dictionary-index-fields', 'effect' => 'fade', ], - '#limit_validation_errors' => [], + '#limit_validation_errors' => [ + ['field_json_metadata', 0, 'indexes', 'fields', 'field_collection', 'group', 'index', 'fields', 'name'], + ['field_json_metadata', 0, 'indexes', 'fields', 'field_collection', 'group', 'index', 'fields', 'length'], + ], ]; if ($location == 'edit') { @@ -120,6 +126,7 @@ public static function submitIndexFieldButton($location, $indexKey) { * Create Submit buttons. */ public static function submitIndexButton($location, $indexKey) { + $class = static::class; $callbackClass = $location == 'edit' ? 'indexEditCallback' : 'indexAddCallback'; $op = !empty($indexKey) ? 'update_' . $indexKey : 'add_index'; $value = $location == 'edit' ? 'Save' : 'Submit Index'; @@ -138,7 +145,10 @@ public static function submitIndexButton($location, $indexKey) { 'wrapper' => 'field-json-metadata-dictionary-indexes', 'effect' => 'fade', ], - '#limit_validation_errors' => [], + '#limit_validation_errors' => [ + ['field_json_metadata', 0, 'indexes', 'field_collection', 'group', 'index', 'description'], + ['field_json_metadata', 0, 'indexes', 'field_collection', 'group', 'index', 'fields'], + ], ]; if ($location == 'edit') { diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php index eb9e5565fd..0c46131c5c 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php @@ -141,6 +141,14 @@ public static function subIndexFormAjax(array &$form, FormStateInterface $form_s * Ajax callback to return indexes. */ public static function indexFormAjax(array &$form, FormStateInterface $form_state) { + $index_fields = $form["field_json_metadata"]["widget"][0]["indexes"]["fields"]; + + // Validation errors skip submit callbacks, this will set the index fields in the correct location. + if ($index_fields["data"]) { + $form["field_json_metadata"]["widget"][0]["indexes"]["field_collection"]["group"]["index"]["fields"] = $index_fields; + $form["field_json_metadata"]["widget"][0]["indexes"]["fields"]['#access'] = FALSE; + } + return $form["field_json_metadata"]["widget"][0]["indexes"]; } @@ -148,14 +156,14 @@ public static function indexFormAjax(array &$form, FormStateInterface $form_stat * 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"]["indexes"]["fields"]; + 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. */ public static function subIndexFormExistingFieldAjax(array &$form, FormStateInterface $form_state) { - $form["field_json_metadata"]["widget"][0]["indexes"]["field_collection"]["group"]["indexes"]["fields"]["add_row_button"]['#access'] = TRUE; - return $form["field_json_metadata"]["widget"][0]["indexes"]["field_collection"]["group"]["indexes"]["fields"]["add_row_button"]; + $form["field_json_metadata"]["widget"][0]["indexes"]["field_collection"]["group"]["index"]["fields"]["add_row_button"]['#access'] = TRUE; + return $form["field_json_metadata"]["widget"][0]["indexes"]["field_collection"]["group"]["index"]["fields"]["add_row_button"]; } } \ No newline at end of file diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php index 22562a66b4..16740129b4 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php @@ -9,15 +9,14 @@ class IndexFieldCreation { /** * Create basic widget. */ - public static function createGeneralIndexFields($element, $field_json_metadata, $current_index_fields, $new_index, $index_fields_being_modified) { - + public static function createGeneralIndexFields($element) { $element['indexes']['fields'] = [ - '#access' => TRUE, '#type' => 'fieldset', '#title' => t('Fields'), '#prefix' => '
', '#suffix' => '
', '#markup' => t('
One or more fields included in index. Must be keys from the fields object.
'), + '#required' => TRUE, ]; return $element; @@ -26,8 +25,7 @@ public static function createGeneralIndexFields($element, $field_json_metadata, /** * Create basic widget. */ - public static function createGeneralIndex($element, $field_json_metadata, $current_index, $index_fields_being_modified) { - + public static function createGeneralIndex($element, $current_indexes) { $element['indexes'] = [ '#type' => 'fieldset', '#title' => t('Data Dictionary Indexes'), @@ -36,7 +34,7 @@ public static function createGeneralIndex($element, $field_json_metadata, $curre '#markup' => t('
Adding indexes to your datastore tables can improve response times from common queries.
'), ]; - $element['indexes']['current_index'] = $current_index; + $element['indexes']['current_index'] = $current_indexes; return $element; } @@ -44,7 +42,7 @@ public static function createGeneralIndex($element, $field_json_metadata, $curre /** * Create data index data rows. */ - 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) { + public static function createIndexFieldsDataRows($index_field_values, $current_index_fields, $index_fields_data_results, $form_state) { if ($index_field_values) { return [ '#access' => ((bool) $current_index_fields || (bool) $index_fields_data_results), @@ -60,14 +58,14 @@ public static function createIndexFieldsDataRows($index_added, $adding_new_index /** * Create data index data rows. */ - public static function createIndexDataRows($current_indexes, $index_data, $form_state) { + public static function createIndexDataRows($current_indexes, $index_data_results, $form_state) { return [ - '#access' => ((bool) $current_indexes || (bool) $index_data), + '#access' => ((bool) $current_indexes || (bool) $index_data_results), '#type' => 'table', '#header' => ['NAME', 'TYPE', 'FIELDS'], '#prefix' => '
', '#suffix' => '
', - '#rows' => $form_state->get('cancel_index') ? $current_indexes : ($index_data ?? []), + '#rows' => $form_state->get('cancel_index') ? $current_indexes : ($index_data_results ?? []), '#tree' => TRUE, '#theme' => 'custom_index_table', ]; diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php index a9567001ff..f4d6087195 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php @@ -11,6 +11,7 @@ class IndexFieldEditCreation { * Create edit fields for Data Dictionary Widget. */ public static function editIndexFields($indexKey, $current_index_fields) { + $id = $current_index_fields ? "field-json-metadata-dictionary-index-fields-new" : "field-json-metadata-dictionary-index-fields"; $indexKeyExplode = explode("_", $indexKey); $edit_index_fields['name'] = [ '#name' => 'field_json_metadata[0][fields][data][' . $indexKeyExplode[3] . '][field_collection][name]', @@ -25,7 +26,7 @@ public static function editIndexFields($indexKey, $current_index_fields) { '#title' => 'Length', ]; - $edit_index_fields['update_index_field']['actions'] = self::createIndexActionFields($indexKey); + $edit_index_fields['update_index_field']['actions'] = self::createIndexActionFields($indexKey, $id); return $edit_index_fields; } @@ -34,6 +35,7 @@ public static function editIndexFields($indexKey, $current_index_fields) { * Create edit fields for Data Dictionary Widget. */ public static function editIndex($indexKey, $current_index) { + $id = $current_index ? "field-json-metadata-dictionary-index-fields-new" : "field-json-metadata-dictionary-index-fields"; $indexKeyExplode = explode("_", $indexKey); $edit_index['name'] = [ '#name' => 'field_json_metadata[0][index][data][' . $indexKeyExplode[3] . '][field_collection][name]', @@ -48,8 +50,7 @@ public static function editIndex($indexKey, $current_index) { '#title' => 'Length', ]; - - $edit_index['update_index_field']['actions'] = self::createIndexActionFields($indexKey); + $edit_index['update_index_field']['actions'] = self::createIndexActionFields($indexKey, $id ); return $edit_index; } @@ -57,11 +58,11 @@ public static function editIndex($indexKey, $current_index) { /** * Create Action buttons. */ - private static function createIndexActionFields($indexKey) { + private static function createIndexActionFields($indexKey, $id) { return [ '#type' => 'actions', 'save_update' => IndexFieldButtons::submitIndexFieldButton('edit', $indexKey), - 'cancel_updates' => IndexFieldButtons::cancelIndexFieldButton('edit', $indexKey), + 'cancel_updates' => IndexFieldButtons::cancelIndexFieldButton('edit', $indexKey, $id), 'delete_field' => IndexFieldButtons::deleteIndexButton($indexKey), ]; } diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php index bb9d2ec179..019b3216ba 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php @@ -65,13 +65,13 @@ public static function processIndexFieldsDataResults($index_data_results, $curre $index_data_results = $current_index_fields; } - 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"]; + if (isset($index_field_values["field_json_metadata"][0]["indexes"]["fields"]["field_collection"])) { + $index_field_group = $index_field_values["field_json_metadata"][0]["indexes"]["fields"]["field_collection"]["group"]; $data_index_fields_pre = [ [ - "name" => $index_field_group["name"], - "length" => (int)$index_field_group["length"], + "name" => $index_field_group['index']['fields']["name"], + "length" => (int)$index_field_group['index']['fields']["length"], ], ]; } @@ -86,25 +86,25 @@ public static function processIndexFieldsDataResults($index_data_results, $curre /** * 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; + public static function processIndexDataResults($index_results, $current_indexes, $index_values, $index_fields_data_results, $op) { + if (isset($current_indexes)) { + $index_results = $current_indexes; } - if (isset($index_values["field_json_metadata"][0]["index"]["field_collection"])) { - $index_group = $index_values["field_json_metadata"][0]["index"]["field_collection"]["group"]; + if (isset($index_values["field_json_metadata"][0]["indexes"]["field_collection"])) { + $index_group = $index_values["field_json_metadata"][0]["indexes"]["field_collection"]["group"]; $data_index_pre = [ [ - "description" => $index_group["description"], - "type" => $index_group["type"], + "description" => $index_group['index']["description"], + "type" => $index_group['index']["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; + $index_results = isset($current_indexes) ? array_merge($current_indexes, $data_index_pre) : $data_index_pre; } return $index_results; @@ -126,7 +126,7 @@ public static function editIndexActions() { /** * Set the elements associated with adding a new field. */ - public static function setAddIndexFieldFormState($add_new_index_field, $new_index_field, $element) { + public static function setAddIndexFieldFormState($add_new_index_field, $element) { if ($add_new_index_field) { $element['indexes']['fields']['#access'] = FALSE; @@ -135,6 +135,7 @@ public static function setAddIndexFieldFormState($add_new_index_field, $new_inde $element['indexes']['fields']['add_row_button']['#access'] = FALSE; $element['identifier']['#required'] = FALSE; $element['title']['#required'] = FALSE; + //$element["indexes"]["field_collection"]["group"]["index"]["description"]['#required'] = FALSE; } return $element; @@ -150,6 +151,7 @@ public static function setAddIndexFormState($add_new_index, $element) { $element['indexes']['add_row_button']['#access'] = FALSE; $element['identifier']['#required'] = FALSE; $element['title']['#required'] = FALSE; + //$element["indexes"]["field_collection"]["group"]["index"]["description"]['#required'] = FALSE; } return $element; diff --git a/modules/data_dictionary_widget/src/Indexes/IndexValidation.php b/modules/data_dictionary_widget/src/Indexes/IndexValidation.php new file mode 100644 index 0000000000..169461116f --- /dev/null +++ b/modules/data_dictionary_widget/src/Indexes/IndexValidation.php @@ -0,0 +1,33 @@ +setError($index_fields_fieldset, t('At least one index field is required.')); + } + } + +} 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 2e0376814b..a1b192ec20 100644 --- a/modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php +++ b/modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php @@ -30,85 +30,78 @@ class DataDictionaryWidget extends WidgetBase implements TrustedCallbackInterfac * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { - $field_values = $form_state->get("new_dictionary_fields"); + // Retrieve values from form state + $dictionary_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'); + $add_index_field = $form_state->get('add_new_index_field'); + $add_new_index = $form_state->get('add_new_index'); + $add_new_dictionary_field = $form_state->get('add_new_field'); + $current_dictionary_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("dictionary_fields_being_modified") ?? NULL; + $dictionary_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; $field_json_metadata = !empty($items[0]->value) ? json_decode($items[0]->value, TRUE) : []; $op_index = isset($form_state->getTriggeringElement()['#op']) ? explode("_", $form_state->getTriggeringElement()['#op']) : NULL; - $data_results = $field_json_metadata ? $field_json_metadata["data"]["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); + // Retrieve initial data results from field JSON metadata + $data_results = $field_json_metadata["data"]["fields"] ?? []; + $index_fields_results = $field_json_metadata["data"]["indexes"][0]["fields"] ?? []; + $index_results = $field_json_metadata["data"]["indexes"] ?? []; - // Build the index_field_data_results array to display the rows in the data table. + // Process data results + $data_results = FieldOperations::processDataResults($data_results, $current_dictionary_fields, $dictionary_field_values, $op); $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_results, $current_indexes, $index_values, $index_fields_data_results, $op); - $element = FieldCreation::createGeneralFields($element, $field_json_metadata, $current_fields, $form_state); - $element = IndexFieldCreation::createGeneralIndex($element, $field_json_metadata, $current_indexes, $form_state); - $element = IndexFieldCreation::createGeneralIndexFields($element, $field_json_metadata, $current_index_fields, $form_state->get('add_new_index'), $form_state); - $element['dictionary_fields']['#pre_render'] = [ - [$this, 'preRenderForm'], - ]; + // Create form elements + $element = FieldCreation::createGeneralFields($element, $field_json_metadata, $current_dictionary_fields, $form_state); + $element = IndexFieldCreation::createGeneralIndex($element, $current_indexes); + $element = IndexFieldCreation::createGeneralIndexFields($element); - $element['indexes']['#pre_render'] = [ - [$this, 'preRenderIndexForm'], - ]; + // Add pre-render functions + $element['dictionary_fields']['#pre_render'] = [[$this, 'preRenderForm']]; + $element['indexes']['#pre_render'] = [[$this, 'preRenderIndexForm']]; + $element['indexes']['fields']['#pre_render'] = [[$this, 'preRenderIndexFieldForm']]; - $element['indexes']['fields']['#pre_render'] = [ - [$this, 'preRenderIndexFieldForm'], - ]; - - $element['dictionary_fields']['data'] = FieldCreation::createDictionaryDataRows($current_fields, $data_results, $form_state); + // Add data rows to display in tables + $element['dictionary_fields']['data'] = FieldCreation::createDictionaryDataRows($current_dictionary_fields, $data_results, $form_state); $element['indexes']['data'] = IndexFieldCreation::createIndexDataRows($current_indexes, $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']); - $element['dictionary_fields']['add_row_button']['#access'] = $fields_being_modified == NULL ? TRUE : FALSE; + $element['indexes']['fields']['data'] = IndexFieldCreation::createIndexFieldsDataRows($index_field_values, $current_index_fields, $index_fields_data_results, $form_state); - // Creating ajax buttons/fields to be placed in correct location later for index fields. + // Create dictionary fields/buttons for editing + $element['dictionary_fields'] = FieldOperations::createDictionaryFieldOptions($op_index, $data_results, $dictionary_fields_being_modified, $element['dictionary_fields']); + $element['dictionary_fields']['add_row_button']['#access'] = $dictionary_fields_being_modified == NULL ? TRUE : FALSE; + + // Create index fields/buttons for editing $element['indexes'] = IndexFieldOperations::createDictionaryIndexOptions($op_index, $index_data_results, $index_fields_being_modified, $element['indexes']); - 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; + // Get form entity $form_object = $form_state->getFormObject(); if (!($form_object instanceof EntityFormInterface)) { return; } $form_entity = $form_object->getEntity(); + // Set form entity data type if ($form_entity instanceof FieldableEntityInterface) { $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'), $form_state->get('add_index_field'), $element); + // Set form state for adding fields and indexes + $element = FieldOperations::setAddFormState($add_new_dictionary_field, $element); + $element = IndexFieldOperations::setAddIndexFormState($add_new_index, $element); + $element = IndexFieldOperations::setAddIndexFieldFormState($add_index_field, $element); // Display index fields only when new index fields are being created. - if ($form_state->get('add_new_index_field') || $form_state->get('new_index_fields')) { + if ($add_index_field || $index_field_values) { $element['indexes']['fields']['#access'] = TRUE; } else { $element['indexes']['fields']['#access'] = FALSE; @@ -121,7 +114,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen * {@inheritdoc} */ public function massageFormValues(array $values, array $form, FormStateInterface $form_state) { - $current_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"]; + $current_dictionary_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"]; $current_indexes = $form["field_json_metadata"]["widget"][0]["indexes"]["data"]["#rows"]; $field_collection = $values[0]['dictionary_fields']["field_collection"]["group"] ?? []; $indexes_collection = $values[0]["indexes"]["fields"]["field_collection"]["group"] ?? []; @@ -144,10 +137,10 @@ public function massageFormValues(array $values, array $form, FormStateInterface ] : []; if (isset($fields_input)) { - $fields = array_merge($current_fields ?? [], $fields_input); + $fields = array_merge($current_dictionary_fields ?? [], $fields_input); } else { - $fields = $current_fields ?? []; + $fields = $current_dictionary_fields ?? []; } $indexes = array_merge($current_indexes ?? [], $index_inputs); @@ -165,7 +158,7 @@ public function massageFormValues(array $values, array $form, FormStateInterface } /** - * Prerender callback for the form. + * Prerender callback for the dictionary form. * * Moves the buttons into the table. */ @@ -174,12 +167,12 @@ public function preRenderForm(array $dictionaryFields) { } /** - * Prerender callback for the index form. + * Prerender callback for the index field form. * * Moves the buttons into the table. */ - public function preRenderIndexFieldForm(array $dictionaryIndexFields) { - return IndexFieldOperations::setIndexFieldsAjaxElements($dictionaryIndexFields); + public function preRenderIndexFieldForm(array $indexFields) { + return IndexFieldOperations::setIndexFieldsAjaxElements($indexFields); } /** @@ -187,8 +180,8 @@ public function preRenderIndexFieldForm(array $dictionaryIndexFields) { * * Moves the buttons into the table. */ - public function preRenderIndexForm(array $dictionaryIndexes) { - return IndexFieldOperations::setIndexAjaxElements($dictionaryIndexes); + public function preRenderIndexForm(array $indexes) { + return IndexFieldOperations::setIndexAjaxElements($indexes); } /** diff --git a/modules/data_dictionary_widget/tests/src/Unit/DataDictionaryWidgetBuildTest.php b/modules/data_dictionary_widget/tests/src/Unit/DataDictionaryWidgetBuildTest.php index 45106113ad..de230f0622 100644 --- a/modules/data_dictionary_widget/tests/src/Unit/DataDictionaryWidgetBuildTest.php +++ b/modules/data_dictionary_widget/tests/src/Unit/DataDictionaryWidgetBuildTest.php @@ -151,6 +151,16 @@ public function testAddNewFieldDictionaryWidget() { 'data' => [ '#rows' => null ] + ], + 'indexes' => [ + 'data' => [ + '#rows' => null + ], + 'fields' => [ + 'data' => [ + '#rows' => null + ] + ] ] ] ] @@ -209,8 +219,6 @@ public function testAddNewFieldDictionaryWidget() { ] ]; - - $dataDictionaryWidget = new DataDictionaryWidget ( $plugin_id, $plugin_definition, @@ -240,13 +248,15 @@ public function testAddNewFieldDictionaryWidget() { ->method('getTriggeringElement') ->willReturn($trigger); - $formState->expects($this->exactly(4)) + $formState->expects($this->exactly(6)) ->method('set') ->willReturnOnConsecutiveCalls ( '', $this->equalTo($user_input), TRUE, - FALSE + FALSE, + '', + '' ); FieldCallbacks::addSubformCallback($form, $formState); @@ -310,9 +320,9 @@ public function testEditButtonsCreation() { ->method('set') ->with('field_data_type', 'data-dictionary'); - $formState->expects($this->exactly(5)) + $formState->expects($this->exactly(13)) ->method('get') - ->willReturnOnConsecutiveCalls([], $current_fields, NULL, FALSE, NULL); + ->willReturnOnConsecutiveCalls(NULL, NULL, NULL, FALSE, NULL, NULL, $current_fields); $dataDictionaryWidget = new DataDictionaryWidget ( $plugin_id, @@ -394,7 +404,29 @@ public function testEditDataDictionaryField() { 'identifier' => 'test_identifier', 'title' => 'test_title', 'dictionary_fields' => [ - 'data' => [ + 'field_collection' => [ + 'group' => [ + 'name' => 'test_edit', + 'title' => 'test_edit', + 'type' => 'string', + 'format' => 'default', + 'format_other' => '', + 'description' => 'test_edit', + ] + ], + "data" => [ + '#rows' => [ + 0 => [ + 'field_collection' => [ + 'name' => 'test_edit', + 'title' => 'test_edit', + 'type' => 'string', + 'format' => 'default', + 'format_other' => '', + 'description' => 'test_edit', + ], + ], + ], 0 => [ 'field_collection' => [ 'name' => 'test_edit', @@ -403,10 +435,15 @@ public function testEditDataDictionaryField() { 'format' => 'default', 'format_other' => '', 'description' => 'test_edit', - ] - ] + ], + ], + ], + ], + 'indexes' => [ + 'data' => [ + 0 => [], ] - ] + ], ] ] ]; @@ -423,9 +460,9 @@ public function testEditDataDictionaryField() { ->method('set') ->with('field_data_type', 'data-dictionary'); - $formState->expects($this->exactly(12)) + $formState->expects($this->exactly(28)) ->method('get') - ->willReturnOnConsecutiveCalls([], $current_fields, $fields_being_modified, FALSE, NULL, $fields_being_modified, $fields_being_modified, [], $updated_current_fields, $fields_being_modified, FALSE, NULL); + ->willReturnOnConsecutiveCalls($user_input, [], [], [], [], $updated_current_fields, $current_fields, [], [], $fields_being_modified, [], FALSE, FALSE); $formState->expects($this->exactly(11)) ->method('getTriggeringElement') @@ -461,15 +498,33 @@ public function testEditDataDictionaryField() { $this->assertArrayHasKey('description', $element["dictionary_fields"]["edit_fields"][0]); $this->assertArrayHasKey('update_field', $element["dictionary_fields"]["edit_fields"][0]); - $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"][0] = [ - 'name' => 'test_edit', - 'title' => 'test_edit', - 'type' => 'string', - 'format' => 'default', - 'format_other' => '', - 'description' => 'test_edit', + $form["field_json_metadata"]["widget"][0] = [ + "dictionary_fields" => [ + "data" => [ + "#rows" => [ + 0 => [ + 'name' => 'test_edit', + 'title' => 'test_edit', + 'type' => 'string', + 'format' => 'default', + 'format_other' => '', + 'description' => 'test_edit', + ], + ], + ], + ], + 'indexes' => [ + 'fields' => [ + 'data' => [ + "#rows" => [ + 0 => [], + ], + ], + ] + ], ]; + // Trigger callback function to save the edited fields. FieldCallbacks::editSubformCallback($form, $formState); From edb0c955d51b848b7aeba3a5fbff40df5be3fd44 Mon Sep 17 00:00:00 2001 From: Kaise Lafrai Date: Thu, 27 Jun 2024 10:01:59 -0400 Subject: [PATCH 05/10] Fully functional edit experience for index fields --- .../src/Indexes/IndexFieldAddCreation.php | 4 +-- .../src/Indexes/IndexFieldButtons.php | 16 ++++++++--- .../src/Indexes/IndexFieldCallbacks.php | 21 ++++++++++++++ .../src/Indexes/IndexFieldCreation.php | 2 +- .../src/Indexes/IndexFieldEditCreation.php | 16 ++++++----- .../src/Indexes/IndexFieldOperations.php | 2 +- .../src/Indexes/IndexFieldValues.php | 8 +++--- .../src/Indexes/IndexValidation.php | 28 +++++++++++++++++++ .../FieldWidget/DataDictionaryWidget.php | 2 +- 9 files changed, 78 insertions(+), 21 deletions(-) diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php index f68636f772..be87b0585a 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php @@ -48,7 +48,6 @@ public static function addIndex() { $add_index['group']['index']['fields'] = [ '#type' => 'fieldset', '#title' => t('Fields'), - '#required' => TRUE, '#prefix' => '
', '#suffix' => '
', '#markup' => t('
One or more fields included in index. Must be keys from the fields object.
'), @@ -58,7 +57,6 @@ public static function addIndex() { ]; $add_index['group']['index']['fields']['add_row_button'] = IndexFieldButtons::addIndexFieldButton(); - $add_index['group']['index']['save_index'] = IndexFieldButtons::submitIndexButton('add_index', NULL); $add_index['group']['index']['cancel_index'] = IndexFieldButtons::cancelIndexButton('cancel_index', NULL); @@ -69,7 +67,7 @@ public static function addIndex() { * Create add fields for Data Dictionary Widget. */ public static function addIndexFields($current_index_fields) { - $id = $current_index_fields ? "field-json-metadata-dictionary-index-fields-new" : "field-json-metadata-dictionary-index-fields"; + $id = "field-json-metadata-dictionary-index-fields-new"; $add_index_fields['#access'] = FALSE; $add_index_fields['group'] = [ '#type' => 'fieldset', diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php index 28ab44c494..1b688c21c1 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php @@ -49,7 +49,7 @@ public static function addIndexButton() { ], '#ajax' => [ 'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexFormAjax', - 'wrapper' => 'field-json-metadata-dictionary-indexes', + 'wrapper' => 'field-json-metadata-dictionary-index', 'effect' => 'fade', ], '#limit_validation_errors' => [], @@ -114,10 +114,18 @@ public static function submitIndexFieldButton($location, $indexKey) { ['field_json_metadata', 0, 'indexes', 'fields', 'field_collection', 'group', 'index', 'fields', 'name'], ['field_json_metadata', 0, 'indexes', 'fields', 'field_collection', 'group', 'index', 'fields', 'length'], ], + '#element_validate' => [ + ['Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', 'indexFieldVal'], + ], ]; if ($location == 'edit') { + $indexKeyExplode = explode("_", $indexKey); $edit_index_button['#name'] = 'update_' . $indexKey; + $edit_index_button['#limit_validation_errors'] = [ + ['field_json_metadata', 0, 'indexes', 'fields', 'edit_index_fields', $indexKeyExplode[3], 'name'], + ['field_json_metadata', 0, 'indexes', 'fields', 'edit_index_fields', $indexKeyExplode[3], 'length'], + ]; } return $edit_index_button; } @@ -142,7 +150,7 @@ public static function submitIndexButton($location, $indexKey) { ], '#ajax' => [ 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexFormAjax', - 'wrapper' => 'field-json-metadata-dictionary-indexes', + 'wrapper' => 'field-json-metadata-dictionary-index', 'effect' => 'fade', ], '#limit_validation_errors' => [ @@ -161,7 +169,7 @@ public static function submitIndexButton($location, $indexKey) { * Create Cancel button. */ public static function cancelIndexFieldButton($location, $indexKey, $id) { - $callbackId = ($id === 'field-json-metadata-dictionary-index-fields-new') ? 'subIndexFormExistingFieldAjax' : 'subIndexFormFieldAjax'; + $callbackId = ($id === 'field-json-metadata-dictionary-index-fields-new') ? 'subIndexFormExistingFieldAjax' : 'subIndexFormAjax'; $callbackClass = $location == 'edit' ? 'indexEditSubformCallback' : 'indexAddSubformCallback'; $op = $location == 'edit' && $indexKey ? 'abort_' . $indexKey : 'cancel_index_field'; $cancel_index_button = [ @@ -206,7 +214,7 @@ public static function cancelIndexButton($location, $indexKey) { ], '#ajax' => [ 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexFormAjax', - 'wrapper' => 'field-json-metadata-dictionary-indexes', + 'wrapper' => 'field-json-metadata-dictionary-index', '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 0c46131c5c..b282180b29 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php @@ -24,6 +24,7 @@ public static function indexAddSubformCallback(array &$form, FormStateInterface if ($op === 'cancel_index_field') { $form_state->set('cancel_index_field', TRUE); + $form_state->set('add_new_index_field', ''); } if ($op === 'add_new_index_field') { @@ -116,7 +117,13 @@ public static function indexEditSubformCallback(array &$form, FormStateInterface unset($currently_modifying_index_fields[$op_index[4]]); unset($current_index_fields[$op_index[4]]); $current_index_fields[$op_index[4]] = IndexFieldValues::updateIndexFieldValues($op_index[4], $update_values, $current_index_fields ); + // $value = $update_values["field_json_metadata"][0]["indexes"]["fields"]["edit_index_fields"][0]["name"]; + // if ($value === "") { + // $field = $form["field_json_metadata"]["widget"][0]["indexes"]["fields"]["edit_index_fields"]["index_field_key_0"]["name"]; + // $form_state->setError($field, t(' testone index field is required.')); + // } ksort($current_index_fields ); + } if (str_contains($op, 'edit')) { @@ -166,4 +173,18 @@ public static function subIndexFormExistingFieldAjax(array &$form, FormStateInte $form["field_json_metadata"]["widget"][0]["indexes"]["field_collection"]["group"]["index"]["fields"]["add_row_button"]['#access'] = TRUE; return $form["field_json_metadata"]["widget"][0]["indexes"]["field_collection"]["group"]["index"]["fields"]["add_row_button"]; } + + /** + * Widget validation callback. + */ + public static function indexFieldVal($element, FormStateInterface &$form_state, array &$form) { + $fields_to_validate = [ + 'name' => 'Name', + 'length' => 'Length', + ]; + + foreach ($fields_to_validate as $field_key => $field_label) { + IndexValidation::indexFieldVal($form_state, $field_key, $field_label, $form); + } + } } \ No newline at end of file diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php index 16740129b4..ada3eee303 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php @@ -29,7 +29,7 @@ public static function createGeneralIndex($element, $current_indexes) { $element['indexes'] = [ '#type' => 'fieldset', '#title' => t('Data Dictionary Indexes'), - '#prefix' => '
', + '#prefix' => '
', '#suffix' => '
', '#markup' => t('
Adding indexes to your datastore tables can improve response times from common queries.
'), ]; diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php index f4d6087195..598bc1c4f9 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php @@ -10,28 +10,30 @@ class IndexFieldEditCreation { /** * Create edit fields for Data Dictionary Widget. */ - public static function editIndexFields($indexKey, $current_index_fields) { - $id = $current_index_fields ? "field-json-metadata-dictionary-index-fields-new" : "field-json-metadata-dictionary-index-fields"; + public static function editIndexFields($indexKey, $current_index_fields, $index_fields_being_modified) { + $id = $current_index_fields ? "field-json-metadata-dictionary-index-fields" : "field-json-metadata-dictionary-index-fields-new"; $indexKeyExplode = explode("_", $indexKey); $edit_index_fields['name'] = [ - '#name' => 'field_json_metadata[0][fields][data][' . $indexKeyExplode[3] . '][field_collection][name]', + '#name' => 'field_json_metadata[0][indexes][fields][edit_index_fields][' . $indexKeyExplode[3] . '][name]', '#type' => 'textfield', '#value' => $current_index_fields[$indexKeyExplode[3]]['name'], '#title' => 'Name', + '#required' => TRUE, ]; $edit_index_fields['length'] = [ - '#name' => 'field_json_metadata[0][fields][data]['. $indexKeyExplode[3] .'][field_collection][length]', + '#name' => 'field_json_metadata[0][indexes][fields][edit_index_fields][' . $indexKeyExplode[3] . '][length]', '#type' => 'number', '#value' => $current_index_fields[$indexKeyExplode[3]]['length'], '#title' => 'Length', + '#required' => TRUE, ]; $edit_index_fields['update_index_field']['actions'] = self::createIndexActionFields($indexKey, $id); - return $edit_index_fields; + return $edit_index_fields; } - /** + /** * Create edit fields for Data Dictionary Widget. */ public static function editIndex($indexKey, $current_index) { @@ -51,8 +53,8 @@ public static function editIndex($indexKey, $current_index) { ]; $edit_index['update_index_field']['actions'] = self::createIndexActionFields($indexKey, $id ); - return $edit_index; + return $edit_index; } /** diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php index 019b3216ba..4ebdc71500 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php @@ -161,7 +161,7 @@ public static function setAddIndexFormState($add_new_index, $element) { * Create edit and update fields where needed. */ public static function createDictionaryIndexFieldOptions($op_index, $index_data_results, $index_fields_being_modified, $element) { - $current_index_fields = $element['current_index_fields'] ?? NULL; + $current_index_fields = $index_data_results ?? NULL; // 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)) { diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php index b3c881a3ec..b51791b609 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php @@ -3,19 +3,19 @@ namespace Drupal\data_dictionary_widget\Indexes; /** - * Various operations for creating Data Dictionary Widget fields. + * Various operations for creating index fields. */ 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]['fields']['data'][$field_index]['field_collection']['name']; - $length = $update_values['field_json_metadata'][0]['fields']['data'][$field_index]['field_collection']['length']; + $name = $update_values["field_json_metadata"][0]["indexes"]["fields"]["edit_index_fields"][0]["name"]; + $length = $update_values["field_json_metadata"][0]["indexes"]["fields"]["edit_index_fields"][0]["length"]; return [ 'name' => $name, - 'length' => $length, + 'length' => (int)$length, ]; } } \ No newline at end of file diff --git a/modules/data_dictionary_widget/src/Indexes/IndexValidation.php b/modules/data_dictionary_widget/src/Indexes/IndexValidation.php index 169461116f..79bcdc24a2 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexValidation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexValidation.php @@ -30,4 +30,32 @@ public static function indexFieldsValidation(array $element, FormStateInterface } } + /** + * Validation callback for a index fields edit form. + * + * If index field name and length is empty on edit, validation will trigger. + * + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + * @param array $field_key + * The key value for the field being edited. + * @param array $field_label + * The label for the field being edited. + */ + public static function indexFieldVal(FormStateInterface $form_state, $field_key, $field_label) { + $trigger = $form_state->getTriggeringElement(); + $op = $trigger['#op']; + $op_index = explode("_", $op); + + // Perform validation for update operation. + if (str_contains($op, 'update')) { + $update_values = $form_state->getUserInput(); + $value = $update_values["field_json_metadata"][0]["indexes"]["fields"]["edit_index_fields"][0][$field_key]; + if ($value === "") { + $field = "field_json_metadata][0][indexes][fields][edit_index_fields][$op_index[4]][$field_key"; + $form_state->setErrorByName($field, t($field_label . ' field is required.')); + } + } + } + } 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 a1b192ec20..c5ac9577f8 100644 --- a/modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php +++ b/modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php @@ -81,7 +81,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen 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; + $element['indexes']['fields']['add_row_button']['#access'] = $index_fields_being_modified == NULL ? TRUE : FALSE; // Get form entity $form_object = $form_state->getFormObject(); From c61206d2483dc7ebc8a7b45cc87b78c87eee3df1 Mon Sep 17 00:00:00 2001 From: Kaise Lafrai Date: Thu, 27 Jun 2024 10:24:52 -0400 Subject: [PATCH 06/10] Fixed index key value to correctly update the index fields upon edit and save --- .../data_dictionary_widget/src/Indexes/IndexFieldValues.php | 4 ++-- .../data_dictionary_widget/src/Indexes/IndexValidation.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php index b51791b609..53fc55d0ce 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]["indexes"]["fields"]["edit_index_fields"][0]["name"]; - $length = $update_values["field_json_metadata"][0]["indexes"]["fields"]["edit_index_fields"][0]["length"]; + $name = $update_values["field_json_metadata"][0]["indexes"]["fields"]["edit_index_fields"][$field_index]["name"]; + $length = $update_values["field_json_metadata"][0]["indexes"]["fields"]["edit_index_fields"][$field_index]["length"]; return [ 'name' => $name, diff --git a/modules/data_dictionary_widget/src/Indexes/IndexValidation.php b/modules/data_dictionary_widget/src/Indexes/IndexValidation.php index 79bcdc24a2..50d7c4187f 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexValidation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexValidation.php @@ -50,7 +50,7 @@ public static function indexFieldVal(FormStateInterface $form_state, $field_key, // Perform validation for update operation. if (str_contains($op, 'update')) { $update_values = $form_state->getUserInput(); - $value = $update_values["field_json_metadata"][0]["indexes"]["fields"]["edit_index_fields"][0][$field_key]; + $value = $update_values["field_json_metadata"][0]["indexes"]["fields"]["edit_index_fields"][$op_index[4]][$field_key]; if ($value === "") { $field = "field_json_metadata][0][indexes][fields][edit_index_fields][$op_index[4]][$field_key"; $form_state->setErrorByName($field, t($field_label . ' field is required.')); From d389df6494dae6f1e7e5bbdf851e4da544fd8a26 Mon Sep 17 00:00:00 2001 From: Kaise Lafrai <56809719+kaise-lafrai@users.noreply.github.com> Date: Tue, 2 Jul 2024 16:20:51 -0400 Subject: [PATCH 07/10] New phpunit function to test the edit feature of the data dictionary fields (#4214) --- .../Unit/DataDictionaryWidgetBuildTest.php | 152 +++++++++--------- 1 file changed, 72 insertions(+), 80 deletions(-) diff --git a/modules/data_dictionary_widget/tests/src/Unit/DataDictionaryWidgetBuildTest.php b/modules/data_dictionary_widget/tests/src/Unit/DataDictionaryWidgetBuildTest.php index de230f0622..c462aed9a6 100644 --- a/modules/data_dictionary_widget/tests/src/Unit/DataDictionaryWidgetBuildTest.php +++ b/modules/data_dictionary_widget/tests/src/Unit/DataDictionaryWidgetBuildTest.php @@ -366,7 +366,7 @@ public function testEditDataDictionaryField() { $plugin_id = ''; $plugin_definition = []; - $current_fields = [ + $current_dictionary_fields = [ [ 'name' => 'test', 'title' => 'test', @@ -376,7 +376,7 @@ public function testEditDataDictionaryField() { ] ]; - $updated_current_fields = [ + $updated_dictionary_fields = [ [ 'name' => 'test_edit', 'title' => 'test_edit', @@ -386,16 +386,6 @@ public function testEditDataDictionaryField() { ] ]; - $fields_being_modified = [ - [ - 'name' => 'test', - 'title' => 'test', - 'type' => 'string', - 'format' => 'default', - 'description' => 'test' - ] - ]; - $op = "edit_0"; $user_input = [ @@ -414,38 +404,57 @@ public function testEditDataDictionaryField() { 'description' => 'test_edit', ] ], - "data" => [ - '#rows' => [ - 0 => [ - 'field_collection' => [ - 'name' => 'test_edit', - 'title' => 'test_edit', - 'type' => 'string', - 'format' => 'default', - 'format_other' => '', - 'description' => 'test_edit', - ], - ], - ], - 0 => [ + 'data' => [ + [ 'field_collection' => [ - 'name' => 'test_edit', - 'title' => 'test_edit', - 'type' => 'string', - 'format' => 'default', - 'format_other' => '', - 'description' => 'test_edit', + 'name' => 'test_edit', + 'title' => 'test_edit', + 'type' => 'string', + 'format' => 'default', + 'format_other' => '', + 'description' => 'test_edit', ], ], ], ], - 'indexes' => [ - 'data' => [ - 0 => [], + ], + ], + ]; + + $form["field_json_metadata"]["widget"][0] = [ + "dictionary_fields" => [ + "data" => [ + "#rows" => [ + [ + 'name' => 'test', + 'title' => 'test', + 'type' => 'string', + 'format' => 'default', + 'format_other' => '', + 'description' => 'test', + ], + ], + ], + 'field_collection' => [ + 'group' => [ + 'name' => 'test', + 'title' => 'test', + 'type' => 'string', + 'format' => 'default', + 'format_other' => '', + 'description' => 'test', ] ], + ], + 'indexes' => [ + 'fields' => [ + 'data' => [ + "#rows" => [ + 0 => [], + ], + ], ] - ] + ], ]; $formState->expects($this->exactly(2)) @@ -460,13 +469,19 @@ public function testEditDataDictionaryField() { ->method('set') ->with('field_data_type', 'data-dictionary'); - $formState->expects($this->exactly(28)) + $formState->expects($this->any()) ->method('get') - ->willReturnOnConsecutiveCalls($user_input, [], [], [], [], $updated_current_fields, $current_fields, [], [], $fields_being_modified, [], FALSE, FALSE); + ->willReturnOnConsecutiveCalls( + [], [], $user_input, [], [], [], [], [], $current_dictionary_fields, [], [], $current_dictionary_fields, [], + [], [], $user_input, [], [], [], [], [], [], [], [], $updated_dictionary_fields, [], [], $current_dictionary_fields, [], + ); - $formState->expects($this->exactly(11)) + $formState->expects($this->any()) ->method('getTriggeringElement') - ->willReturnOnConsecutiveCalls(['#op' => $op], ['#op' => $op], ['#op' => $op], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => $op], ['#op' => $op], ['#op' => $op], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0']); + ->willReturnOnConsecutiveCalls( + ['#op' => $op], ['#op' => $op], ['#op' => $op], ['#op' => $op], ['#op' => $op], ['#op' => $op], ['#op' => $op], ['#op' => $op], ['#op' => $op], ['#op' => $op], + ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'], + ); $formState->expects($this->any()) ->method('getUserInput') @@ -480,6 +495,10 @@ public function testEditDataDictionaryField() { $third_party_settings ); + // Trigger callback function to edit fields. + FieldCallbacks::editSubformCallback($form, $formState); + + // First call to re-create data dictionary form with the editable fields. $element = $dataDictionaryWidget->formElement( $fieldItemList, 0, @@ -488,43 +507,15 @@ public function testEditDataDictionaryField() { $formState ); - // Assert that the subform to collect the edits for the data dictionary field exists. - $this->assertNotNull($element["dictionary_fields"]["edit_fields"][0]); - $this->assertArrayHasKey('name', $element["dictionary_fields"]["edit_fields"][0]); - $this->assertArrayHasKey('title', $element["dictionary_fields"]["edit_fields"][0]); - $this->assertArrayHasKey('type', $element["dictionary_fields"]["edit_fields"][0]); - $this->assertArrayHasKey('format', $element["dictionary_fields"]["edit_fields"][0]); - $this->assertArrayHasKey('format_other', $element["dictionary_fields"]["edit_fields"][0]); - $this->assertArrayHasKey('description', $element["dictionary_fields"]["edit_fields"][0]); - $this->assertArrayHasKey('update_field', $element["dictionary_fields"]["edit_fields"][0]); + // Assert edit feature loads form with the current field values. + $this->assertNotNull($element); + $this->assertEquals($element["dictionary_fields"]["data"]["#rows"][0]["name"], $current_dictionary_fields[0]["name"]); + $this->assertEquals($element["dictionary_fields"]["data"]["#rows"][0]["title"], $current_dictionary_fields[0]["title"]); + $this->assertEquals($element["dictionary_fields"]["data"]["#rows"][0]["type"], $current_dictionary_fields[0]["type"]); + $this->assertEquals($element["dictionary_fields"]["data"]["#rows"][0]["format"], $current_dictionary_fields[0]["format"]); + $this->assertEquals($element["dictionary_fields"]["data"]["#rows"][0]["description"], $current_dictionary_fields[0]["description"]); - $form["field_json_metadata"]["widget"][0] = [ - "dictionary_fields" => [ - "data" => [ - "#rows" => [ - 0 => [ - 'name' => 'test_edit', - 'title' => 'test_edit', - 'type' => 'string', - 'format' => 'default', - 'format_other' => '', - 'description' => 'test_edit', - ], - ], - ], - ], - 'indexes' => [ - 'fields' => [ - 'data' => [ - "#rows" => [ - 0 => [], - ], - ], - ] - ], - ]; - // Trigger callback function to save the edited fields. FieldCallbacks::editSubformCallback($form, $formState); @@ -537,11 +528,12 @@ public function testEditDataDictionaryField() { $formState ); + // Assert update feature loads form with the edited field values. $this->assertNotNull($element); - $this->assertEquals($element["dictionary_fields"]["data"]["#rows"][0]["name"], $updated_current_fields[0]["name"]); - $this->assertEquals($element["dictionary_fields"]["data"]["#rows"][0]["title"], $updated_current_fields[0]["title"]); - $this->assertEquals($element["dictionary_fields"]["data"]["#rows"][0]["type"], $updated_current_fields[0]["type"]); - $this->assertEquals($element["dictionary_fields"]["data"]["#rows"][0]["format"], $updated_current_fields[0]["format"]); - $this->assertEquals($element["dictionary_fields"]["data"]["#rows"][0]["description"], $updated_current_fields[0]["description"]); + $this->assertEquals($element["dictionary_fields"]["data"]["#rows"][0]["name"], $updated_dictionary_fields[0]["name"]); + $this->assertEquals($element["dictionary_fields"]["data"]["#rows"][0]["title"], $updated_dictionary_fields[0]["title"]); + $this->assertEquals($element["dictionary_fields"]["data"]["#rows"][0]["type"], $updated_dictionary_fields[0]["type"]); + $this->assertEquals($element["dictionary_fields"]["data"]["#rows"][0]["format"], $updated_dictionary_fields[0]["format"]); + $this->assertEquals($element["dictionary_fields"]["data"]["#rows"][0]["description"], $updated_dictionary_fields[0]["description"]); } } From 1e6c0245538f74a82d51295f0a839ce0b78871a0 Mon Sep 17 00:00:00 2001 From: Kaise Lafrai Date: Tue, 2 Jul 2024 17:21:43 -0400 Subject: [PATCH 08/10] Removed commented out code that was missed, added line breaks --- .../src/Indexes/IndexFieldCallbacks.php | 7 +------ .../src/Indexes/IndexFieldValues.php | 2 +- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php index b282180b29..6966afeee6 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php @@ -117,11 +117,6 @@ public static function indexEditSubformCallback(array &$form, FormStateInterface unset($currently_modifying_index_fields[$op_index[4]]); unset($current_index_fields[$op_index[4]]); $current_index_fields[$op_index[4]] = IndexFieldValues::updateIndexFieldValues($op_index[4], $update_values, $current_index_fields ); - // $value = $update_values["field_json_metadata"][0]["indexes"]["fields"]["edit_index_fields"][0]["name"]; - // if ($value === "") { - // $field = $form["field_json_metadata"]["widget"][0]["indexes"]["fields"]["edit_index_fields"]["index_field_key_0"]["name"]; - // $form_state->setError($field, t(' testone index field is required.')); - // } ksort($current_index_fields ); } @@ -187,4 +182,4 @@ public static function indexFieldVal($element, FormStateInterface &$form_state, IndexValidation::indexFieldVal($form_state, $field_key, $field_label, $form); } } -} \ No newline at end of file +} diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php index 53fc55d0ce..4a5dcac5e8 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php @@ -18,4 +18,4 @@ public static function updateIndexFieldValues($field_index, $update_values, $cur 'length' => (int)$length, ]; } -} \ No newline at end of file +} From 0da283cb1bf33bc6dadc2a50cd8787d60ce45611 Mon Sep 17 00:00:00 2001 From: Kaise Lafrai Date: Mon, 8 Jul 2024 15:07:22 -0400 Subject: [PATCH 09/10] Code cleanup, added unique name attribute to buttons to prevent conflicting callback triggers. --- .../data_dictionary_widget/src/Fields/FieldButtons.php | 10 ++++++---- .../src/Fields/FieldCallbacks.php | 1 + .../src/Indexes/IndexFieldButtons.php | 5 ++++- .../src/Indexes/IndexFieldCreation.php | 2 +- .../src/Indexes/IndexFieldOperations.php | 3 --- .../tests/src/Unit/DataDictionaryWidgetBuildTest.php | 2 +- 6 files changed, 13 insertions(+), 10 deletions(-) diff --git a/modules/data_dictionary_widget/src/Fields/FieldButtons.php b/modules/data_dictionary_widget/src/Fields/FieldButtons.php index 87d36ff492..da7c06c5d6 100644 --- a/modules/data_dictionary_widget/src/Fields/FieldButtons.php +++ b/modules/data_dictionary_widget/src/Fields/FieldButtons.php @@ -14,13 +14,14 @@ public static function addButton() { return [ '#type' => 'submit', '#value' => 'Add field', + '#name' => 'add_dictionary_field', '#access' => TRUE, '#op' => 'add_new_field', '#submit' => [ - [ - '\Drupal\data_dictionary_widget\Fields\FieldCallbacks', - 'addSubformCallback', - ], + [ + '\Drupal\data_dictionary_widget\Fields\FieldCallbacks', + 'addSubformCallback', + ], ], '#ajax' => [ 'callback' => '\Drupal\data_dictionary_widget\Fields\FieldCallbacks::subformAjax', @@ -112,6 +113,7 @@ public static function cancelButton($location, $key) { $cancel_button = [ '#type' => 'submit', '#value' => t('Cancel'), + '#name' => 'cancel_dictionary_field', '#op' => $op, '#submit' => [ [ diff --git a/modules/data_dictionary_widget/src/Fields/FieldCallbacks.php b/modules/data_dictionary_widget/src/Fields/FieldCallbacks.php index 1d608a7860..c63edb3512 100644 --- a/modules/data_dictionary_widget/src/Fields/FieldCallbacks.php +++ b/modules/data_dictionary_widget/src/Fields/FieldCallbacks.php @@ -123,6 +123,7 @@ public static function addSubformCallback(array &$form, FormStateInterface $form $form_state->set('add', TRUE); $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/IndexFieldButtons.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php index 1b688c21c1..419ade0e72 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php @@ -2,7 +2,6 @@ namespace Drupal\data_dictionary_widget\Indexes; -use Drupal\Core\Form\FormStateInterface; /** * Various operations for creating Data Dictionary Widget fields. */ @@ -15,6 +14,7 @@ public static function addIndexFieldButton() { return [ '#type' => 'submit', '#value' => 'Add field', + '#name' => 'add_index_field', '#access' => TRUE, '#op' => 'add_new_index_field', '#submit' => [ @@ -39,6 +39,7 @@ public static function addIndexButton() { return [ '#type' => 'submit', '#value' => 'Add index', + '#name' => 'add_index', '#access' => TRUE, '#op' => 'add_new_index', '#submit' => [ @@ -175,6 +176,7 @@ public static function cancelIndexFieldButton($location, $indexKey, $id) { $cancel_index_button = [ '#type' => 'submit', '#value' => t('Cancel'), + '#name' => 'cancel_index_field', '#op' => $op, '#submit' => [ [ @@ -205,6 +207,7 @@ public static function cancelIndexButton($location, $indexKey) { $cancel_index_button = [ '#type' => 'submit', '#value' => t('Cancel Index'), + '#name' => 'cancel_index', '#op' => $op, '#submit' => [ [ diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php index ada3eee303..4fae4f57d9 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php @@ -28,7 +28,7 @@ public static function createGeneralIndexFields($element) { public static function createGeneralIndex($element, $current_indexes) { $element['indexes'] = [ '#type' => 'fieldset', - '#title' => t('Data Dictionary Indexes'), + '#title' => t('Indexes'), '#prefix' => '
', '#suffix' => '
', '#markup' => t('
Adding indexes to your datastore tables can improve response times from common queries.
'), diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php index 4ebdc71500..381472dbbb 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php @@ -128,14 +128,12 @@ public static function editIndexActions() { */ public static function setAddIndexFieldFormState($add_new_index_field, $element) { if ($add_new_index_field) { - $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; - //$element["indexes"]["field_collection"]["group"]["index"]["description"]['#required'] = FALSE; } return $element; @@ -151,7 +149,6 @@ public static function setAddIndexFormState($add_new_index, $element) { $element['indexes']['add_row_button']['#access'] = FALSE; $element['identifier']['#required'] = FALSE; $element['title']['#required'] = FALSE; - //$element["indexes"]["field_collection"]["group"]["index"]["description"]['#required'] = FALSE; } return $element; diff --git a/modules/data_dictionary_widget/tests/src/Unit/DataDictionaryWidgetBuildTest.php b/modules/data_dictionary_widget/tests/src/Unit/DataDictionaryWidgetBuildTest.php index c462aed9a6..387c07818d 100644 --- a/modules/data_dictionary_widget/tests/src/Unit/DataDictionaryWidgetBuildTest.php +++ b/modules/data_dictionary_widget/tests/src/Unit/DataDictionaryWidgetBuildTest.php @@ -241,7 +241,7 @@ public function testAddNewFieldDictionaryWidget() { $element = FieldOperations::setAddFormState($add_fields, $element); // Set up a triggering element with '#op' set to 'add'. - $trigger= ['#op' => 'add']; + $trigger = ['#op' => 'add']; // Expect that getTriggeringElement will be called once and return the add. $formState->expects($this->any()) From bf7f4049adc406a0d9e58a8db82446dd5e05dd8c Mon Sep 17 00:00:00 2001 From: Kaise Lafrai Date: Thu, 18 Jul 2024 09:57:47 -0400 Subject: [PATCH 10/10] Index Edit --- .../src/Indexes/IndexFieldButtons.php | 62 ++++++-- .../src/Indexes/IndexFieldCallbacks.php | 39 ++++- .../src/Indexes/IndexFieldCreation.php | 20 ++- .../src/Indexes/IndexFieldEditCreation.php | 88 +++++++++-- .../src/Indexes/IndexFieldOperations.php | 149 ++++++++++++++++-- .../src/Indexes/IndexFieldValues.php | 15 ++ .../FieldWidget/DataDictionaryWidget.php | 32 +++- .../custom-index-fields-table.html.twig | 2 + 8 files changed, 360 insertions(+), 47 deletions(-) diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php index 419ade0e72..6953ba0dc4 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php @@ -60,17 +60,17 @@ public static function addIndexButton() { /** * Returns the edit buttons. */ - public static function editIndexButtons($indexKey) { + public static function editIndexFieldButtons($indexKey) { return [ '#type' => 'image_button', - '#name' => 'edit_index_' . $indexKey, - '#id' => 'edit_index_' . $indexKey, + '#name' => 'edit_' . $indexKey, + '#id' => 'edit_' . $indexKey, '#access' => TRUE, '#op' => 'edit_' . $indexKey, '#src' => 'core/misc/icons/787878/cog.svg', '#attributes' => [ 'class' => ['index-field-plugin-settings-edit'], - 'alt' => t('Edit index'), + 'alt' => t('Edit index field'), ], '#submit' => [ [ @@ -89,16 +89,49 @@ public static function editIndexButtons($indexKey) { ]; } + /** + * Returns the edit buttons. + */ + public static function editIndexButtons($indexKey) { + return [ + '#type' => 'image_button', + '#name' => 'edit_' . $indexKey, + '#id' => 'edit_' . $indexKey, + '#access' => TRUE, + '#op' => 'edit_' . $indexKey, + '#src' => 'core/misc/icons/787878/cog.svg', + '#attributes' => [ + 'class' => ['index-field-plugin-settings-edit'], + 'alt' => t('Edit index'), + ], + '#submit' => [ + [ + '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + 'indexEditSubformCallback', + ], + ], + '#ajax' => [ + 'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexFormAjax', + 'wrapper' => 'field-json-metadata-dictionary-index', + 'effect' => 'fade', + ], + '#limit_validation_errors' => [ + ['field_json_metadata', 0, 'indexes', 'field_collection', 'group', 'index', 'type'], + ], + ]; + } + /** * Create Submit buttons. */ public static function submitIndexFieldButton($location, $indexKey) { $callbackClass = $location == 'edit' ? 'indexEditSubformCallback' : 'indexAddSubformCallback'; $op = !empty($indexKey) ? 'update_' . $indexKey : 'add_index_field'; - $value = $location == 'edit' ? 'Save' : 'Add '; + $value = $location == 'edit' ? 'Save123' : 'Add '; $edit_index_button = [ '#type' => 'submit', '#value' => $value, + '#name' => uniqid(), '#op' => $op, '#submit' => [ [ @@ -136,9 +169,9 @@ public static function submitIndexFieldButton($location, $indexKey) { */ public static function submitIndexButton($location, $indexKey) { $class = static::class; - $callbackClass = $location == 'edit' ? 'indexEditCallback' : 'indexAddCallback'; + $callbackClass = $location == 'edit' ? 'indexEditSubformCallback' : 'indexAddCallback'; $op = !empty($indexKey) ? 'update_' . $indexKey : 'add_index'; - $value = $location == 'edit' ? 'Save' : 'Submit Index'; + $value = $location == 'edit' ? $callbackClass : 'Submit Index'; $edit_index_button = [ '#type' => 'submit', '#value' => $value, @@ -162,6 +195,8 @@ public static function submitIndexButton($location, $indexKey) { if ($location == 'edit') { $edit_index_button['#name'] = 'update_' . $indexKey; + } else { + $edit_index_button['#name'] = 'add_' . $indexKey; } return $edit_index_button; } @@ -202,7 +237,7 @@ public static function cancelIndexFieldButton($location, $indexKey, $id) { * Create Cancel button. */ public static function cancelIndexButton($location, $indexKey) { - $callbackClass = $location == 'edit' ? 'indexEditCallback' : 'indexAddCallback'; + $callbackClass = $location == 'edit' ? 'indexEditSubformCallback' : 'indexAddCallback'; $op = $location == 'edit' && $indexKey ? 'abort_' . $indexKey : 'cancel_index'; $cancel_index_button = [ '#type' => 'submit', @@ -233,6 +268,13 @@ public static function cancelIndexButton($location, $indexKey) { * Create Delete button. */ public static function deleteIndexButton($indexKey) { + if (str_contains($indexKey, 'field')) { + $id = 'field-json-metadata-dictionary-index-fields'; + $callback = 'subIndexFormAjax'; + } else { + $id = 'field-json-metadata-dictionary-index'; + $callback = 'indexFormAjax'; + } return [ '#type' => 'submit', '#name' => 'index_delete_' . $indexKey, @@ -245,8 +287,8 @@ public static function deleteIndexButton($indexKey) { ], ], '#ajax' => [ - 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexFormAjax', - 'wrapper' => 'field-json-metadata-dictionary-index-fields', + 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::' . $callback, + 'wrapper' => $id, '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 6966afeee6..1012fe5b5a 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php @@ -17,7 +17,7 @@ public static function indexAddSubformCallback(array &$form, FormStateInterface $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"]; - + //$current_index_fields = $form["field_json_metadata"]["widget"][0]["indexes"]["edit_index"]["index_key_0"]["fields"]["index_fields"]["#rows"]; if ($current_index_fields) { $form_state->set('current_index_fields', $current_index_fields); } @@ -97,37 +97,50 @@ 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"]["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 = $form_state->get('dictionary_fields_being_modified') != NULL ? $form_state->get('dictionary_fields_being_modified') : []; + $currently_modifying_index = $form_state->get('index_being_modified') != NULL ? $form_state->get('index_being_modified') : []; if (str_contains($op, 'abort')) { unset($currently_modifying_index_fields[$op_index[4]]); + unset($currently_modifying_index[$op_index[3]]); } if (str_contains($op, 'delete')) { unset($currently_modifying_index_fields[$op_index[4]]); + unset($currently_modifying_index[$op_index[3]]); unset($current_index_fields[$op_index[4]]); + unset($current_index[$op_index[3]]); } if (str_contains($op, 'update')) { $update_values = $form_state->getUserInput(); unset($currently_modifying_index_fields[$op_index[4]]); unset($current_index_fields[$op_index[4]]); - $current_index_fields[$op_index[4]] = IndexFieldValues::updateIndexFieldValues($op_index[4], $update_values, $current_index_fields ); - ksort($current_index_fields ); - + unset($currently_modifying_index[$op_index[3]]); + $current_index_fields[$op_index[4]] = IndexFieldValues::updateIndexFieldValues($op_index[4], $update_values, $current_index_fields); + $current_index[$op_index[3]] = IndexFieldValues::updateIndexValues($op_index[3], $update_values, $current_index); + ksort($current_index_fields); + ksort($current_index); + } + + if (str_contains($op, 'edit_index_key')) { + $currently_modifying_index[$op_index[3]] = $current_index[$op_index[3]]; } - if (str_contains($op, 'edit')) { + if (str_contains($op, 'edit_index_field_key')) { $currently_modifying_index_fields[$op_index[4]] = $current_index_fields[$op_index[4]]; } $form_state->set('dictionary_fields_being_modified', $currently_modifying); $form_state->set('index_fields_being_modified', $currently_modifying_index_fields); - $form_state->set('current_index_fields', $current_index_fields ); + $form_state->set('index_being_modified', $currently_modifying_index); + $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 ); $form_state->setRebuild(); } @@ -139,6 +152,20 @@ public static function subIndexFormAjax(array &$form, FormStateInterface $form_s return $form["field_json_metadata"]["widget"][0]["indexes"]["fields"]; } + /** + * Ajax callback to return index fields. + */ + public static function editsubIndexFormAjax(array &$form, FormStateInterface $form_state) { + return $form["field_json_metadata"]["widget"][0]["indexes"]["edit_index"]["index_key_0"]["fields"]; + } + + /** + * Ajax callback to return index fields. + */ + public static function testsubIndexFormAjax(array &$form, FormStateInterface $form_state) { + return $form["field_json_metadata"]["widget"][0]["indexes"]; + } + /** * Ajax callback to return indexes. */ diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php index 4fae4f57d9..3a5ce59ea3 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php @@ -43,7 +43,7 @@ public static function createGeneralIndex($element, $current_indexes) { * Create data index data rows. */ public static function createIndexFieldsDataRows($index_field_values, $current_index_fields, $index_fields_data_results, $form_state) { - if ($index_field_values) { + //if ($index_field_values || $current_index_fields) { return [ '#access' => ((bool) $current_index_fields || (bool) $index_fields_data_results), '#type' => 'table', @@ -52,9 +52,25 @@ public static function createIndexFieldsDataRows($index_field_values, $current_i '#tree' => TRUE, '#theme' => 'custom_index_fields_table', ]; - } + //} } + // /** + // * Create data index data rows. + // */ + // public static function createIndexFieldsEditDataRows($index_field_values, $current_index_fields, $index_fields_data_results, $form_state) { + // if ($index_fields_data_results) { + // return [ + // '#access' => ((bool) $current_index_fields || (bool) $index_fields_data_results), + // '#type' => 'table', + // '#header' => ['NAME', 'LENGTH'], + // '#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. */ diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php index 598bc1c4f9..bdbe7feb60 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php @@ -28,7 +28,9 @@ public static function editIndexFields($indexKey, $current_index_fields, $index_ '#required' => TRUE, ]; - $edit_index_fields['update_index_field']['actions'] = self::createIndexActionFields($indexKey, $id); + $edit_index_fields['update_index_field']['actions'] = self::createIndexFieldActionFields($indexKey, $id); + + //$edit_index_fields['update_index_field']['actions'] = self::createIndexActionFields($indexKey, $id); return $edit_index_fields; } @@ -36,23 +38,73 @@ public static function editIndexFields($indexKey, $current_index_fields, $index_ /** * Create edit fields for Data Dictionary Widget. */ - public static function editIndex($indexKey, $current_index) { - $id = $current_index ? "field-json-metadata-dictionary-index-fields-new" : "field-json-metadata-dictionary-index-fields"; + public static function editIndex($indexKey, $current_index, $index_being_modified, $element) { + $id = $current_index ? "field-json-metadata-dictionary-index-new" : "field-json-metadata-dictionary-index"; $indexKeyExplode = explode("_", $indexKey); - $edit_index['name'] = [ - '#name' => 'field_json_metadata[0][index][data][' . $indexKeyExplode[3] . '][field_collection][name]', + if ($element["fields"]["data"]["#rows"]) { + $test = $element["fields"]["data"]["#rows"]; + } else { + $test = $current_index[$indexKeyExplode[2]]['fields']; + } + + $edit_index['description'] = [ + '#name' => "field_json_metadata[0][indexes][edit_index][index_key_" . $indexKeyExplode[2] . "][description]", '#type' => 'textfield', - '#value' => $current_index[$indexKeyExplode[3]]['name'], + '#description' => t('Description of index purpose or functionality.'), + '#value' => $current_index[$indexKeyExplode[2]]['description'], '#title' => 'Name', + '#required' => TRUE, ]; - $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['type'] = [ + '#name' => "field_json_metadata[0][indexes][edit_index][index_key_" . $indexKeyExplode[2] . "][type]", + '#value' => $current_index[$indexKeyExplode[2]]['type'], + '#type' => 'select', + '#description' => t('Index type.'), + '#title' => 'Index Type', + '#options' => [ + 'index' => t('index'), + 'fulltext' => t('fulltext'), + ], + '#required' => TRUE, ]; - $edit_index['update_index_field']['actions'] = self::createIndexActionFields($indexKey, $id ); + $edit_index['fields'] = [ + '#type' => 'fieldset', + '#title' => t('Fields'), + '#prefix' => '
', + '#suffix' => '
', + '#markup' => t('
One or more fields included in index. Must be keys from the fields object.
'), + '#attributes' => [ + 'class' => ['index-fields-form'], + ], + ]; + + $edit_index['fields']['index_fields'] = $element["fields"]["data"]; + + $button = IndexFieldButtons::editIndexFieldButtons('index_field_key_0'); + + $edit_button['button'] = $button; + //$edit_index['edit_index_field_buttons']['index_field_key_' . $indexKey]['edit_index_field_button'] = IndexFieldButtons::editIndexFieldButtons('index_field_key_' . $indexKey); + $edit_index["fields"]["index_fields"]["#rows"][0] = array_merge($element["fields"]["data"]["#rows"][0], $edit_button); + + $edit_index['fields']['add_row_button'] = IndexFieldButtons::addIndexFieldButton(); + + //IndexFieldOperations::setIndexFieldsAjaxElements($current_index[$indexKeyExplode[2]]['fields']); + + // $edit_index['fields']['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['fields']['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']['actions'] = self::createIndexActionFields($indexKey, $id ); return $edit_index; } @@ -61,6 +113,18 @@ public static function editIndex($indexKey, $current_index) { * Create Action buttons. */ private static function createIndexActionFields($indexKey, $id) { + return [ + '#type' => 'actions', + 'save_update' => IndexFieldButtons::submitIndexButton('edit', $indexKey), + 'cancel_updates' => IndexFieldButtons::cancelIndexButton('edit', $indexKey, $id), + 'delete_field' => IndexFieldButtons::deleteIndexButton($indexKey), + ]; + } + + /** + * Create Action buttons. + */ + private static function createIndexFieldActionFields($indexKey, $id) { return [ '#type' => 'actions', 'save_update' => IndexFieldButtons::submitIndexFieldButton('edit', $indexKey), diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php index 381472dbbb..130922bd8f 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php @@ -6,17 +6,49 @@ * Various operations for the Data Dictionary Widget. */ class IndexFieldOperations { + // /** + // * Setting ajax elements. + // */ + // public static function setIndexFieldsAjaxElements(array $dictionaryIndexFields) { + // if ($dictionaryIndexFields["index_fields"]) { + // foreach ($dictionaryIndexFields['index_fields']['#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['index_fields']['#rows'][$row] = array_merge($data, $edit_index_button); + // unset($dictionaryIndexFields['edit_index_buttons']['index_field_key_' . $row]); + // } + // elseif ($edit_index_fields) { + // unset($dictionaryIndexFields['index_fields']['#rows']['index_field_key_' . $row]); + // $dictionaryIndexFields['index_fields']['#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['index_fields']['#rows']); + // } + // } + // } + + // return $dictionaryIndexFields; + // } + /** * Setting ajax elements. */ public static function setIndexFieldsAjaxElements(array $dictionaryIndexFields) { if ($dictionaryIndexFields["data"]) { foreach ($dictionaryIndexFields['data']['#rows'] as $row => $data) { - $edit_index_button = $dictionaryIndexFields['edit_index_buttons']['index_field_key_' . $row] ?? NULL; + $edit_index_button = $dictionaryIndexFields["fields"]["edit_index_buttons"]['index_field_key_' . $row] ?? NULL; + //$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); + $dictionaryIndexFields["data"]["#rows"][0]["fields"][0]["name"] = "blah"; + $dictionaryIndexFields["fields"]["data"]["#rows"][0]["name"] = "blah"; + $dictionaryIndexFields["current_index"][0]["fields"][0]["name"]["#value"] = "blah"; + $dictionaryIndexFields["field_collection"]["group"]["index"]["fields"]["data"]["#rows"][0]["name"] = "blah"; + //$dictionaryIndexFields['data']['#rows'][$row] = array_merge($data, $edit_index_button); + $dictionaryIndexFields["data"]["#rows"][0]["fields"][0] = array_merge($data["fields"][0], $edit_index_button); unset($dictionaryIndexFields['edit_index_buttons']['index_field_key_' . $row]); } elseif ($edit_index_fields) { @@ -32,23 +64,53 @@ public static function setIndexFieldsAjaxElements(array $dictionaryIndexFields) return $dictionaryIndexFields; } + /** + * Setting ajax elements. + */ + public static function setIndexFieldsEditAjaxElements(array $dictionaryIndexFields) { + if ($dictionaryIndexFields) { + //$dictionaryIndexFields["#rows"][0]["fields"][0] + foreach ($dictionaryIndexFields['data']['#rows'] as $row => $data) { + $edit_index_button = $dictionaryIndexFields["edit_index"]["index_key_0"]["fields"]["edit_index_field_buttons"]["index_field_key_0"]["edit_index_field_button"] ?? NULL; + $edit_index_fields = $dictionaryIndexFields["#rows"][0]["fields"][0] ?? NULL; + // Setting the ajax fields if they exsist. + if ($edit_index_button) { + $dictionaryIndexFields["data"]["#rows"][0]["fields"][0] = array_merge($data["fields"][0], $edit_index_button); + unset($dictionaryIndexFields['edit_index_field_buttons']['index_field_key_' . $row]); + } + if ($edit_index_fields) { + unset($dictionaryIndexFields['data']['#rows']['index_field_key_' . $row]); + $dictionaryIndexFields['#rows'][$row]['fields'] = $edit_index_fields; + $dictionaryIndexFields["#rows"][0]["field_collection"]["fields"]["index_fields"]["#rows"][0]; + // 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; + } + + + /** * Setting ajax elements. */ public static function setIndexAjaxElements(array $dictionaryIndexes) { foreach ($dictionaryIndexes['data']['#rows'] as $row => $data) { $edit_index_button = $dictionaryIndexes['edit_index_buttons']['index_key_' . $row] ?? NULL; - $edit_index_fields = $dictionaryIndexes['edit_index_fields']['index_key_' . $row] ?? NULL; + $edit_index = $dictionaryIndexes['edit_index']['index_key_' . $row] ?? NULL; // Setting the ajax fields if they exsist. if ($edit_index_button) { $dictionaryIndexes['data']['#rows'][$row] = array_merge($data, $edit_index_button); unset($dictionaryIndexes['edit_index_buttons']['index_key_' . $row]); } - elseif ($edit_index_fields) { + elseif ($edit_index) { unset($dictionaryIndexes['data']['#rows']['index_key_' . $row]); - $dictionaryIndexes['data']['#rows'][$row]['field_collection'] = $edit_index_fields; + $dictionaryIndexes['data']['#rows'][$row]['field_collection'] = $edit_index; // Remove the buttons so they don't show up twice. - unset($dictionaryIndexes['edit_index_fields']['index_key_' . $row]); + unset($dictionaryIndexes['edit_index']['index_key_' . $row]); ksort($dictionaryIndexes['data']['#rows']); } @@ -80,6 +142,12 @@ public static function processIndexFieldsDataResults($index_data_results, $curre $index_data_results = isset($current_index_fields) ? array_merge($current_index_fields, $data_index_fields_pre) : $data_index_fields_pre; } + if (isset($index_data_results) && $op === "add_new_index_field") { + //$index_data_results = isset($index_data_results) ? array_merge($index_data_results, $data_index_fields_pre) : $data_index_fields_pre; + $index_data_results = isset($index_data_results) ? $index_data_results : $data_index_fields_pre; + + } + return $index_data_results; } @@ -87,6 +155,9 @@ public static function processIndexFieldsDataResults($index_data_results, $curre * Cleaning the data up. */ public static function processIndexDataResults($index_results, $current_indexes, $index_values, $index_fields_data_results, $op) { + // if($op === 'edit_0') { + // $op = 'add_index'; + // }; if (isset($current_indexes)) { $index_results = $current_indexes; } @@ -154,6 +225,25 @@ public static function setAddIndexFormState($add_new_index, $element) { return $element; } + // /** + // * Create edit and update fields where needed. + // */ + // public static function createDictionaryIndexFieldOptions($op_index, $index_fields_data_results, $index_fields_being_modified, $element) { + // $current_index_fields = $index_fields_data_results ?? NULL; + // // Creating ajax buttons/fields to be placed in correct location later. + // foreach ($index_fields_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); + // } + // else { + // $element['edit_index_buttons']['index_field_key_' . $indexKey]['edit_index_button'] = IndexFieldButtons::editIndexFieldButtons('index_field_key_' . $indexKey); + // } + // } + // $element['add_row_button'] = IndexFieldButtons::addIndexFieldButton(); + + // return $element; + // } + /** * Create edit and update fields where needed. */ @@ -161,11 +251,32 @@ public static function createDictionaryIndexFieldOptions($op_index, $index_data_ $current_index_fields = $index_data_results ?? NULL; // 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)) { + + if ($index_data_results) { + $element['edit_index_fields']['index_field_key_' . $indexKey] = IndexFieldEditCreation::editIndexFields('index_field_key_' . $indexKey, $current_index_fields, $index_fields_being_modified); + } + else { + $element['edit_index_buttons']['index_field_key_' . $indexKey]['edit_index_field_button'] = IndexFieldButtons::editIndexButtons('index_field_key_' . $indexKey); + } + } + $element['add_row_button'] = IndexFieldButtons::addIndexFieldButton(); + + return $element; + } + + /** + * Create edit and update fields where needed. + */ + public static function createDictionaryIndexFieldEditOptions($op_index, $index_fields_data_results, $index_fields_being_modified, $element) { + $current_index_fields = $index_fields_data_results ?? NULL; + // Creating ajax buttons/fields to be placed in correct location later. + foreach ($index_fields_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); } else { - $element['edit_index_buttons']['index_field_key_' . $indexKey]['edit_index_button'] = IndexFieldButtons::editIndexButtons('index_field_key_' . $indexKey); + $element['edit_index_field_buttons']['index_field_key_' . $indexKey]['edit_index_field_button'] = IndexFieldButtons::editIndexFieldButtons('index_field_key_' . $indexKey); } } $element['add_row_button'] = IndexFieldButtons::addIndexFieldButton(); @@ -176,12 +287,14 @@ public static function createDictionaryIndexFieldOptions($op_index, $index_data_ /** * Create edit and update fields where needed. */ - public static function createDictionaryIndexOptions($op_index, $index_data_results, $index_fields_being_modified, $element) { - $current_indexes = $element['current_index']; + public static function createDictionaryIndexOptions($op_index, $index_data_results, $index_being_modified, $element) { + $current_indexes = $index_data_results ?? NULL; // Creating ajax buttons/fields to be placed in correct location later. foreach ($index_data_results as $indexKey => $data) { - 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); + if ($index_being_modified) { + 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, $element); + } } else { $element['edit_index_buttons']['index_key_' . $indexKey]['edit_index_button'] = IndexFieldButtons::editIndexButtons('index_key_' . $indexKey); @@ -205,4 +318,18 @@ public static function checkIndexEditingField($indexKey, $op_index, $index_field return FALSE; } } + + /** + * Return true if field is being edited. + */ + public static function checkIndexEditing($indexKey, $op_index, $index_being_modified) { + $action_list = IndexFieldOperations::editIndexActions(); + $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; + } + else { + return FALSE; + } + } } \ No newline at end of file diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php index 4a5dcac5e8..afd09698d7 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php @@ -18,4 +18,19 @@ public static function updateIndexFieldValues($field_index, $update_values, $cur 'length' => (int)$length, ]; } + + /** + * Return updated index field values after edit. + */ + public static function updateIndexValues($field_index, $update_values, $current_index) { + $description = $update_values["field_json_metadata"][0]["indexes"]["edit_index"]['index_key_' . $field_index]["description"]; + $type = $update_values["field_json_metadata"][0]["indexes"]["edit_index"]['index_key_' . $field_index]["type"]; + $fields = $current_index[$field_index]['fields']; + + return [ + 'description' => $description, + 'type' => $type, + 'fields' => $fields, + ]; + } } 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 c5ac9577f8..c9387847a9 100644 --- a/modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php +++ b/modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php @@ -42,6 +42,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen $current_index_fields = $form_state->get('current_index_fields'); $dictionary_fields_being_modified = $form_state->get("dictionary_fields_being_modified") ?? NULL; $index_fields_being_modified = $form_state->get("index_fields_being_modified") ?? NULL; + $index_being_modified = $form_state->get("index_being_modified") ?? NULL; $op = $form_state->getTriggeringElement()['#op'] ?? NULL; $field_json_metadata = !empty($items[0]->value) ? json_decode($items[0]->value, TRUE) : []; @@ -52,7 +53,6 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen $index_fields_results = $field_json_metadata["data"]["indexes"][0]["fields"] ?? []; $index_results = $field_json_metadata["data"]["indexes"] ?? []; - // Process data results $data_results = FieldOperations::processDataResults($data_results, $current_dictionary_fields, $dictionary_field_values, $op); $index_fields_data_results = IndexFieldOperations::processIndexFieldsDataResults($index_fields_results, $current_index_fields, $index_field_values, $op); $index_data_results = IndexFieldOperations::processIndexDataResults($index_results, $current_indexes, $index_values, $index_fields_data_results, $op); @@ -64,7 +64,10 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen // Add pre-render functions $element['dictionary_fields']['#pre_render'] = [[$this, 'preRenderForm']]; - $element['indexes']['#pre_render'] = [[$this, 'preRenderIndexForm']]; + $element['indexes']['#pre_render'] = [ + [$this, 'preRenderIndexForm'], + [$this, 'preRenderIndexFieldForm'], + ]; $element['indexes']['fields']['#pre_render'] = [[$this, 'preRenderIndexFieldForm']]; // Add data rows to display in tables @@ -77,12 +80,20 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen $element['dictionary_fields']['add_row_button']['#access'] = $dictionary_fields_being_modified == NULL ? TRUE : FALSE; // Create index fields/buttons for editing - $element['indexes'] = IndexFieldOperations::createDictionaryIndexOptions($op_index, $index_data_results, $index_fields_being_modified, $element['indexes']); - if ($index_field_values || $current_index_fields) { + $element['indexes'] = IndexFieldOperations::createDictionaryIndexOptions($op_index, $index_data_results, $index_being_modified, $element['indexes']); + //$element["indexes"]["fields"] = IndexFieldOperations::createDictionaryIndexFieldEditOptions($op_index, $index_fields_data_results, $index_fields_being_modified, $element["indexes"]["fields"]); + + if ($index_field_values || $current_index_fields || $index_being_modified) { $element["indexes"]["fields"] = IndexFieldOperations::createDictionaryIndexFieldOptions($op_index, $index_fields_data_results, $index_fields_being_modified, $element['indexes']['fields']); } + + // if ($index_being_modified) { + // $element['indexes']['fields']['data'] = IndexFieldCreation::createIndexFieldsEditDataRows($index_field_values, $current_index_fields, $index_fields_data_results, $form_state); + // } + $element['indexes']['fields']['add_row_button']['#access'] = $index_fields_being_modified == NULL ? TRUE : FALSE; - + $element['indexes']['add_row_button']['#access'] = $index_being_modified == NULL ? TRUE : FALSE; + // Get form entity $form_object = $form_state->getFormObject(); if (!($form_object instanceof EntityFormInterface)) { @@ -175,6 +186,15 @@ public function preRenderIndexFieldForm(array $indexFields) { return IndexFieldOperations::setIndexFieldsAjaxElements($indexFields); } + /** + * Prerender callback for the index field form. + * + * Moves the buttons into the table. + */ + public function preRenderIndexFieldEditForm(array $indexFields) { + return IndexFieldOperations::setIndexFieldsEditAjaxElements($indexFields); + } + /** * Prerender callback for the index form. * @@ -188,7 +208,7 @@ public function preRenderIndexForm(array $indexes) { * {@inheritdoc} */ public static function trustedCallbacks() { - return ['preRenderForm', 'preRenderIndexFieldForm', 'preRenderIndexForm']; + return ['preRenderForm', 'preRenderIndexFieldForm', 'preRenderIndexForm', 'preRenderIndexFieldEditForm']; } } diff --git a/modules/data_dictionary_widget/templates/custom-index-fields-table.html.twig b/modules/data_dictionary_widget/templates/custom-index-fields-table.html.twig index 1b401daf01..5a877069d5 100644 --- a/modules/data_dictionary_widget/templates/custom-index-fields-table.html.twig +++ b/modules/data_dictionary_widget/templates/custom-index-fields-table.html.twig @@ -19,7 +19,9 @@ {{ row.name }} {{ row.length }} + {{ row.edit_index_field_button }} {{ row.edit_index_button }} + {{ row.button }} {% endif %}