Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data dictionary indexes code refactoring #4280

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Various operations for the Index callbacks.
*/
class IndexFieldCallbacks {

/**
* Submit callback for the Index Add button.
*/
Expand Down Expand Up @@ -141,11 +142,11 @@ public static function indexEditSubformCallback(array &$form, FormStateInterface
*/
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"] ?? [];
$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']);
$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') : [];
Expand Down Expand Up @@ -226,8 +227,8 @@ public static function subIndexFormFieldAjax(array &$form, FormStateInterface $f
}

/**
* Ajax callback to return index fields fieldset with existing fields and Add
* Field button.
* 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"]["index"]["fields"]["add_row_button"]['#access'] = TRUE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static function editIndex($indexKey, $current_index, $index_being_modifie

$edit_index['group']['fields']['data'] = IndexFieldCreation::createIndexFieldsDataRows($current_index[$indexKeyExplode[2]]['fields'], $current_index[$indexKeyExplode[2]]['fields'], $current_index[$indexKeyExplode[2]]['fields'], $form_state);

$edit_index['update_index']['actions'] = self::createIndexActionFields($indexKey, $id );
$edit_index['update_index']['actions'] = self::createIndexActionFields($indexKey, $id);

return $edit_index;
}
Expand Down
42 changes: 23 additions & 19 deletions modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class IndexFieldOperations {
* Setting ajax elements when editing newly added index fields.
*/
public static function setIndexFieldsAjaxElementsOnAdd(array $indexFields) {
if ($indexFields["data"]) {
if ($indexFields['data']) {
foreach ($indexFields['data']['#rows'] as $row => $data) {
$edit_index_button = $indexFields['edit_index_buttons']['index_field_key_' . $row] ?? NULL;
$edit_index_fields = $indexFields['edit_index_fields']['index_field_key_' . $row] ?? NULL;
Expand All @@ -35,16 +35,18 @@ public static function setIndexFieldsAjaxElementsOnAdd(array $indexFields) {

/**
* Setting ajax elements when editing existing index fields.
*
* Currently, not being used as index fields cannot be edited once added.
*/
public static function setIndexFieldsAjaxElements(array $indexFields) {
if ($indexFields["data"]) {
if ($indexFields['data']) {
foreach ($indexFields['data']['#rows'] as $row => $data) {
$edit_index_fields_button = $indexFields['fields']['edit_index_fields_buttons']['index_field_key_' . $row] ?? NULL;
$edit_index_fields = $indexFields['fields']['edit_index_fields']['index_field_key_' . $row] ?? NULL;
// Setting the ajax fields if they exist.
if ($edit_index_fields_button) {
$indexFields["data"]["#rows"][$row] = array_merge($data, $edit_index_fields_button);
unset($indexFields["fields"]["edit_index_fields_buttons"]['index_field_key_' . $row]);
$indexFields['data']['#rows'][$row] = array_merge($data, $edit_index_fields_button);
unset($indexFields['fields']['edit_index_fields_buttons']['index_field_key_' . $row]);
}
elseif ($edit_index_fields) {
unset($indexFields['data']['#rows']['index_field_key_' . $row]);
Expand Down Expand Up @@ -118,14 +120,14 @@ public static function processIndexDataResults($index_results, $current_indexes,
$index_results = $current_indexes;
}

if (isset($index_values["field_json_metadata"][0]["indexes"]["field_collection"])) {
$index_group = $index_values["field_json_metadata"][0]["indexes"]["field_collection"]["group"];
if (isset($index_values['field_json_metadata'][0]['indexes']['field_collection'])) {
$index_group = $index_values['field_json_metadata'][0]['indexes']['field_collection']['group'];

$data_index_pre = [
[
"description" => $index_group['index']["description"],
"type" => $index_group['index']["type"],
"fields" => $index_fields_data_results,
'description' => $index_group['index']['description'],
'type' => $index_group['index']['type'],
'fields' => $index_fields_data_results,
],
];
}
Expand Down Expand Up @@ -215,7 +217,7 @@ public static function createIndexFieldOptions($op_index, $index_data_results, $
/**
* Create edit and update fields for indexes.
*/
public static function createIndexOptions($op_index, $index_data_results, $index_being_modified, $index_field_being_modified, $element, $form_state) {
public static function createIndexOptions($op_index, $index_data_results, $index_being_modified, $element, $form_state) {
$current_indexes = $element['current_index'];

// Creating ajax buttons/fields to be placed in correct location later.
Expand All @@ -237,13 +239,13 @@ public static function createIndexOptions($op_index, $index_data_results, $index
*/
public static function checkIndexEditingField($indexKey, $op_index, $index_fields_being_modified) {
$action_list = IndexFieldOperations::editIndexActions();
$indexKeyExplode = explode("_", $indexKey);
if (isset($op_index[0]) && in_array($op_index[0], $action_list) && array_key_exists($indexKeyExplode[3], $index_fields_being_modified)) {
return TRUE;
}
else {
return FALSE;
if (isset($op_index[0]) && in_array($op_index[0], $action_list)) {
$indexKeyExplode = explode('_', $indexKey);
if (array_key_exists($indexKeyExplode[3], $index_fields_being_modified)) {
return TRUE;
}
}
return FALSE;
}

/**
Expand All @@ -254,9 +256,11 @@ public static function checkIndexEditing($indexKey, $op_index, $index_being_modi
$op_index_string = implode('_', $op_index);
if (str_contains($op_index_string, 'edit_index_key')) {
$action_list = IndexFieldOperations::editIndexActions();
$indexKeyExplode = explode("_", $indexKey);
if (isset($op_index[0]) && in_array($op_index[0], $action_list) && array_key_exists($indexKeyExplode[2], $index_being_modified)) {
return TRUE;
if (isset($op_index[0]) && in_array($op_index[0], $action_list)) {
$indexKeyExplode = explode('_', $indexKey);
if (array_key_exists($indexKeyExplode[2], $index_being_modified)) {
return TRUE;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,37 @@ class DataDictionaryWidget extends WidgetBase implements TrustedCallbackInterfac
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
// Retrieve data-dictionary form_state values to be used for various operations.
$dictionary_field_values = $form_state->get("new_dictionary_fields");
// Retrieve data-dictionary form_state values to be used for various
// operations.
$dictionary_field_values = $form_state->get('new_dictionary_fields');
$add_new_dictionary_field = $form_state->get('add_new_field');
$current_dictionary_fields = $form_state->get('current_dictionary_fields');
$dictionary_fields_being_modified = $form_state->get("dictionary_fields_being_modified") ?? NULL;
$dictionary_fields_being_modified = $form_state->get('dictionary_fields_being_modified') ?? NULL;

// Retrieve indexes form_state values to be used for various operations.
$current_indexes = $form_state->get('current_index');
$index_values = $form_state->get('new_index');
$add_new_index = $form_state->get('add_new_index');
$index_being_modified = $form_state->get("index_being_modified") ?? NULL;
$index_being_modified = $form_state->get('index_being_modified') ?? NULL;

// Retrieve index fields form_state values to be used for various operations.
// Retrieve index fields form_state values to be used for various
// operations.
$current_index_fields = $form_state->get('current_index_fields');
$index_field_values = $form_state->get("new_index_fields");
$index_field_values = $form_state->get('new_index_fields');
$add_index_field = $form_state->get('add_new_index_field');
$index_fields_being_modified = $form_state->get("index_fields_being_modified") ?? NULL;
$index_fields_being_modified = $form_state->get('index_fields_being_modified') ?? NULL;

// Retrieve triggered element to be used for various operations.
$op = $form_state->getTriggeringElement()['#op'] ?? NULL;
$op_index = isset($op) ? explode("_", $op) : NULL;
$op_index = isset($op) ? explode('_', $op) : NULL;

// Retrieve form element item values.
$field_json_metadata = !empty($items[0]->value) ? json_decode($items[0]->value, TRUE) : [];

// Retrieve initial data results from field JSON metadata.
$data_results = $field_json_metadata["data"]["fields"] ?? [];
$index_fields_results = $field_json_metadata["data"]["indexes"][0]["fields"] ?? [];
$index_results = $field_json_metadata["data"]["indexes"] ?? [];
$data_results = $field_json_metadata['data']['fields'] ?? [];
$index_fields_results = $field_json_metadata['data']['indexes'][0]['fields'] ?? [];
$index_results = $field_json_metadata['data']['indexes'] ?? [];

// Process data results.
$data_results = FieldOperations::processDataResults($data_results, $current_dictionary_fields, $dictionary_field_values, $op);
Expand Down Expand Up @@ -89,18 +91,18 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen

// Create dictionary fields/buttons for editing.
$element['dictionary_fields'] = FieldOperations::createDictionaryFieldOptions($op_index, $data_results, $dictionary_fields_being_modified, $element['dictionary_fields']);
$element['dictionary_fields']['add_row_button']['#access'] = $dictionary_fields_being_modified == NULL ? TRUE : FALSE;
$element['dictionary_fields']['add_row_button']['#access'] = $dictionary_fields_being_modified == NULL;

// Create index fields/buttons for editing.
$element['indexes'] = IndexFieldOperations::createIndexOptions($op_index, $index_data_results, $index_being_modified, $index_fields_being_modified, $element['indexes'], $form_state);
$element['indexes'] = IndexFieldOperations::createIndexOptions($op_index, $index_data_results, $index_being_modified, $element['indexes'], $form_state);

// Create edit buttons and fields for index fields.
if ($index_field_values || $current_index_fields) {
$element["indexes"]["fields"] = IndexFieldOperations::createIndexFieldOptions($op_index, $index_fields_data_results, $index_fields_being_modified, $element['indexes']['fields']);
$element['indexes']['fields'] = IndexFieldOperations::createIndexFieldOptions($op_index, $index_fields_data_results, $index_fields_being_modified, $element['indexes']['fields']);
}

// Set access to index fields Add button when index fields being modified.
$element['indexes']['add_row_button']['#access'] = $index_being_modified == NULL ? TRUE : FALSE;
$element['indexes']['add_row_button']['#access'] = $index_being_modified == NULL;

// Get form entity.
$form_object = $form_state->getFormObject();
Expand Down