Skip to content

Commit

Permalink
Editable Index (#4239)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaise-lafrai authored and dmundra committed Aug 9, 2024
1 parent dd6ebf8 commit ed52003
Show file tree
Hide file tree
Showing 13 changed files with 420 additions and 144 deletions.
6 changes: 3 additions & 3 deletions modules/data_dictionary_widget/src/Fields/FieldCallbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ 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"]["fields"]["data"]["#rows"];
$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
$op_index = explode("_", $form_state->getTriggeringElement()['#op']);
Expand Down Expand Up @@ -103,7 +103,7 @@ public static function addSubformCallback(array &$form, FormStateInterface $form
$form_state->set('add_new_field', '');
$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"];
$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 Down
14 changes: 13 additions & 1 deletion modules/data_dictionary_widget/src/Fields/FieldOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,9 @@ public static function checkFieldCollection($data_pre, $op) {
/**
* Set the elements associated with adding a new field.
*/
public static function setAddFormState($add_new_field, $element) {
public static function setAddDictionaryFieldFormState($add_new_field, $element) {
if ($add_new_field) {
unset($element['dictionary_fields']["edit_buttons"]);
$element['dictionary_fields']['field_collection'] = $add_new_field;
$element['dictionary_fields']['field_collection']['#access'] = TRUE;
$element['dictionary_fields']['add_row_button']['#access'] = FALSE;
Expand All @@ -207,6 +208,17 @@ public static function setAddFormState($add_new_field, $element) {
return $element;
}

/**
* Set the elements associated with editing a dictionary field.
*/
public static function editDictionaryFieldFormState($edit_dictionary_field, $element) {
if ($edit_dictionary_field) {
unset($element['dictionary_fields']["edit_buttons"]);
}

return $element;
}

/**
* Create edit and update fields where needed.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
namespace Drupal\data_dictionary_widget\Indexes;

/**
* Various operations for creating Data Dictionary Widget add fields.
* Various operations for creating Index fields.
*/
class IndexFieldAddCreation {

/**
* Create add fields for Data Dictionary Widget.
* 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-dictionary-index>',
'#prefix' => '<div id = field-json-metadata-index>',
'#suffix' => '</div>',
'#element_validate' => [
['\Drupal\data_dictionary_widget\Indexes\IndexValidation', 'indexFieldsValidation']
Expand Down Expand Up @@ -48,7 +48,7 @@ public static function addIndex() {
$add_index['group']['index']['fields'] = [
'#type' => 'fieldset',
'#title' => t('Fields'),
'#prefix' => '<div id = field-json-metadata-dictionary-index-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' => [
Expand All @@ -64,10 +64,10 @@ public static function addIndex() {
}

/**
* Create add fields for Data Dictionary Widget.
* Create fields for adding an index field.
*/
public static function addIndexFields($current_index_fields) {
$id = "field-json-metadata-dictionary-index-fields-new";
$id = "field-json-metadata-index-fields-new";
$add_index_fields['#access'] = FALSE;
$add_index_fields['group'] = [
'#type' => 'fieldset',
Expand Down
64 changes: 49 additions & 15 deletions modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Drupal\data_dictionary_widget\Indexes;

/**
* Various operations for creating Data Dictionary Widget fields.
* Various operations for index widget buttons.
*/
class IndexFieldButtons {

Expand All @@ -25,7 +25,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-index-fields',
'effect' => 'fade',
],
'#limit_validation_errors' => [],
Expand All @@ -50,7 +50,7 @@ public static function addIndexButton() {
],
'#ajax' => [
'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexFormAjax',
'wrapper' => 'field-json-metadata-dictionary-index',
'wrapper' => 'field-json-metadata-index',
'effect' => 'fade',
],
'#limit_validation_errors' => [],
Expand All @@ -61,10 +61,19 @@ public static function addIndexButton() {
* Returns the edit buttons.
*/
public static function editIndexButtons($indexKey) {
if (str_contains($indexKey, 'field')) {
$callback = 'indexEditSubformCallback';
$id = 'field-json-metadata-index-fields';
$function = 'subIndexFormAjax';
} else {
$callback = 'indexEditCallback';
$id = 'field-json-metadata-index';
$function = 'indexFormAjax';
}
return [
'#type' => 'image_button',
'#name' => 'edit_index_' . $indexKey,
'#id' => 'edit_index_' . $indexKey,
'#name' => 'edit_' . $indexKey,
'#id' => 'edit_' . $indexKey,
'#access' => TRUE,
'#op' => 'edit_' . $indexKey,
'#src' => 'core/misc/icons/787878/cog.svg',
Expand All @@ -75,12 +84,12 @@ public static function editIndexButtons($indexKey) {
'#submit' => [
[
'\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks',
'indexEditSubformCallback',
$callback,
],
],
'#ajax' => [
'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexFormAjax',
'wrapper' => 'field-json-metadata-dictionary-index-fields',
'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::' . $function,
'wrapper' => $id,
'effect' => 'fade',
],
'#limit_validation_errors' => [
Expand All @@ -96,6 +105,7 @@ public static function submitIndexFieldButton($location, $indexKey) {
$callbackClass = $location == 'edit' ? 'indexEditSubformCallback' : 'indexAddSubformCallback';
$op = !empty($indexKey) ? 'update_' . $indexKey : 'add_index_field';
$value = $location == 'edit' ? 'Save' : 'Add ';
$function = $location == 'edit' ? 'subIndexFormAjax' : 'subIndexFormAjax';
$edit_index_button = [
'#type' => 'submit',
'#value' => $value,
Expand All @@ -107,8 +117,8 @@ public static function submitIndexFieldButton($location, $indexKey) {
],
],
'#ajax' => [
'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexFormAjax',
'wrapper' => 'field-json-metadata-dictionary-index-fields',
'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::' . $function,
'wrapper' => 'field-json-metadata-index-fields',
'effect' => 'fade',
],
'#limit_validation_errors' => [
Expand Down Expand Up @@ -151,7 +161,7 @@ public static function submitIndexButton($location, $indexKey) {
],
'#ajax' => [
'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexFormAjax',
'wrapper' => 'field-json-metadata-dictionary-index',
'wrapper' => 'field-json-metadata-index',
'effect' => 'fade',
],
'#limit_validation_errors' => [
Expand All @@ -170,7 +180,7 @@ public static function submitIndexButton($location, $indexKey) {
* Create Cancel button.
*/
public static function cancelIndexFieldButton($location, $indexKey, $id) {
$callbackId = ($id === 'field-json-metadata-dictionary-index-fields-new') ? 'subIndexFormExistingFieldAjax' : 'subIndexFormAjax';
$callbackId = ($id === 'field-json-metadata-index-fields-new') ? 'subIndexFormExistingFieldAjax' : 'subIndexFormAjax';
$callbackClass = $location == 'edit' ? 'indexEditSubformCallback' : 'indexAddSubformCallback';
$op = $location == 'edit' && $indexKey ? 'abort_' . $indexKey : 'cancel_index_field';
$cancel_index_button = [
Expand Down Expand Up @@ -217,7 +227,7 @@ public static function cancelIndexButton($location, $indexKey) {
],
'#ajax' => [
'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexFormAjax',
'wrapper' => 'field-json-metadata-dictionary-index',
'wrapper' => 'field-json-metadata-index',
'effect' => 'fade',
],
'#limit_validation_errors' => [],
Expand All @@ -232,7 +242,7 @@ public static function cancelIndexButton($location, $indexKey) {
/**
* Create Delete button.
*/
public static function deleteIndexButton($indexKey) {
public static function deleteIndexFieldButton($indexKey) {
return [
'#type' => 'submit',
'#name' => 'index_delete_' . $indexKey,
Expand All @@ -246,7 +256,31 @@ public static function deleteIndexButton($indexKey) {
],
'#ajax' => [
'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::subIndexFormAjax',
'wrapper' => 'field-json-metadata-dictionary-index-fields',
'wrapper' => 'field-json-metadata-index-fields',
'effect' => 'fade',
],
'#limit_validation_errors' => [],
];
}

/**
* Create Delete button.
*/
public static function deleteIndexButton($indexKey) {
return [
'#type' => 'submit',
'#name' => 'index_delete_' . $indexKey,
'#value' => t('Delete index'),
'#op' => 'delete_' . $indexKey,
'#submit' => [
[
'\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks',
'indexEditCallback',
],
],
'#ajax' => [
'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexFormAjax',
'wrapper' => 'field-json-metadata-index',
'effect' => 'fade',
],
'#limit_validation_errors' => [],
Expand Down
77 changes: 71 additions & 6 deletions modules/data_dictionary_widget/src/Indexes/IndexFieldCallbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Drupal\Core\Form\FormStateInterface;

/**
* Various operations for the Data Dictionary Widget callbacks.
* Various operations for the Index callbacks.
*/
class IndexFieldCallbacks {
/**
Expand All @@ -14,9 +14,9 @@ class IndexFieldCallbacks {
public static function indexAddSubformCallback(array &$form, FormStateInterface $form_state) {
$trigger = $form_state->getTriggeringElement();
$op = $trigger['#op'];
$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"];
$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_index_fields) {
$form_state->set('current_index_fields', $current_index_fields);
Expand Down Expand Up @@ -92,7 +92,7 @@ public static function indexAddCallback(array &$form, FormStateInterface $form_s
}

/**
* Submit callback for the Index Edit button.
* Submit callback for the Index Field Edit button.
*/
public static function indexEditSubformCallback(array &$form, FormStateInterface $form_state) {
$trigger = $form_state->getTriggeringElement();
Expand Down Expand Up @@ -121,7 +121,11 @@ public static function indexEditSubformCallback(array &$form, FormStateInterface

}

if (str_contains($op, 'edit')) {
if (str_contains($op, 'edit_index_key')) {
$currently_modifying_index_fields[$op_index[4]] = $current_index_fields[$op_index[4]];
}

if (str_contains($op, 'edit_index_field')) {
$currently_modifying_index_fields[$op_index[4]] = $current_index_fields[$op_index[4]];
}

Expand All @@ -132,11 +136,72 @@ public static function indexEditSubformCallback(array &$form, FormStateInterface
$form_state->setRebuild();
}

/**
* Submit callback for the Index Edit button.
*/
public static function indexEditCallback(array &$form, FormStateInterface $form_state) {
$trigger = $form_state->getTriggeringElement();
$current_index_fields = $form["field_json_metadata"]["widget"][0]["indexes"]["fields"]["data"]["#rows"] ?? [];
$current_index = $form["field_json_metadata"]["widget"][0]["indexes"]["data"]["#rows"] ?? [];
$current_dictionary_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"] ?? [];
$op = $trigger['#op'];
$op_index = explode("_", $trigger['#op']);
$currently_modifying_index_fields = $form_state->get('index_fields_being_modified') != NULL ? $form_state->get('index_fields_being_modified') : [];
$currently_modifying_index = $form_state->get('index_being_modified') != NULL ? $form_state->get('index_being_modified') : [];
$currently_modifying_dictionary_fields = $form_state->get('dictionary_fields_being_modified') != NULL ? $form_state->get('dictionary_fields_being_modified') : [];

if (str_contains($op, 'abort_index_key')) {
unset($currently_modifying_index[$op_index[3]]);
}

if (str_contains($op, 'abort_index_field_key')) {
unset($currently_modifying_index_fields[$op_index[4]]);
}

if (str_contains($op, 'delete_index_key')) {
unset($currently_modifying_index[$op_index[3]]);
unset($current_index[$op_index[3]]);
}

if (str_contains($op, 'delete_index_field_key')) {
unset($currently_modifying_index_fields[$op_index[4]]);
unset($current_index_fields[$op_index[4]]);
}

if (str_contains($op, 'update')) {
$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);
}

if (str_contains($op, 'edit')) {
$currently_modifying_index[$op_index[3]] = $current_index[$op_index[3]];
}

$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', $current_index);
$form_state->set('current_dictionary_fields', $current_dictionary_fields);
$form_state->setRebuild();
}

/**
* Ajax callback to return index fields.
*/
public static function subIndexEditFormAjax(array &$form, FormStateInterface $form_state) {
return $form["field_json_metadata"]["widget"][0]["indexes"]["edit_index"]["index_key_0"]["group"]["fields"]["fields"];
}

/**
* Ajax callback to return index fields.
*/
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"];
}

/**
Expand Down
Loading

0 comments on commit ed52003

Please sign in to comment.