From c5acf6d3a5d391a12748f4b2d037914050bf4b64 Mon Sep 17 00:00:00 2001 From: Kaise Lafrai Date: Mon, 29 Apr 2024 17:23:04 -0400 Subject: [PATCH 1/5] 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 8abfc3c5d16ed303670f13194c0ff30d27cd948f Mon Sep 17 00:00:00 2001 From: Kaise Lafrai Date: Tue, 14 May 2024 13:35:41 -0400 Subject: [PATCH 2/5] 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 59fbb315332ce370c0c554ec5fa01a018b9844d8 Mon Sep 17 00:00:00 2001 From: Kaise Lafrai Date: Mon, 20 May 2024 14:17:47 -0400 Subject: [PATCH 3/5] 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 dbcf8d977bb40cc32ca0dc397a70f30130fc53e1 Mon Sep 17 00:00:00 2001 From: Kaise Lafrai Date: Fri, 24 May 2024 14:53:21 -0400 Subject: [PATCH 4/5] 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 743ec03417..ae47ee749c 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 792b96f94f12581acda70de74454602431fdce41 Mon Sep 17 00:00:00 2001 From: Robert Casey Date: Wed, 12 Jun 2024 16:58:35 -0400 Subject: [PATCH 5/5] Rename files and distinguish index iterator from index field iterator --- ...ldAddCreation.php => IndexAddCreation.php} | 12 +- ...IndexFieldButtons.php => IndexButtons.php} | 112 +++++++++++++----- ...xFieldCallbacks.php => IndexCallbacks.php} | 59 ++++++++- ...dexFieldCreation.php => IndexCreation.php} | 2 +- ...EditCreation.php => IndexEditCreation.php} | 34 ++++-- ...ieldOperations.php => IndexOperations.php} | 54 +++++---- .../{IndexFieldValues.php => IndexValues.php} | 2 +- .../FieldWidget/DataDictionaryWidget.php | 28 ++--- 8 files changed, 218 insertions(+), 85 deletions(-) rename modules/data_dictionary_widget/src/Indexes/{IndexFieldAddCreation.php => IndexAddCreation.php} (88%) rename modules/data_dictionary_widget/src/Indexes/{IndexFieldButtons.php => IndexButtons.php} (65%) rename modules/data_dictionary_widget/src/Indexes/{IndexFieldCallbacks.php => IndexCallbacks.php} (70%) rename modules/data_dictionary_widget/src/Indexes/{IndexFieldCreation.php => IndexCreation.php} (98%) rename modules/data_dictionary_widget/src/Indexes/{IndexFieldEditCreation.php => IndexEditCreation.php} (61%) rename modules/data_dictionary_widget/src/Indexes/{IndexFieldOperations.php => IndexOperations.php} (79%) rename modules/data_dictionary_widget/src/Indexes/{IndexFieldValues.php => IndexValues.php} (96%) diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php b/modules/data_dictionary_widget/src/Indexes/IndexAddCreation.php similarity index 88% rename from modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php rename to modules/data_dictionary_widget/src/Indexes/IndexAddCreation.php index f68636f772..631486af71 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexAddCreation.php @@ -5,7 +5,7 @@ /** * Various operations for creating Data Dictionary Widget add fields. */ -class IndexFieldAddCreation { +class IndexAddCreation { /** * Create add fields for Data Dictionary Widget. @@ -57,10 +57,10 @@ public static function addIndex() { ], ]; - $add_index['group']['index']['fields']['add_row_button'] = IndexFieldButtons::addIndexFieldButton(); + $add_index['group']['index']['fields']['add_row_button'] = IndexButtons::addIndexFieldButton(); - $add_index['group']['index']['save_index'] = IndexFieldButtons::submitIndexButton('add_index', NULL); - $add_index['group']['index']['cancel_index'] = IndexFieldButtons::cancelIndexButton('cancel_index', NULL); + $add_index['group']['index']['save_index'] = IndexButtons::submitIndexButton('add_index', NULL); + $add_index['group']['index']['cancel_index'] = IndexButtons::cancelIndexButton('cancel_index', NULL); return $add_index; } @@ -110,8 +110,8 @@ private static function createIndexFieldLengthField() { private static function createIndexActionFields($id) { return [ '#type' => 'actions', - 'save_index_settings' => IndexFieldButtons::submitIndexFieldButton('add', NULL), - 'cancel_index_settings' => IndexFieldButtons::cancelIndexFieldButton('cancel', NULL, $id), + 'save_index_settings' => IndexButtons::submitIndexFieldButton('add', NULL), + 'cancel_index_settings' => IndexButtons::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/IndexButtons.php similarity index 65% rename from modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php rename to modules/data_dictionary_widget/src/Indexes/IndexButtons.php index 28ab44c494..9e701cf2e2 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexButtons.php @@ -6,7 +6,7 @@ /** * Various operations for creating Data Dictionary Widget fields. */ -class IndexFieldButtons { +class IndexButtons { /** * Returns the add index field button. @@ -19,12 +19,12 @@ public static function addIndexFieldButton() { '#op' => 'add_new_index_field', '#submit' => [ [ - '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', - 'indexAddSubformCallback', + '\Drupal\data_dictionary_widget\Indexes\IndexCallbacks', + 'indexFieldAddSubformCallback', ], ], '#ajax' => [ - 'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexFormAjax', + 'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexCallbacks::subIndexFormAjax', 'wrapper' => 'field-json-metadata-dictionary-index-fields', 'effect' => 'fade', ], @@ -43,12 +43,12 @@ public static function addIndexButton() { '#op' => 'add_new_index', '#submit' => [ [ - '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + '\Drupal\data_dictionary_widget\Indexes\IndexCallbacks', 'indexAddCallback', ], ], '#ajax' => [ - 'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexFormAjax', + 'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexCallbacks::indexFormAjax', 'wrapper' => 'field-json-metadata-dictionary-indexes', 'effect' => 'fade', ], @@ -73,12 +73,12 @@ public static function editIndexButtons($indexKey) { ], '#submit' => [ [ - '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + '\Drupal\data_dictionary_widget\Indexes\IndexCallbacks', 'indexEditSubformCallback', ], ], '#ajax' => [ - 'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexFormAjax', + 'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexCallbacks::subIndexFormAjax', 'wrapper' => 'field-json-metadata-dictionary-index-fields', 'effect' => 'fade', ], @@ -88,25 +88,59 @@ public static function editIndexButtons($indexKey) { ]; } + /** + * Returns the edit Index Field Buttons. + */ + public static function editIndexFieldButtons($indexFieldKey) { + return [ + '#type' => 'image_button', + // '#name' => 'edit_index_field' . $indexKey . '_' . $indexFieldKey, + // '#id' => 'edit_index_field' . $indexKey . '_' . $indexFieldKey, + // '#access' => TRUE, + // '#op' => 'index_field_edit_' . $indexKey . '_' . $indexFieldKey, + '#name' => 'edit_index_field' . $indexFieldKey, + '#id' => 'edit_index_field' . $indexFieldKey, + '#access' => TRUE, + '#op' => 'index_field_edit_' . $indexFieldKey, + '#src' => 'core/misc/icons/787878/cog.svg', + '#attributes' => [ + 'class' => ['index-field-plugin-settings-edit'], + 'alt' => t('Edit index field'), + ], + '#submit' => [ + [ + '\Drupal\data_dictionary_widget\Indexes\IndexCallbacks', + 'indexFieldEditSubformCallback', + ], + ], + '#ajax' => [ + 'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexCallbacks::subIndexformAjax', + 'wrapper' => 'field-json-metadata-dictionary-index-fields', + 'effect' => 'fade', + ], + '#limit_validation_errors' => [], + ]; + } + /** * Create Submit buttons. */ - public static function submitIndexFieldButton($location, $indexKey) { + public static function submitIndexFieldButton($location, $indexFieldKey) { $callbackClass = $location == 'edit' ? 'indexEditSubformCallback' : 'indexAddSubformCallback'; - $op = !empty($indexKey) ? 'update_' . $indexKey : 'add_index_field'; + $op = !empty($indexFieldKey) ? 'update_' . $indexFieldKey : 'add_index_field'; $value = $location == 'edit' ? 'Save' : 'Add '; - $edit_index_button = [ + $edit_index_field_button = [ '#type' => 'submit', '#value' => $value, '#op' => $op, '#submit' => [ [ - '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + '\Drupal\data_dictionary_widget\Indexes\IndexCallbacks', $callbackClass, ], ], '#ajax' => [ - 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexFormAjax', + 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexCallbacks::subIndexFormAjax', 'wrapper' => 'field-json-metadata-dictionary-index-fields', 'effect' => 'fade', ], @@ -117,9 +151,9 @@ public static function submitIndexFieldButton($location, $indexKey) { ]; if ($location == 'edit') { - $edit_index_button['#name'] = 'update_' . $indexKey; + $edit_index_button['#name'] = 'update_' . $indexFieldKey; } - return $edit_index_button; + return $edit_index__field_button; } /** @@ -136,12 +170,12 @@ public static function submitIndexButton($location, $indexKey) { '#op' => $op, '#submit' => [ [ - '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + '\Drupal\data_dictionary_widget\Indexes\IndexCallbacks', $callbackClass, ], ], '#ajax' => [ - 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexFormAjax', + 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexCallbacks::indexFormAjax', 'wrapper' => 'field-json-metadata-dictionary-indexes', 'effect' => 'fade', ], @@ -160,22 +194,22 @@ public static function submitIndexButton($location, $indexKey) { /** * Create Cancel button. */ - public static function cancelIndexFieldButton($location, $indexKey, $id) { + public static function cancelIndexFieldButton($location, $indexFieldKey, $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'; + $callbackClass = $location == 'edit' ? 'indexFieldEditSubformCallback' : 'indexFieldAddSubformCallback'; + $op = $location == 'edit' && $indexFieldKey ? 'abort_' . $indexFieldKey : 'cancel_index_field'; $cancel_index_button = [ '#type' => 'submit', '#value' => t('Cancel'), '#op' => $op, '#submit' => [ [ - '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + '\Drupal\data_dictionary_widget\Indexes\IndexCallbacks', $callbackClass, ], ], '#ajax' => [ - 'callback' => "Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::$callbackId", + 'callback' => "Drupal\data_dictionary_widget\Indexes\IndexCallbacks::$callbackId", 'wrapper' => $id, 'effect' => 'fade', ], @@ -183,7 +217,7 @@ public static function cancelIndexFieldButton($location, $indexKey, $id) { ]; if ($location == 'edit') { - $cancel_index_button['#name'] = 'cancel_update_' . $indexKey; + $cancel_index_button['#name'] = 'cancel_update_' . $indexFieldKey; } return $cancel_index_button; } @@ -200,12 +234,12 @@ public static function cancelIndexButton($location, $indexKey) { '#op' => $op, '#submit' => [ [ - '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + '\Drupal\data_dictionary_widget\Indexes\IndexCallbacks', $callbackClass, ], ], '#ajax' => [ - 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexFormAjax', + 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexCallbacks::indexFormAjax', 'wrapper' => 'field-json-metadata-dictionary-indexes', 'effect' => 'fade', ], @@ -229,12 +263,36 @@ public static function deleteIndexButton($indexKey) { '#op' => 'delete_' . $indexKey, '#submit' => [ [ - '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + '\Drupal\data_dictionary_widget\Indexes\IndexCallbacks', 'indexEditSubformCallback', ], ], '#ajax' => [ - 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexFormAjax', + 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexCallbacks::subIndexFormAjax', + 'wrapper' => 'field-json-metadata-dictionary-index-fields', + 'effect' => 'fade', + ], + '#limit_validation_errors' => [], + ]; + } + + /** + * Create Delete Index Field button. + */ + public static function deleteIndexFieldButton($indexFieldKey) { + return [ + '#type' => 'submit', + '#name' => 'index_delete_' . $indexFieldKey, + '#value' => t('Delete index field'), + '#op' => 'delete_' . $indexFieldKey, + '#submit' => [ + [ + '\Drupal\data_dictionary_widget\Indexes\IndexCallbacks', + 'indexFieldEditSubformCallback', + ], + ], + '#ajax' => [ + 'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexCallbacks::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/IndexCallbacks.php similarity index 70% rename from modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php rename to modules/data_dictionary_widget/src/Indexes/IndexCallbacks.php index 0c46131c5c..5ef0ec2c14 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexCallbacks.php @@ -7,11 +7,11 @@ /** * Various operations for the Data Dictionary Widget callbacks. */ -class IndexFieldCallbacks { +class IndexCallbacks { /** * Submit callback for the Index Add button. */ - public static function indexAddSubformCallback(array &$form, FormStateInterface $form_state) { + public static function indexFieldAddSubformCallback(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"]; @@ -28,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($current_index_fields); + $add_index_fields = IndexAddCreation::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); @@ -71,7 +71,7 @@ public static function indexAddCallback(array &$form, FormStateInterface $form_s } if ($op === 'add_new_index') { - $add_new_index = IndexFieldAddCreation::addIndex(); + $add_new_index = IndexAddCreation::addIndex(); $form_state->set('new_index', ''); $form_state->set('add_new_index', $add_new_index); } @@ -115,7 +115,56 @@ public static function indexEditSubformCallback(array &$form, FormStateInterface $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 ); + $current_index_fields[$op_index[4]] = IndexValues::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(); + } + + /** + * Submit callback for the Index Field Edit button. + */ + public static function indexFieldEditSubformCallback(array &$form, FormStateInterface $form_state) { + // What button was clicked? + $trigger = $form_state->getTriggeringElement(); + // Set the 'operation' of the actioned button to a var + $op = $trigger['#op']; + // Get the portion of the operation string that's relevent + // so we can action on it + $op_index = explode("_", $trigger['#op']); + // Get our current (soft saved) data on the form for each main element + $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"]; + // Get the data that we're currently modifying (temporary) + // This differs from the current fields in that + // this is the data we are immediately editing + $currently_modifying = $form_state->get('dictionary_fields_being_modified') != NULL ? $form_state->get('dictionary_fields_being_modified') : []; + $currently_modifying_index_fields = $form_state->get('index_fields_being_modified') != NULL ? $form_state->get('index_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]] = IndexValues::updateIndexFieldValues($op_index[4], $update_values, $current_index_fields ); ksort($current_index_fields ); } diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php b/modules/data_dictionary_widget/src/Indexes/IndexCreation.php similarity index 98% rename from modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php rename to modules/data_dictionary_widget/src/Indexes/IndexCreation.php index 16740129b4..2bc766d5ec 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexCreation.php @@ -5,7 +5,7 @@ /** * Various operations for creating Data Dictionary Widget fields. */ -class IndexFieldCreation { +class IndexCreation { /** * Create basic widget. */ diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php b/modules/data_dictionary_widget/src/Indexes/IndexEditCreation.php similarity index 61% rename from modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php rename to modules/data_dictionary_widget/src/Indexes/IndexEditCreation.php index f4d6087195..7cd2f3c804 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexEditCreation.php @@ -6,27 +6,27 @@ * Various operations for creating Data Dictionary Widget add fields. */ -class IndexFieldEditCreation { +class IndexEditCreation { /** * Create edit fields for Data Dictionary Widget. */ - public static function editIndexFields($indexKey, $current_index_fields) { + public static function editIndexFields($indexFieldKey, $current_index_fields) { $id = $current_index_fields ? "field-json-metadata-dictionary-index-fields-new" : "field-json-metadata-dictionary-index-fields"; - $indexKeyExplode = explode("_", $indexKey); + $indexFieldKeyExplode = explode("_", $indexFieldKey); $edit_index_fields['name'] = [ - '#name' => 'field_json_metadata[0][fields][data][' . $indexKeyExplode[3] . '][field_collection][name]', + '#name' => 'field_json_metadata[0][fields][data][' . $indexFieldKeyExplode[3] . '][field_collection][name]', '#type' => 'textfield', - '#value' => $current_index_fields[$indexKeyExplode[3]]['name'], + '#value' => $current_index_fields[$indexFieldKeyExplode[3]]['name'], '#title' => 'Name', ]; $edit_index_fields['length'] = [ - '#name' => 'field_json_metadata[0][fields][data]['. $indexKeyExplode[3] .'][field_collection][length]', + '#name' => 'field_json_metadata[0][fields][data]['. $indexFieldKeyExplode[3] .'][field_collection][length]', '#type' => 'number', - '#value' => $current_index_fields[$indexKeyExplode[3]]['length'], + '#value' => $current_index_fields[$indexFieldKeyExplode[3]]['length'], '#title' => 'Length', ]; - $edit_index_fields['update_index_field']['actions'] = self::createIndexActionFields($indexKey, $id); + $edit_index_fields['update_index_field']['actions'] = self::createIndexActionFields($indexFieldKey, $id); return $edit_index_fields; } @@ -61,9 +61,21 @@ public static function editIndex($indexKey, $current_index) { private static function createIndexActionFields($indexKey, $id) { return [ '#type' => 'actions', - 'save_update' => IndexFieldButtons::submitIndexFieldButton('edit', $indexKey), - 'cancel_updates' => IndexFieldButtons::cancelIndexFieldButton('edit', $indexKey, $id), - 'delete_field' => IndexFieldButtons::deleteIndexButton($indexKey), + 'save_update' => IndexButtons::submitIndexFieldButton('edit', $indexKey), + 'cancel_updates' => IndexButtons::cancelIndexFieldButton('edit', $indexKey, $id), + 'delete_field' => IndexButtons::deleteIndexButton($indexKey), + ]; + } + + /** + * Create Index Field Action buttons. + */ + private static function createIndexFieldActionFields($indexFieldKey) { + return [ + '#type' => 'actions', + 'save_update_index_field' => IndexButtons::submitIndexFieldButton('edit', $indexFieldKey), + 'cancel_update_index_field' => IndexButtons::cancelIndexFieldButton('edit', $indexFieldKey), + 'delete_index_field' => IndexButtons::deleteIndexFieldButton($indexFieldKey), ]; } diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php b/modules/data_dictionary_widget/src/Indexes/IndexOperations.php similarity index 79% rename from modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php rename to modules/data_dictionary_widget/src/Indexes/IndexOperations.php index 019b3216ba..f12cc8c630 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexOperations.php @@ -5,19 +5,19 @@ /** * Various operations for the Data Dictionary Widget. */ -class IndexFieldOperations { +class IndexOperations { /** * 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_field_button = $dictionaryIndexFields['edit_index__field_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 ($edit_index_field_button) { + $dictionaryIndexFields['data']['#rows'][$row] = array_merge($data, $edit_index_field_button); + unset($dictionaryIndexFields['edit_index_field_buttons']['index_field_key_' . $row]); } elseif ($edit_index_fields) { unset($dictionaryIndexFields['data']['#rows']['index_field_key_' . $row]); @@ -160,37 +160,37 @@ 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; + 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_index_fields, $index_fields_being_modified); + if (self::checkIndexEditingField('index_key_' . $indexKey, $op_index, $index_fields_being_modified)) { + $element['edit_index']['index_key_' . $indexKey] = IndexEditCreation::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'] = IndexButtons::editIndexButtons('index_key_' . $indexKey); } } - $element['add_row_button'] = IndexFieldButtons::addIndexFieldButton(); + $element['add_row_button'] = IndexButtons::addIndexButton(); return $element; } /** - * Create edit and update fields where needed. + * Create edit and update fields for index fields (children) where needed. */ - public static function createDictionaryIndexOptions($op_index, $index_data_results, $index_fields_being_modified, $element) { - $current_indexes = $element['current_index']; + 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_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); + foreach ($index_data_results as $indexFieldKey => $data) { + if (self::checkIndexFieldEditingField('index_field_key_' . $indexFieldKey, $op_index, $index_fields_being_modified)) { + $element['edit_index_fields']['index_field_key_' . $indexFieldKey] = IndexEditCreation::editIndexFields('index_field_key_' . $indexFieldKey, $current_index_fields, $index_fields_being_modified); } else { - $element['edit_index_buttons']['index_key_' . $indexKey]['edit_index_button'] = IndexFieldButtons::editIndexButtons('index_key_' . $indexKey); + $element['edit_index_field_buttons']['index_field_key_' . $indexFieldKey]['edit_index_field_button'] = IndexButtons::editIndexFieldButtons('index_field_key_' . $indexFieldKey); } } - $element['add_row_button'] = IndexFieldButtons::addIndexButton(); + $element['add_row_button'] = IndexButtons::addIndexFieldButton(); return $element; } @@ -199,7 +199,7 @@ public static function createDictionaryIndexOptions($op_index, $index_data_resul * Return true if field is being edited. */ public static function checkIndexEditingField($indexKey, $op_index, $index_fields_being_modified) { - $action_list = IndexFieldOperations::editIndexActions(); + $action_list = IndexOperations::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; @@ -208,4 +208,18 @@ public static function checkIndexEditingField($indexKey, $op_index, $index_field return FALSE; } } + + /** + * Return true if field is being edited. + */ + public static function checkIndexFieldEditingField($indexFieldKey, $op_index, $index_fields_being_modified) { + $action_list = IndexOperations::editIndexActions(); + $indexFieldKeyExplode = explode("_", $indexFieldKey); + if (isset($op_index[0]) && in_array($op_index[0], $action_list) && array_key_exists($indexFieldKeyExplode[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/IndexValues.php similarity index 96% rename from modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php rename to modules/data_dictionary_widget/src/Indexes/IndexValues.php index b3c881a3ec..01ecd16011 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexValues.php @@ -5,7 +5,7 @@ /** * Various operations for creating Data Dictionary Widget fields. */ -class IndexFieldValues { +class IndexValues { /** * Return updated index field values after edit. */ 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..e8f963de32 100644 --- a/modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php +++ b/modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php @@ -10,8 +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; +use Drupal\data_dictionary_widget\Indexes\IndexCreation; +use Drupal\data_dictionary_widget\Indexes\IndexOperations; /** * A data-dictionary widget. @@ -54,13 +54,13 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen // 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); + $index_fields_data_results = IndexOperations::processIndexFieldsDataResults($index_fields_results, $current_index_fields, $index_field_values, $op); + $index_data_results = IndexOperations::processIndexDataResults($index_results, $current_indexes, $index_values, $index_fields_data_results, $op); // 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 = IndexCreation::createGeneralIndex($element, $current_indexes); + $element = IndexCreation::createGeneralIndexFields($element); // Add pre-render functions $element['dictionary_fields']['#pre_render'] = [[$this, 'preRenderForm']]; @@ -69,17 +69,17 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen // 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_field_values, $current_index_fields, $index_fields_data_results, $form_state); + $element['indexes']['data'] = IndexCreation::createIndexDataRows($current_indexes, $index_data_results, $form_state); + $element['indexes']['fields']['data'] = IndexCreation::createIndexFieldsDataRows($index_field_values, $current_index_fields, $index_fields_data_results, $form_state); // 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']); + $element['indexes'] = IndexOperations::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"] = IndexOperations::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; @@ -97,8 +97,8 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen // 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); + $element = IndexOperations::setAddIndexFormState($add_new_index, $element); + $element = IndexOperations::setAddIndexFieldFormState($add_index_field, $element); // Display index fields only when new index fields are being created. if ($add_index_field || $index_field_values) { @@ -172,7 +172,7 @@ public function preRenderForm(array $dictionaryFields) { * Moves the buttons into the table. */ public function preRenderIndexFieldForm(array $indexFields) { - return IndexFieldOperations::setIndexFieldsAjaxElements($indexFields); + return IndexOperations::setIndexFieldsAjaxElements($indexFields); } /** @@ -181,7 +181,7 @@ public function preRenderIndexFieldForm(array $indexFields) { * Moves the buttons into the table. */ public function preRenderIndexForm(array $indexes) { - return IndexFieldOperations::setIndexAjaxElements($indexes); + return IndexOperations::setIndexAjaxElements($indexes); } /**