Skip to content

Commit

Permalink
Fully functional edit experience for index fields (#4204)
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 3ed3a9a commit dd6ebf8
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 113 deletions.
10 changes: 6 additions & 4 deletions modules/data_dictionary_widget/src/Fields/FieldButtons.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ public static function addButton() {
return [
'#type' => 'submit',
'#value' => 'Add field',
'#name' => 'add_dictionary_field',
'#access' => TRUE,
'#op' => 'add_new_field',
'#submit' => [
[
'\Drupal\data_dictionary_widget\Fields\FieldCallbacks',
'addSubformCallback',
],
[
'\Drupal\data_dictionary_widget\Fields\FieldCallbacks',
'addSubformCallback',
],
],
'#ajax' => [
'callback' => '\Drupal\data_dictionary_widget\Fields\FieldCallbacks::subformAjax',
Expand Down Expand Up @@ -112,6 +113,7 @@ public static function cancelButton($location, $key) {
$cancel_button = [
'#type' => 'submit',
'#value' => t('Cancel'),
'#name' => 'cancel_dictionary_field',
'#op' => $op,
'#submit' => [
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public static function addSubformCallback(array &$form, FormStateInterface $form
$form_state->set('add', TRUE);
$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 @@ -48,7 +48,6 @@ public static function addIndex() {
$add_index['group']['index']['fields'] = [
'#type' => 'fieldset',
'#title' => t('Fields'),
'#required' => TRUE,
'#prefix' => '<div id = field-json-metadata-dictionary-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>'),
Expand All @@ -58,7 +57,6 @@ public static function addIndex() {
];

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

Expand All @@ -69,7 +67,7 @@ public static function addIndex() {
* Create add fields for Data Dictionary Widget.
*/
public static function addIndexFields($current_index_fields) {
$id = $current_index_fields ? "field-json-metadata-dictionary-index-fields-new" : "field-json-metadata-dictionary-index-fields";
$id = "field-json-metadata-dictionary-index-fields-new";
$add_index_fields['#access'] = FALSE;
$add_index_fields['group'] = [
'#type' => 'fieldset',
Expand Down
21 changes: 16 additions & 5 deletions modules/data_dictionary_widget/src/Indexes/IndexFieldButtons.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Drupal\data_dictionary_widget\Indexes;

use Drupal\Core\Form\FormStateInterface;
/**
* Various operations for creating Data Dictionary Widget fields.
*/
Expand All @@ -15,6 +14,7 @@ public static function addIndexFieldButton() {
return [
'#type' => 'submit',
'#value' => 'Add field',
'#name' => 'add_index_field',
'#access' => TRUE,
'#op' => 'add_new_index_field',
'#submit' => [
Expand All @@ -39,6 +39,7 @@ public static function addIndexButton() {
return [
'#type' => 'submit',
'#value' => 'Add index',
'#name' => 'add_index',
'#access' => TRUE,
'#op' => 'add_new_index',
'#submit' => [
Expand All @@ -49,7 +50,7 @@ public static function addIndexButton() {
],
'#ajax' => [
'callback' => '\Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexFormAjax',
'wrapper' => 'field-json-metadata-dictionary-indexes',
'wrapper' => 'field-json-metadata-dictionary-index',
'effect' => 'fade',
],
'#limit_validation_errors' => [],
Expand Down Expand Up @@ -114,10 +115,18 @@ public static function submitIndexFieldButton($location, $indexKey) {
['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'],
],
];

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'],
];
}
return $edit_index_button;
}
Expand All @@ -142,7 +151,7 @@ public static function submitIndexButton($location, $indexKey) {
],
'#ajax' => [
'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexFormAjax',
'wrapper' => 'field-json-metadata-dictionary-indexes',
'wrapper' => 'field-json-metadata-dictionary-index',
'effect' => 'fade',
],
'#limit_validation_errors' => [
Expand All @@ -161,12 +170,13 @@ 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' : 'subIndexFormFieldAjax';
$callbackId = ($id === 'field-json-metadata-dictionary-index-fields-new') ? 'subIndexFormExistingFieldAjax' : 'subIndexFormAjax';
$callbackClass = $location == 'edit' ? 'indexEditSubformCallback' : 'indexAddSubformCallback';
$op = $location == 'edit' && $indexKey ? 'abort_' . $indexKey : 'cancel_index_field';
$cancel_index_button = [
'#type' => 'submit',
'#value' => t('Cancel'),
'#name' => 'cancel_index_field',
'#op' => $op,
'#submit' => [
[
Expand Down Expand Up @@ -197,6 +207,7 @@ public static function cancelIndexButton($location, $indexKey) {
$cancel_index_button = [
'#type' => 'submit',
'#value' => t('Cancel Index'),
'#name' => 'cancel_index',
'#op' => $op,
'#submit' => [
[
Expand All @@ -206,7 +217,7 @@ public static function cancelIndexButton($location, $indexKey) {
],
'#ajax' => [
'callback' => 'Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks::indexFormAjax',
'wrapper' => 'field-json-metadata-dictionary-indexes',
'wrapper' => 'field-json-metadata-dictionary-index',
'effect' => 'fade',
],
'#limit_validation_errors' => [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static function indexAddSubformCallback(array &$form, FormStateInterface

if ($op === 'cancel_index_field') {
$form_state->set('cancel_index_field', TRUE);
$form_state->set('add_new_index_field', '');
}

if ($op === 'add_new_index_field') {
Expand Down Expand Up @@ -117,6 +118,7 @@ public static function indexEditSubformCallback(array &$form, FormStateInterface
unset($current_index_fields[$op_index[4]]);
$current_index_fields[$op_index[4]] = IndexFieldValues::updateIndexFieldValues($op_index[4], $update_values, $current_index_fields );
ksort($current_index_fields );

}

if (str_contains($op, 'edit')) {
Expand Down Expand Up @@ -166,4 +168,18 @@ public static function subIndexFormExistingFieldAjax(array &$form, FormStateInte
$form["field_json_metadata"]["widget"][0]["indexes"]["field_collection"]["group"]["index"]["fields"]["add_row_button"]['#access'] = TRUE;
return $form["field_json_metadata"]["widget"][0]["indexes"]["field_collection"]["group"]["index"]["fields"]["add_row_button"];
}
}

/**
* Widget validation callback.
*/
public static function indexFieldVal($element, FormStateInterface &$form_state, array &$form) {
$fields_to_validate = [
'name' => 'Name',
'length' => 'Length',
];

foreach ($fields_to_validate as $field_key => $field_label) {
IndexValidation::indexFieldVal($form_state, $field_key, $field_label, $form);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public static function createGeneralIndexFields($element) {
public static function createGeneralIndex($element, $current_indexes) {
$element['indexes'] = [
'#type' => 'fieldset',
'#title' => t('Data Dictionary Indexes'),
'#prefix' => '<div id = field-json-metadata-dictionary-indexes>',
'#title' => t('Indexes'),
'#prefix' => '<div id = field-json-metadata-dictionary-index>',
'#suffix' => '</div>',
'#markup' => t('<div class="claro-details__description">Adding indexes to your datastore tables can improve response times from common queries.</div>'),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,30 @@ class IndexFieldEditCreation {
/**
* Create edit fields for Data Dictionary Widget.
*/
public static function editIndexFields($indexKey, $current_index_fields) {
$id = $current_index_fields ? "field-json-metadata-dictionary-index-fields-new" : "field-json-metadata-dictionary-index-fields";
public static function editIndexFields($indexKey, $current_index_fields, $index_fields_being_modified) {
$id = $current_index_fields ? "field-json-metadata-dictionary-index-fields" : "field-json-metadata-dictionary-index-fields-new";
$indexKeyExplode = explode("_", $indexKey);
$edit_index_fields['name'] = [
'#name' => 'field_json_metadata[0][fields][data][' . $indexKeyExplode[3] . '][field_collection][name]',
'#name' => 'field_json_metadata[0][indexes][fields][edit_index_fields][' . $indexKeyExplode[3] . '][name]',
'#type' => 'textfield',
'#value' => $current_index_fields[$indexKeyExplode[3]]['name'],
'#title' => 'Name',
'#required' => TRUE,
];
$edit_index_fields['length'] = [
'#name' => 'field_json_metadata[0][fields][data]['. $indexKeyExplode[3] .'][field_collection][length]',
'#name' => 'field_json_metadata[0][indexes][fields][edit_index_fields][' . $indexKeyExplode[3] . '][length]',
'#type' => 'number',
'#value' => $current_index_fields[$indexKeyExplode[3]]['length'],
'#title' => 'Length',
'#required' => TRUE,
];

$edit_index_fields['update_index_field']['actions'] = self::createIndexActionFields($indexKey, $id);
return $edit_index_fields;

return $edit_index_fields;
}

/**
/**
* Create edit fields for Data Dictionary Widget.
*/
public static function editIndex($indexKey, $current_index) {
Expand All @@ -51,8 +53,8 @@ public static function editIndex($indexKey, $current_index) {
];

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

return $edit_index;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,12 @@ public static function editIndexActions() {
*/
public static function setAddIndexFieldFormState($add_new_index_field, $element) {
if ($add_new_index_field) {

$element['indexes']['fields']['#access'] = FALSE;
$element['indexes']['fields']['field_collection'] = $add_new_index_field;
$element['indexes']['fields']['field_collection']['#access'] = TRUE;
$element['indexes']['fields']['add_row_button']['#access'] = FALSE;
$element['identifier']['#required'] = FALSE;
$element['title']['#required'] = FALSE;
//$element["indexes"]["field_collection"]["group"]["index"]["description"]['#required'] = FALSE;
}

return $element;
Expand All @@ -151,7 +149,6 @@ public static function setAddIndexFormState($add_new_index, $element) {
$element['indexes']['add_row_button']['#access'] = FALSE;
$element['identifier']['#required'] = FALSE;
$element['title']['#required'] = FALSE;
//$element["indexes"]["field_collection"]["group"]["index"]["description"]['#required'] = FALSE;
}

return $element;
Expand All @@ -161,7 +158,7 @@ public static function setAddIndexFormState($add_new_index, $element) {
* Create edit and update fields where needed.
*/
public static function createDictionaryIndexFieldOptions($op_index, $index_data_results, $index_fields_being_modified, $element) {
$current_index_fields = $element['current_index_fields'] ?? NULL;
$current_index_fields = $index_data_results ?? NULL;
// Creating ajax buttons/fields to be placed in correct location later.
foreach ($index_data_results as $indexKey => $data) {
if (self::checkIndexEditingField('index_field_key_' . $indexKey, $op_index, $index_fields_being_modified)) {
Expand Down
10 changes: 5 additions & 5 deletions modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
namespace Drupal\data_dictionary_widget\Indexes;

/**
* Various operations for creating Data Dictionary Widget fields.
* Various operations for creating index fields.
*/
class IndexFieldValues {
/**
* Return updated index field values after edit.
*/
public static function updateIndexFieldValues($field_index, $update_values, $current_index_fields) {
$name = $update_values['field_json_metadata'][0]['fields']['data'][$field_index]['field_collection']['name'];
$length = $update_values['field_json_metadata'][0]['fields']['data'][$field_index]['field_collection']['length'];
$name = $update_values["field_json_metadata"][0]["indexes"]["fields"]["edit_index_fields"][$field_index]["name"];
$length = $update_values["field_json_metadata"][0]["indexes"]["fields"]["edit_index_fields"][$field_index]["length"];

return [
'name' => $name,
'length' => $length,
'length' => (int)$length,
];
}
}
}
28 changes: 28 additions & 0 deletions modules/data_dictionary_widget/src/Indexes/IndexValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,32 @@ public static function indexFieldsValidation(array $element, FormStateInterface
}
}

/**
* Validation callback for a index fields edit form.
*
* If index field name and length is empty on edit, validation will trigger.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $field_key
* The key value for the field being edited.
* @param array $field_label
* The label for the field being edited.
*/
public static function indexFieldVal(FormStateInterface $form_state, $field_key, $field_label) {
$trigger = $form_state->getTriggeringElement();
$op = $trigger['#op'];
$op_index = explode("_", $op);

// Perform validation for update operation.
if (str_contains($op, 'update')) {
$update_values = $form_state->getUserInput();
$value = $update_values["field_json_metadata"][0]["indexes"]["fields"]["edit_index_fields"][$op_index[4]][$field_key];
if ($value === "") {
$field = "field_json_metadata][0][indexes][fields][edit_index_fields][$op_index[4]][$field_key";
$form_state->setErrorByName($field, t($field_label . ' field is required.'));
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
if ($index_field_values || $current_index_fields) {
$element["indexes"]["fields"] = IndexFieldOperations::createDictionaryIndexFieldOptions($op_index, $index_fields_data_results, $index_fields_being_modified, $element['indexes']['fields']);
}
$element['indexes']['fields']['add_row_button']['#access'] = $index_field_values ? TRUE : FALSE;
$element['indexes']['fields']['add_row_button']['#access'] = $index_fields_being_modified == NULL ? TRUE : FALSE;

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

0 comments on commit dd6ebf8

Please sign in to comment.