From 1160845ea317d4aebf8e7897c5970b8a7634c21f Mon Sep 17 00:00:00 2001 From: Daniel Mundra Date: Thu, 5 Sep 2024 08:42:02 -0700 Subject: [PATCH] Some simple code cleanup (white space, removal of commented out code, arrays, comment updates). --- .../src/Fields/FieldCallbacks.php | 12 +- .../src/Fields/FieldOperations.php | 4 +- .../src/Indexes/IndexFieldAddCreation.php | 112 +++++++++--------- .../src/Indexes/IndexFieldButtons.php | 102 +++++++++++++--- .../src/Indexes/IndexFieldCallbacks.php | 23 ++-- .../src/Indexes/IndexFieldCreation.php | 6 +- .../src/Indexes/IndexFieldEditCreation.php | 6 +- .../src/Indexes/IndexFieldOperations.php | 24 ++-- .../src/Indexes/IndexFieldValues.php | 6 +- .../src/Indexes/IndexValidation.php | 16 +-- .../FieldWidget/DataDictionaryWidget.php | 34 +++--- 11 files changed, 212 insertions(+), 133 deletions(-) diff --git a/modules/data_dictionary_widget/src/Fields/FieldCallbacks.php b/modules/data_dictionary_widget/src/Fields/FieldCallbacks.php index d0784d0f58..ce3ad613fc 100644 --- a/modules/data_dictionary_widget/src/Fields/FieldCallbacks.php +++ b/modules/data_dictionary_widget/src/Fields/FieldCallbacks.php @@ -39,16 +39,16 @@ public static function updateFormatOptions(array &$form, FormStateInterface $for * Submit callback for the Edit button. */ public static function editSubformCallback(array &$form, FormStateInterface $form_state) { - // Get the current fields data + // 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"]["fields"]["data"]["#rows"] ?? []; // Get the field index from the triggering op attribute - // so we can use it to store the respective field later + // so we can use it to store the respective field later. $op_index = explode("_", $form_state->getTriggeringElement()['#op']); - // Get the fields we're currently modifying + // 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, + // If the op (trigger) contains abort, // we're canceling the field we're currently modifying so unset it. if (str_contains($form_state->getTriggeringElement()['#op'], 'abort')) { unset($dictionary_fields_being_modified[$op_index[1]]); @@ -86,10 +86,10 @@ public static function editSubformCallback(array &$form, FormStateInterface $for // 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, + // 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 ); + $form_state->set('current_index_fields', $current_index_fields); // Let's rebuild the form. $form_state->setRebuild(); } diff --git a/modules/data_dictionary_widget/src/Fields/FieldOperations.php b/modules/data_dictionary_widget/src/Fields/FieldOperations.php index 40875fb8ae..d3875cb36e 100644 --- a/modules/data_dictionary_widget/src/Fields/FieldOperations.php +++ b/modules/data_dictionary_widget/src/Fields/FieldOperations.php @@ -214,7 +214,7 @@ public static function setAddDictionaryFieldFormState($add_new_field, $element) public static function editDictionaryFieldFormState($edit_dictionary_field, $element) { if ($edit_dictionary_field) { unset($element['dictionary_fields']["edit_buttons"]); - } + } return $element; } @@ -379,4 +379,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 0e0a65f3e1..cdf2517f04 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php @@ -7,61 +7,64 @@ */ class IndexFieldAddCreation { - /** - * Create fields for adding an index. - */ - public static function addIndex() { - $add_index['#access'] = FALSE; - $add_index['group'] = [ - '#type' => 'fieldset', - '#title' => t('Index'), - '#open' => TRUE, - '#prefix' => '
', - '#suffix' => '
', - '#element_validate' => [ - ['\Drupal\data_dictionary_widget\Indexes\IndexValidation', 'indexFieldsValidation'] - ], - ]; + /** + * Create fields for adding an index. + */ + public static function addIndex() { + $add_index['#access'] = FALSE; + $add_index['group'] = [ + '#type' => 'fieldset', + '#title' => t('Index'), + '#open' => TRUE, + '#prefix' => '
', + '#suffix' => '
', + '#element_validate' => [ + [ + '\Drupal\data_dictionary_widget\Indexes\IndexValidation', + 'indexFieldsValidation', + ], + ], + ]; - $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']['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']['index']['type'] = [ - '#name' => 'field_json_metadata[0][indexes][field_collection][group][index][type]', - '#type' => 'select', - '#description' => t('Index type.'), - '#title' => 'Index Type', - '#default_value' => 'index', - '#op' => 'index_type', - '#required' => TRUE, - '#options' => [ - 'index' => t('index'), - 'fulltext' => t('fulltext'), - ], - ]; + $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', + '#default_value' => 'index', + '#op' => 'index_type', + '#required' => TRUE, + '#options' => [ + 'index' => t('index'), + 'fulltext' => t('fulltext'), + ], + ]; + + $add_index['group']['index']['fields'] = [ + '#type' => 'fieldset', + '#title' => t('Fields'), + '#prefix' => '
', + '#suffix' => '
', + '#markup' => t('
One or more fields included in index. Must be keys from the fields object.
'), + '#attributes' => [ + 'class' => ['index-fields-form'], + ], + ]; - $add_index['group']['index']['fields'] = [ - '#type' => 'fieldset', - '#title' => t('Fields'), - '#prefix' => '
', - '#suffix' => '
', - '#markup' => t('
One or more fields included in index. Must be keys from the fields object.
'), - '#attributes' => [ - 'class' => ['index-fields-form'], - ], - ]; - - $add_index['group']['index']['fields']['add_row_button'] = IndexFieldButtons::addIndexFieldButton(); - $add_index['group']['index']['save_index'] = IndexFieldButtons::submitIndexButton('add_index', NULL); - $add_index['group']['index']['cancel_index'] = IndexFieldButtons::cancelIndexButton('cancel_index', NULL); - - return $add_index; - } + $add_index['group']['index']['fields']['add_row_button'] = IndexFieldButtons::addIndexFieldButton(); + $add_index['group']['index']['save_index'] = IndexFieldButtons::submitIndexButton('add_index', NULL); + $add_index['group']['index']['cancel_index'] = IndexFieldButtons::cancelIndexButton('cancel_index', NULL); + + return $add_index; + } /** * Create fields for adding an index field. @@ -86,7 +89,7 @@ public static function addIndexFields($current_index_fields) { $add_index_fields['group']['index']['fields']['length'] = self::createIndexFieldLengthField(); $add_index_fields['group']['index']['fields']['actions'] = self::createIndexActionFields($id); - + return $add_index_fields; } @@ -112,4 +115,5 @@ private static function createIndexActionFields($id) { '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 12dd904c84..18afd6ec3d 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php @@ -18,10 +18,10 @@ public static function addIndexFieldButton() { '#access' => TRUE, '#op' => 'add_new_index_field', '#submit' => [ - [ - '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', - 'indexAddSubformCallback', - ], + [ + '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + 'indexAddSubformCallback', + ], ], '#ajax' => [ 'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexFormAjax', @@ -65,7 +65,8 @@ public static function editIndexButtons($indexKey) { $callback = 'indexEditSubformCallback'; $id = 'field-json-metadata-index-fields'; $function = 'subIndexFormAjax'; - } else { + } + else { $callback = 'indexEditCallback'; $id = 'field-json-metadata-index'; $function = 'indexFormAjax'; @@ -82,10 +83,10 @@ public static function editIndexButtons($indexKey) { 'alt' => t('Edit index'), ], '#submit' => [ - [ - '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', - $callback, - ], + [ + '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + $callback, + ], ], '#ajax' => [ 'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::' . $function, @@ -93,7 +94,15 @@ public static function editIndexButtons($indexKey) { 'effect' => 'fade', ], '#limit_validation_errors' => [ - ['field_json_metadata', 0, 'indexes', 'field_collection', 'group', 'index', 'type'], + [ + 'field_json_metadata', + 0, + 'indexes', + 'field_collection', + 'group', + 'index', + 'type', + ], ], ]; } @@ -122,11 +131,34 @@ public static function submitIndexFieldButton($location, $indexKey) { 'effect' => 'fade', ], '#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'], + [ + 'field_json_metadata', + 0, + 'indexes', + 'fields', + 'field_collection', + 'group', + 'index', + 'fields', + 'name', + ], + [ + 'field_json_metadata', + 0, + 'indexes', + 'fields', + 'field_collection', + 'group', + 'index', + 'fields', + 'length', + ], ], '#element_validate' => [ - ['Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', 'indexFieldVal'], + [ + 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks', + 'indexFieldVal', + ], ], ]; @@ -134,8 +166,24 @@ public static function submitIndexFieldButton($location, $indexKey) { $indexKeyExplode = explode("_", $indexKey); $edit_index_button['#name'] = 'update_' . $indexKey; $edit_index_button['#limit_validation_errors'] = [ - ['field_json_metadata', 0, 'indexes', 'fields', 'edit_index_fields', $indexKeyExplode[3], 'name'], - ['field_json_metadata', 0, 'indexes', 'fields', 'edit_index_fields', $indexKeyExplode[3], 'length'], + [ + 'field_json_metadata', + 0, + 'indexes', + 'fields', + 'edit_index_fields', + $indexKeyExplode[3], + 'name', + ], + [ + 'field_json_metadata', + 0, + 'indexes', + 'fields', + 'edit_index_fields', + $indexKeyExplode[3], + 'length', + ], ]; } return $edit_index_button; @@ -145,7 +193,6 @@ 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'; @@ -165,8 +212,24 @@ public static function submitIndexButton($location, $indexKey) { 'effect' => 'fade', ], '#limit_validation_errors' => [ - ['field_json_metadata', 0, 'indexes', 'field_collection', 'group', 'index', 'description'], - ['field_json_metadata', 0, 'indexes', 'field_collection', 'group', 'index', 'fields'], + [ + 'field_json_metadata', + 0, + 'indexes', + 'field_collection', + 'group', + 'index', + 'description', + ], + [ + 'field_json_metadata', + 0, + 'indexes', + 'field_collection', + 'group', + 'index', + 'fields', + ], ], ]; @@ -286,4 +349,5 @@ public static function deleteIndexButton($indexKey) { '#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 index 2e007a92aa..9feb5dd8e5 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php @@ -116,8 +116,8 @@ 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 ); - ksort($current_index_fields ); + $current_index_fields[$op_index[4]] = IndexFieldValues::updateIndexFieldValues($op_index[4], $update_values, $current_index_fields); + ksort($current_index_fields); } @@ -131,8 +131,8 @@ public static function indexEditSubformCallback(array &$form, FormStateInterface $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->set('current_index_fields', $current_index_fields); + $form_state->set('current_dictionary_fields', $current_dictionary_fields); $form_state->setRebuild(); } @@ -172,7 +172,6 @@ public static function indexEditCallback(array &$form, FormStateInterface $form_ $update_values = $form_state->getUserInput(); $current_index[$op_index[3]] = IndexFieldValues::updateIndexValues($op_index[3], $update_values, $current_index); unset($currently_modifying_index[$op_index[3]]); - //unset($current_index[$op_index[3]]); ksort($current_index); } @@ -183,7 +182,7 @@ public static function indexEditCallback(array &$form, FormStateInterface $form_ $form_state->set('dictionary_fields_being_modified', $currently_modifying_dictionary_fields); $form_state->set('index_fields_being_modified', $currently_modifying_index_fields); $form_state->set('index_being_modified', $currently_modifying_index); - $form_state->set('current_index_fields', $current_index_fields ); + $form_state->set('current_index_fields', $current_index_fields); $form_state->set('current_index', $current_index); $form_state->set('current_dictionary_fields', $current_dictionary_fields); $form_state->setRebuild(); @@ -201,7 +200,6 @@ public static function subIndexEditFormAjax(array &$form, FormStateInterface $fo */ public static function subIndexFormAjax(array &$form, FormStateInterface $form_state) { return $form["field_json_metadata"]["widget"][0]["indexes"]["fields"]; - //return $form["field_json_metadata"]["widget"][0]["indexes"]["edit_index"]["index_key_0"]["group"]["fields"]["fields"]; } /** @@ -210,7 +208,8 @@ public static function subIndexFormAjax(array &$form, FormStateInterface $form_s 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. + // 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; @@ -227,11 +226,12 @@ public static function subIndexFormFieldAjax(array &$form, FormStateInterface $f } /** - * Ajax callback to return index fields fieldset with existing fields and Add Field button. + * Ajax callback to return index fields fieldset with existing fields and Add + * Field button. */ public static function subIndexFormExistingFieldAjax(array &$form, FormStateInterface $form_state) { $form["field_json_metadata"]["widget"][0]["indexes"]["field_collection"]["group"]["index"]["fields"]["add_row_button"]['#access'] = TRUE; - return $form["field_json_metadata"]["widget"][0]["indexes"]["field_collection"]["group"]["index"]["fields"]["add_row_button"]; + return $form["field_json_metadata"]["widget"][0]["indexes"]["field_collection"]["group"]["index"]["fields"]["add_row_button"]; } /** @@ -244,7 +244,8 @@ public static function indexFieldVal($element, FormStateInterface &$form_state, ]; foreach ($fields_to_validate as $field_key => $field_label) { - IndexValidation::indexFieldVal($form_state, $field_key, $field_label, $form); + IndexValidation::indexFieldVal($form_state, $field_key, $field_label); } } + } diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php index a928ca590b..77622f6744 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php @@ -6,6 +6,7 @@ * Various operations for creating Index fields. */ class IndexFieldCreation { + /** * Create basic index fields fieldset. */ @@ -33,7 +34,7 @@ public static function createGeneralIndex($element, $current_indexes) { '#suffix' => '', '#markup' => t('
Adding indexes to your datastore tables can improve response times from common queries.
'), ]; - + $element['indexes']['current_index'] = $current_indexes; return $element; @@ -70,4 +71,5 @@ public static function createIndexDataRows($current_indexes, $index_data_results '#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 4cd203b928..fa88f2149c 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php @@ -5,8 +5,8 @@ /** * Various operations for creating edit fields for indexes. */ - class IndexFieldEditCreation { + /** * Create edit index fields. */ @@ -69,7 +69,7 @@ public static function editIndex($indexKey, $current_index, $index_being_modifie '#markup' => t('
One or more fields included in index. Must be keys from the fields object.
'), '#attributes' => [ 'class' => ['index-fields-form'], - ], + ], ]; $edit_index['group']['fields']['data'] = IndexFieldCreation::createIndexFieldsDataRows($current_index[$indexKeyExplode[2]]['fields'], $current_index[$indexKeyExplode[2]]['fields'], $current_index[$indexKeyExplode[2]]['fields'], $form_state); @@ -103,4 +103,4 @@ private static function createIndexFieldsActionFields($indexKey, $id) { ]; } -} \ 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 index e6529c669d..4a682a31a7 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php @@ -6,6 +6,7 @@ * Various operations for structuring indexes and fields before rendering. */ class IndexFieldOperations { + /** * Setting ajax elements when editing newly added index fields. */ @@ -14,7 +15,7 @@ public static function setIndexFieldsAjaxElementsOnAdd(array $indexFields) { foreach ($indexFields['data']['#rows'] as $row => $data) { $edit_index_button = $indexFields['edit_index_buttons']['index_field_key_' . $row] ?? NULL; $edit_index_fields = $indexFields['edit_index_fields']['index_field_key_' . $row] ?? NULL; - // Setting the ajax fields if they exsist. + // Setting the ajax fields if they exist. if ($edit_index_button) { $indexFields['data']['#rows'][$row] = array_merge($data, $edit_index_button); unset($indexFields['edit_index_buttons']['index_field_key_' . $row]); @@ -40,9 +41,8 @@ public static function setIndexFieldsAjaxElements(array $indexFields) { foreach ($indexFields['data']['#rows'] as $row => $data) { $edit_index_fields_button = $indexFields['fields']['edit_index_fields_buttons']['index_field_key_' . $row] ?? NULL; $edit_index_fields = $indexFields['fields']['edit_index_fields']['index_field_key_' . $row] ?? NULL; - // Setting the ajax fields if they exsist. + // Setting the ajax fields if they exist. if ($edit_index_fields_button) { - //$indexFields["data"]["#rows"][0] = array_merge($data, $edit_index_fields_button); $indexFields["data"]["#rows"][$row] = array_merge($data, $edit_index_fields_button); unset($indexFields["fields"]["edit_index_fields_buttons"]['index_field_key_' . $row]); } @@ -66,7 +66,7 @@ public static function setIndexAjaxElements(array $indexes) { foreach ($indexes['data']['#rows'] as $row => $data) { $edit_index_button = $indexes['edit_index_buttons']['index_key_' . $row] ?? NULL; $edit_index = $indexes['edit_index']['index_key_' . $row] ?? NULL; - // Setting the ajax fields if they exsist. + // Setting the ajax fields if they exist. if ($edit_index_button) { $indexes['data']['#rows'][$row] = array_merge($data, $edit_index_button); unset($indexes['edit_index_buttons']['index_key_' . $row]); @@ -98,7 +98,7 @@ public static function processIndexFieldsDataResults($index_data_results, $curre $data_index_fields_pre = [ [ "name" => $index_field_group['index']['fields']["name"], - "length" => (int)$index_field_group['index']['fields']["length"], + "length" => (int) $index_field_group['index']['fields']["length"], ], ]; } @@ -156,7 +156,7 @@ public static function editIndexActions() { public static function editIndexFormState($edit_index, $element) { if ($edit_index) { unset($element["indexes"]["edit_index_buttons"]); - } + } return $element; } @@ -172,13 +172,13 @@ public static function setAddIndexFieldFormState($add_new_index_field, $element) $element['indexes']['fields']['add_row_button']['#access'] = FALSE; $element['identifier']['#required'] = FALSE; $element['title']['#required'] = FALSE; - } + } return $element; } /** - * Set the elements associated with adding a new index + * Set the elements associated with adding a new index. */ public static function setAddIndexFormState($add_new_index, $element) { if ($add_new_index) { @@ -204,7 +204,6 @@ public static function createIndexFieldOptions($op_index, $index_data_results, $ $element['edit_index_fields']['index_field_key_' . $indexKey] = IndexFieldEditCreation::editIndexFields('index_field_key_' . $indexKey, $current_index_fields, $index_fields_being_modified); } else { - //$element['edit_index_fields_buttons']['index_field_key_' . $indexKey]['edit_index_fields_button'] = IndexFieldButtons::editIndexButtons('index_field_key_' . $indexKey); $element['edit_index_buttons']['index_field_key_' . $indexKey]['edit_index_button'] = IndexFieldButtons::editIndexButtons('index_field_key_' . $indexKey); } } @@ -238,7 +237,7 @@ public static function createIndexOptions($op_index, $index_data_results, $index */ public static function checkIndexEditingField($indexKey, $op_index, $index_fields_being_modified) { $action_list = IndexFieldOperations::editIndexActions(); - $indexKeyExplode = explode("_", $indexKey); + $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; } @@ -255,7 +254,7 @@ public static function checkIndexEditing($indexKey, $op_index, $index_being_modi $op_index_string = implode('_', $op_index); if (str_contains($op_index_string, 'edit_index_key')) { $action_list = IndexFieldOperations::editIndexActions(); - $indexKeyExplode = explode("_", $indexKey); + $indexKeyExplode = explode("_", $indexKey); if (isset($op_index[0]) && in_array($op_index[0], $action_list) && array_key_exists($indexKeyExplode[2], $index_being_modified)) { return TRUE; } @@ -264,4 +263,5 @@ public static function checkIndexEditing($indexKey, $op_index, $index_being_modi return FALSE; } -} \ No newline at end of file + +} diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php index 66cb7840c6..a217d7a7b2 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php @@ -6,16 +6,17 @@ * Various operations for returning index values when editing the field. */ class IndexFieldValues { + /** * Return updated index field values after edit. */ public static function updateIndexFieldValues($field_index, $update_values, $current_index_fields) { $name = $update_values["field_json_metadata"][0]["indexes"]["fields"]["edit_index_fields"][$field_index]["name"]; $length = $update_values["field_json_metadata"][0]["indexes"]["fields"]["edit_index_fields"][$field_index]["length"]; - + return [ 'name' => $name, - 'length' => (int)$length, + 'length' => (int) $length, ]; } @@ -33,4 +34,5 @@ public static function updateIndexValues($field_index, $update_values, $current_ 'fields' => $fields, ]; } + } diff --git a/modules/data_dictionary_widget/src/Indexes/IndexValidation.php b/modules/data_dictionary_widget/src/Indexes/IndexValidation.php index 50d7c4187f..253178a435 100644 --- a/modules/data_dictionary_widget/src/Indexes/IndexValidation.php +++ b/modules/data_dictionary_widget/src/Indexes/IndexValidation.php @@ -10,7 +10,7 @@ class IndexValidation { /** - * Validation callback for a index fields form. + * Validation callback for an index fields form. * * If index field name and length is empty, validation will trigger. * @@ -26,23 +26,23 @@ public static function indexFieldsValidation(array $element, FormStateInterface $fields = $form["field_json_metadata"]["widget"][0]["indexes"]["fields"]["data"]["#rows"] ?? NULL; if ($index_fields_fieldset && !$fields) { - $form_state->setError($index_fields_fieldset, t('At least one index field is required.')); + $form_state->setError($index_fields_fieldset, t('At least one index field is required.')); } } /** - * Validation callback for a index fields edit form. + * Validation callback for an index fields edit form. * * If index field name and length is empty on edit, validation will trigger. * * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. - * @param array $field_key + * @param string $field_key * The key value for the field being edited. - * @param array $field_label + * @param string $field_label * The label for the field being edited. */ - public static function indexFieldVal(FormStateInterface $form_state, $field_key, $field_label) { + public static function indexFieldVal(FormStateInterface $form_state, string $field_key, string $field_label) { $trigger = $form_state->getTriggeringElement(); $op = $trigger['#op']; $op_index = explode("_", $op); @@ -53,7 +53,9 @@ public static function indexFieldVal(FormStateInterface $form_state, $field_key, $value = $update_values["field_json_metadata"][0]["indexes"]["fields"]["edit_index_fields"][$op_index[4]][$field_key]; if ($value === "") { $field = "field_json_metadata][0][indexes][fields][edit_index_fields][$op_index[4]][$field_key"; - $form_state->setErrorByName($field, t($field_label . ' field is required.')); + $form_state->setErrorByName($field, t(':field_label field is required.', [ + ':field_label' => $field_label, + ])); } } } 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 8fc0837e3a..739506a311 100644 --- a/modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php +++ b/modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php @@ -75,7 +75,12 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen // Add pre-render functions. $element['dictionary_fields']['#pre_render'] = [[$this, 'preRenderForm']]; $element['indexes']['#pre_render'] = [[$this, 'preRenderIndexForm']]; - $element['indexes']['fields']['#pre_render'] = [[$this, 'preRenderIndexFieldFormOnAdd']]; + $element['indexes']['fields']['#pre_render'] = [ + [ + $this, + 'preRenderIndexFieldFormOnAdd', + ], + ]; // Add data rows to display in tables. $element['dictionary_fields']['data'] = FieldCreation::createDictionaryDataRows($current_dictionary_fields, $data_results, $form_state); @@ -85,10 +90,9 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen // 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::createIndexOptions($op_index, $index_data_results, $index_being_modified, $index_fields_being_modified, $element['indexes'], $form_state); - //$element['indexes'] = IndexFieldOperations::createIndexOptions($op_index, $index_data_results, $index_fields_being_modified, $element['indexes'], $form_state); // Create edit buttons and fields for index fields. if ($index_field_values || $current_index_fields) { @@ -98,25 +102,19 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen // Set access to index fields Add button when index fields being modified. $element['indexes']['add_row_button']['#access'] = $index_being_modified == NULL ? TRUE : FALSE; - // if ($index_field_values || $current_index_fields || $index_being_modified || $index_fields_being_modified) { - // $element["indexes"]["edit_index"]["index_key_0"]["group"]["fields"]["fields"] = IndexFieldOperations::createDictionaryIndexFieldOptions($op_index, $index_fields_data_results, $index_fields_being_modified, $element['indexes']['fields']); - // $element["indexes"]["edit_index"]["index_key_0"]["group"]["fields"]['#pre_render'] = [[$this, 'preRenderIndexFieldForm']]; - // //$element["indexes"]['edit_index_fields']["index_field_key_0"]['#pre_render'] = [[$this, 'preRenderIndexFieldForm']]; - // } - - // Get form entity + // Get form entity. $form_object = $form_state->getFormObject(); if (!($form_object instanceof EntityFormInterface)) { return; } $form_entity = $form_object->getEntity(); - // Set form entity data type + // Set form entity data type. if ($form_entity instanceof FieldableEntityInterface) { $form_entity->set('field_data_type', 'data-dictionary'); } - // Set form state for adding fields and indexes + // Set form state for adding fields and indexes. $element = FieldOperations::setAddDictionaryFieldFormState($add_new_dictionary_field, $element); $element = FieldOperations::editDictionaryFieldFormState($dictionary_fields_being_modified, $element); $element = IndexFieldOperations::setAddIndexFormState($add_new_index, $element); @@ -126,7 +124,8 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen // Display index fields only when new index fields are being created. if ($add_index_field || $index_field_values) { $element['indexes']['fields']['#access'] = TRUE; - } else { + } + else { $element['indexes']['fields']['#access'] = FALSE; } @@ -159,7 +158,7 @@ public function massageFormValues(array $values, array $form, FormStateInterface $index_inputs = !empty($indexes_collection) ? [ [ "name" => $index_fields["name"] ?? '', - "length" => isset($index_fields["length"]) ? (int)$index_fields["length"] : 0, + "length" => isset($index_fields["length"]) ? (int) $index_fields["length"] : 0, ], ] : []; @@ -218,7 +217,12 @@ public function preRenderIndexForm(array $indexes) { * {@inheritdoc} */ public static function trustedCallbacks() { - return ['preRenderForm', 'preRenderIndexFieldFormOnAdd', 'preRenderIndexFieldForm', 'preRenderIndexForm']; + return [ + 'preRenderForm', + 'preRenderIndexFieldFormOnAdd', + 'preRenderIndexFieldForm', + 'preRenderIndexForm', + ]; } }