Skip to content

Commit

Permalink
Some simple code cleanup (white space, removal of commented out code,…
Browse files Browse the repository at this point in the history
… arrays, comment updates).
  • Loading branch information
dmundra committed Sep 6, 2024
1 parent 887202f commit 1160845
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 133 deletions.
12 changes: 6 additions & 6 deletions modules/data_dictionary_widget/src/Fields/FieldCallbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]]);
Expand Down Expand Up @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions modules/data_dictionary_widget/src/Fields/FieldOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -379,4 +379,4 @@ public static function resetFieldValues(array &$form, FormStateInterface $form_s
}
}

}
}
112 changes: 58 additions & 54 deletions modules/data_dictionary_widget/src/Indexes/IndexFieldAddCreation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<div id = field-json-metadata-index>',
'#suffix' => '</div>',
'#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' => '<div id = field-json-metadata-index>',
'#suffix' => '</div>',
'#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' => '<div id = field-json-metadata-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']['index']['fields'] = [
'#type' => 'fieldset',
'#title' => t('Fields'),
'#prefix' => '<div id = field-json-metadata-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']['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.
Expand All @@ -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;
}

Expand All @@ -112,4 +115,5 @@ private static function createIndexActionFields($id) {
'cancel_index_settings' => IndexFieldButtons::cancelIndexFieldButton('cancel', NULL, $id),
];
}
}

}
102 changes: 83 additions & 19 deletions modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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';
Expand All @@ -82,18 +83,26 @@ 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,
'wrapper' => $id,
'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',
],
],
];
}
Expand Down Expand Up @@ -122,20 +131,59 @@ 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',
],
],
];

if ($location == 'edit') {
$indexKeyExplode = explode("_", $indexKey);
$edit_index_button['#name'] = 'update_' . $indexKey;
$edit_index_button['#limit_validation_errors'] = [
['field_json_metadata', 0, 'indexes', 'fields', 'edit_index_fields', $indexKeyExplode[3], 'name'],
['field_json_metadata', 0, 'indexes', 'fields', 'edit_index_fields', $indexKeyExplode[3], 'length'],
[
'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;
Expand All @@ -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';
Expand All @@ -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',
],
],
];

Expand Down Expand Up @@ -286,4 +349,5 @@ public static function deleteIndexButton($indexKey) {
'#limit_validation_errors' => [],
];
}
}

}
Loading

0 comments on commit 1160845

Please sign in to comment.