Skip to content

Commit

Permalink
Removed unused code, fixed index fields cancel button
Browse files Browse the repository at this point in the history
  • Loading branch information
kaise-lafrai committed Jun 27, 2024
1 parent 35f8e82 commit 3516bd6
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 173 deletions.
2 changes: 0 additions & 2 deletions modules/data_dictionary_widget/src/Fields/FieldCallbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down
2 changes: 0 additions & 2 deletions modules/data_dictionary_widget/src/Fields/FieldCreation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = [
Expand Down
12 changes: 0 additions & 12 deletions modules/data_dictionary_widget/src/Fields/FieldOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ public static function addIndex() {
'#suffix' => '</div>',
];

//$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',
];

Expand All @@ -47,15 +45,13 @@ public static function addIndex() {
'#type' => 'fieldset',
'#title' => t('Fields'),
'#required' => TRUE,
'#prefix' => '<div id = field-json-metadata-dictionary-index-fields-new>',
'#prefix' => '<div id = field-json-metadata-dictionary-index-fields>',
'#suffix' => '</div>',
'#markup' => t('<div class="claro-details__description">Test One or more fields included in index. Must be keys from the fields object.</div>'),
'#markup' => t('<div class="claro-details__description">One or more fields included in index. Must be keys from the fields object.</div>'),
];

$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);

Expand All @@ -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' => '<div id = field-json-metadata-dictionary-index-fields-new>',
'#prefix' => "<div id = $id>",
'#suffix' => '</div>',
'#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']['index_fields'] = [
// '#prefix' => '<div id = field-json-metadata-dictionary-index-fields>',
// '#suffix' => '</div>',
// ];

$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;
}
Expand All @@ -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),
];
}
}
73 changes: 37 additions & 36 deletions modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [],
Expand All @@ -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',
],
Expand Down Expand Up @@ -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',
],
Expand All @@ -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' => [],
Expand All @@ -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',
],
Expand All @@ -150,22 +150,23 @@ 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 = [
'#type' => 'submit',
'#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' => [],
Expand All @@ -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',
],
Expand All @@ -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',
],
Expand Down
37 changes: 21 additions & 16 deletions modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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') {
Expand All @@ -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);
}

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

0 comments on commit 3516bd6

Please sign in to comment.