Skip to content

Commit

Permalink
Added working cancel button for index, fixed errors appearing in log
Browse files Browse the repository at this point in the history
  • Loading branch information
kaise-lafrai committed May 14, 2024
1 parent 9aa893c commit 2489108
Show file tree
Hide file tree
Showing 11 changed files with 295 additions and 120 deletions.
39 changes: 31 additions & 8 deletions modules/data_dictionary_widget/data_dictionary_widget.module
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,44 @@ function data_dictionary_widget_form_alter(&$form, &$form_state, $form_id) {

if ($form_id == 'node_data_edit_form' || $form_id == 'node_data_form') {
$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.
Expand Down
8 changes: 6 additions & 2 deletions modules/data_dictionary_widget/src/Fields/FieldCallbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -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);
}
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<div id = field-json-metadata-dictionary-index-fields>',
'#required' => TRUE,
'#prefix' => '<div id = field-json-metadata-dictionary-index-fields-new>',
'#suffix' => '</div>',
'#markup' => t('<div class="claro-details__description">One or more fields included in index. Must be keys from the fields object.</div>'),
'#markup' => t('<div class="claro-details__description">Test One or more fields included in index. Must be keys from the fields object.</div>'),
];

$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;
}
Expand All @@ -68,7 +70,7 @@ public static function addIndexFields() {
$add_index_fields['group'] = [
'#type' => 'fieldset',
'#title' => t('Add new field'),
'#prefix' => '<div id = field-json-metadata-dictionary-index-fields>',
'#prefix' => '<div id = field-json-metadata-dictionary-index-fields-new>',
'#suffix' => '</div>',
];

Expand All @@ -77,13 +79,13 @@ public static function addIndexFields() {
// '#suffix' => '</div>',
// ];

$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;
}
Expand All @@ -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',
];
Expand Down
14 changes: 7 additions & 7 deletions modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [],
Expand Down Expand Up @@ -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' => [],
Expand Down Expand Up @@ -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' => [],
Expand Down Expand Up @@ -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',
Expand All @@ -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' => [],
Expand Down
30 changes: 26 additions & 4 deletions modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}
Expand All @@ -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') {
Expand All @@ -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();
}

Expand All @@ -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']);
Expand Down Expand Up @@ -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"];
}

/**
Expand Down
Loading

0 comments on commit 2489108

Please sign in to comment.