Skip to content

Commit

Permalink
Added validation for index fields form, refactored code, refactored t…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
kaise-lafrai committed Jun 27, 2024
1 parent 3516bd6 commit 4e9882e
Show file tree
Hide file tree
Showing 13 changed files with 261 additions and 135 deletions.
5 changes: 5 additions & 0 deletions modules/data_dictionary_widget/css/dataDictionaryWidget.css
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
6 changes: 3 additions & 3 deletions modules/data_dictionary_widget/data_dictionary_widget.module
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ 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]',
'#type' => 'select',
'#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' => '<div id = field-json-metadata-' . $key . '-format>',
'#suffix' => '</div>',
Expand All @@ -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 [
Expand Down
34 changes: 23 additions & 11 deletions modules/data_dictionary_widget/src/Fields/FieldOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"])) {
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
Expand Down Expand Up @@ -355,4 +367,4 @@ public static function resetFieldValues(array &$form, FormStateInterface $form_s
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@ public static function addIndex() {
'#open' => TRUE,
'#prefix' => '<div id = field-json-metadata-dictionary-index>',
'#suffix' => '</div>',
'#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',
Expand All @@ -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' => '<div id = field-json-metadata-dictionary-index-fields>',
'#suffix' => '</div>',
'#markup' => t('<div class="claro-details__description">One or more fields included in index. Must be keys from the fields object.</div>'),
'#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;
}
Expand All @@ -72,14 +79,15 @@ public static function addIndexFields($current_index_fields) {
'#markup' => t('<div class="claro-details__description">Add a single index field. Must be keys from the fields object.</div>'),
];

$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;
}
Expand All @@ -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,
];
}

Expand Down
16 changes: 13 additions & 3 deletions modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\data_dictionary_widget\Indexes;

use Drupal\Core\Form\FormStateInterface;
/**
* Various operations for creating Data Dictionary Widget fields.
*/
Expand Down Expand Up @@ -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'],
],
];
}

Expand All @@ -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') {
Expand All @@ -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';
Expand All @@ -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') {
Expand Down
14 changes: 11 additions & 3 deletions modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,29 @@ 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"];
}

/**
* 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"];
}
}
18 changes: 8 additions & 10 deletions modules/data_dictionary_widget/src/Indexes/IndexFieldCreation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<div id = field-json-metadata-dictionary-index-fields>',
'#suffix' => '</div>',
'#markup' => t('<div class="claro-details__description">One or more fields included in index. Must be keys from the fields object.</div>'),
'#required' => TRUE,
];

return $element;
Expand All @@ -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'),
Expand All @@ -36,15 +34,15 @@ public static function createGeneralIndex($element, $field_json_metadata, $curre
'#markup' => t('<div class="claro-details__description">Adding indexes to your datastore tables can improve response times from common queries.</div>'),
];

$element['indexes']['current_index'] = $current_index;
$element['indexes']['current_index'] = $current_indexes;

return $element;
}

/**
* 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),
Expand All @@ -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' => '<div id = field-json-metadata-dictionary-indexes>',
'#suffix' => '</div>',
'#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',
];
Expand Down
Loading

0 comments on commit 4e9882e

Please sign in to comment.