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

Index edit #4230

Draft
wants to merge 10 commits into
base: Data-Dictionary-Index-Working
Choose a base branch
from
Draft
5 changes: 5 additions & 0 deletions modules/data_dictionary_widget/css/dataDictionaryWidget.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
.index-fields-form.error {
border-width: var(--input--error-border-size);
border-color: var(--input--error-border-color);
}

.table {
display: table;
width: auto;
Expand Down
61 changes: 50 additions & 11 deletions modules/data_dictionary_widget/data_dictionary_widget.module
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ function data_dictionary_widget_theme($existing, $type, $theme, $path) {
],
'template' => 'custom-table',
],
'custom_index_fields_table' => [
'variables' => [
'header' => [],
'rows' => [],
'attributes' => [],
],
'template' => 'custom-index-fields-table',
],
'custom_index_table' => [
'variables' => [
'header' => [],
'rows' => [],
'attributes' => [],
],
'template' => 'custom-index-table',
],
];
}

Expand Down Expand Up @@ -96,21 +112,44 @@ function data_dictionary_widget_form_alter(&$form, &$form_state, $form_id) {
}

$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_dictionary_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] = [];

foreach ($keys as $attr) {
$formatted_current_fields[$key][$attr] = [
'#value' => $value[$attr]
];
if ($current_dictionary_fields) {
foreach ($current_dictionary_fields as $key => $value) {
$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;
}

// 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
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
76 changes: 51 additions & 25 deletions modules/data_dictionary_widget/src/Fields/FieldCallbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,58 @@ public static function updateFormatOptions(array &$form, FormStateInterface $for
* Submit callback for the Edit button.
*/
public static function editSubformCallback(array &$form, FormStateInterface $form_state) {
$current_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"];
// 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"];
// 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']);
$currently_modifying = $form_state->get('fields_being_modified') != NULL ? $form_state->get('fields_being_modified') : [];

// Get the fields we're currently modifying
$dictionary_fields_being_modified = $form_state->get('dictionary_fields_being_modified') != NULL ? $form_state->get('dictionary_fields_being_modified') : [];
$index_fields_being_modified = $form_state->get('index_fields_being_modified') != NULL ? $form_state->get('index_fields_being_modified') : [];
// If the op (trigger) containes abort,
// we're canceling the field we're currently modifying so unset it.
if (str_contains($form_state->getTriggeringElement()['#op'], 'abort')) {
unset($currently_modifying[$op_index[1]]);
unset($dictionary_fields_being_modified[$op_index[1]]);
}

// If the op (trigger) contains delete,
// we're deleting the field we're editing so...
if (str_contains($form_state->getTriggeringElement()['#op'], 'delete')) {
unset($currently_modifying[$op_index[1]]);
unset($current_fields[$op_index[1]]);
// Unset it from being currently modified.
unset($dictionary_fields_being_modified[$op_index[1]]);
// Remove the respective field/data from the form.
unset($current_dictionary_fields[$op_index[1]]);
}

// If the op (trigger) contains update,
// We're saving the field we're editing so...
if (str_contains($form_state->getTriggeringElement()['#op'], 'update')) {
unset($currently_modifying[$op_index[1]]);
unset($current_fields[$op_index[1]]);
$current_fields[$op_index[1]] = FieldValues::updateValues($op_index[1], $form_state->getUserInput(), $current_fields);
ksort($current_fields);
// Unset the respective currently modifying field.
unset($dictionary_fields_being_modified[$op_index[1]]);
// Unset the respective field/data from the form.
unset($current_dictionary_fields[$op_index[1]]);
// Update the respective current field data with our new input data.
$current_dictionary_fields[$op_index[1]] = FieldValues::updateValues($op_index[1], $form_state->getUserInput(), $current_dictionary_fields);
// Sort the current fields data.
ksort($current_dictionary_fields);
}

// If the op (trigger) contains edit
// We're editing a specific field so...
if (str_contains($form_state->getTriggeringElement()['#op'], 'edit')) {
$currently_modifying[$op_index[1]] = $current_fields[$op_index[1]];
// Set the field we're modifying to that field.
$dictionary_fields_being_modified[$op_index[1]] = $current_dictionary_fields[$op_index[1]];
}

// Re-index the current_fields array.
if ($current_fields) {
$current_fields = array_values($current_fields);
// Reindex the current_dictionary_fields array.
if ($current_dictionary_fields) {
$current_dictionary_fields = array_values($current_dictionary_fields);
}

$form_state->set('fields_being_modified', $currently_modifying);
$form_state->set('current_fields', $current_fields);
// Let's retain the fields that are being modified.
$form_state->set('index_fields_being_modified', $index_fields_being_modified);
$form_state->set('dictionary_fields_being_modified', $dictionary_fields_being_modified);
// Let's retain the fields that are already stored on the form,
// but aren't currently being modified.
$form_state->set('current_dictionary_fields', $current_dictionary_fields);
$form_state->set('current_index_fields', $current_index_fields );
// Let's rebuild the form.
$form_state->setRebuild();
}

Expand All @@ -80,9 +101,12 @@ public static function addSubformCallback(array &$form, FormStateInterface $form
$trigger = $form_state->getTriggeringElement();
$op = $trigger['#op'];
$form_state->set('add_new_field', '');
$current_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"];
if ($current_fields) {
$form_state->set('current_fields', $current_fields);
$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);
}

if ($op === 'cancel') {
Expand All @@ -95,11 +119,13 @@ public static function addSubformCallback(array &$form, FormStateInterface $form
}

if ($op === 'add') {
$form_state->set('new_fields', $form_state->getUserInput());
$form_state->set('new_dictionary_fields', $form_state->getUserInput());
$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
13 changes: 5 additions & 8 deletions modules/data_dictionary_widget/src/Fields/FieldCreation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ class FieldCreation {
/**
* Create basic widget.
*/
public static function createGeneralFields($element, $field_json_metadata, $current_fields, $form_state) {

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 All @@ -25,8 +23,7 @@ public static function createGeneralFields($element, $field_json_metadata, $curr
'#suffix' => '</div>',
'#markup' => t('<div class="claro-details__description">A data dictionary for this resource, compliant with the <a href="https://specs.frictionlessdata.io/table-schema/" target="_blank">Table Schema</a> specification.</div>'),
];

$element['dictionary_fields']['current_fields'] = $current_fields;
$element['dictionary_fields']['current_dictionary_fields'] = $current_dictionary_fields;

if (isset($field_json_metadata['data']['indexes'])) {
$element['indexes'] = self::createField('indexes', $field_json_metadata, $form_state);
Expand Down Expand Up @@ -84,13 +81,13 @@ protected static function createField(string $field, array $field_json_metadata,
/**
* Create data dictionary data rows.
*/
public static function createDictionaryDataRows($current_fields, $data_results, $form_state) {
public static function createDictionaryDataRows($current_dictionary_fields, $data_results, $form_state) {

return [
'#access' => ((bool) $current_fields || (bool) $data_results),
'#access' => ((bool) $current_dictionary_fields || (bool) $data_results),
'#type' => 'table',
'#header' => ['NAME', 'TITLE', 'DETAILS'],
'#rows' => $form_state->get('cancel') ? $current_fields : ($data_results ?? []),
'#rows' => $form_state->get('cancel') ? $current_dictionary_fields : ($data_results ?? []),
'#tree' => TRUE,
'#theme' => 'custom_table',
];
Expand Down
36 changes: 18 additions & 18 deletions modules/data_dictionary_widget/src/Fields/FieldEditCreation.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,28 @@ class FieldEditCreation {
/**
* Create edit fields for Data Dictionary Widget.
*/
public static function editFields($key, $current_fields) {
public static function editFields($key, $current_dictionary_fields) {

$edit_fields['name'] = [
'#name' => 'field_json_metadata[0][dictionary_fields][data][' . $key . '][field_collection][name]',
'#type' => 'textfield',
'#value' => $current_fields[$key]['name'],
'#value' => $current_dictionary_fields[$key]['name'],
'#required' => TRUE,
'#title' => 'Name',
'#description' => t('Machine name of the field/column in the data table.'),
];
$edit_fields['title'] = [
'#name' => 'field_json_metadata[0][dictionary_fields][data][' . $key . '][field_collection][title]',
'#type' => 'textfield',
'#value' => $current_fields[$key]['title'],
'#value' => $current_dictionary_fields[$key]['title'],
'#required' => TRUE,
'#title' => 'Title',
'#description' => t('A human-readable title.'),
];
$edit_fields['type'] = self::createType($key, $current_fields);
$edit_fields['format'] = self::createFormat($key, $current_fields);
$edit_fields['format_other'] = self::createFormatOther($key, $current_fields);
$edit_fields['description'] = self::createDescriptionField($key, $current_fields);
$edit_fields['type'] = self::createType($key, $current_dictionary_fields);
$edit_fields['format'] = self::createFormat($key, $current_dictionary_fields);
$edit_fields['format_other'] = self::createFormatOther($key, $current_dictionary_fields);
$edit_fields['description'] = self::createDescriptionField($key, $current_dictionary_fields);

$edit_fields['update_field']['actions'] = self::createActionFields($key);
return $edit_fields;
Expand All @@ -41,14 +41,14 @@ public static function editFields($key, $current_fields) {
/**
* Create Type field.
*/
private static function createType($key, $current_fields) {
private static function createType($key, $current_dictionary_fields) {
return [
'#name' => 'field_json_metadata[0][dictionary_fields][data][' . $key . '][field_collection][type]',
'#type' => 'select',
'#required' => TRUE,
'#title' => 'Data type',
'#default_value' => 'string',
'#value' => $current_fields[$key]['type'],
'#value' => $current_dictionary_fields[$key]['type'],
'#op' => 'format_' . $key,
'#options' => FieldOperations::setTypeOptions(),
'#ajax' => [
Expand All @@ -62,16 +62,16 @@ private static function createType($key, $current_fields) {
/**
* Create Format field.
*/
private static function createFormat($key, $current_fields) {
$format_options = FieldOperations::generateFormats($current_fields[$key]['type'], "options");
$value = in_array($current_fields[$key]['format'], $format_options, TRUE) ? $current_fields[$key]['format'] : 'other';
private static function createFormat($key, $current_dictionary_fields) {
$format_options = FieldOperations::generateFormats($current_dictionary_fields[$key]['type'], "options");
$value = in_array($current_dictionary_fields[$key]['format'], $format_options) ? $current_dictionary_fields[$key]['format'] : 'other';
return [
'#name' => 'field_json_metadata[0][dictionary_fields][data][' . $key . '][field_collection][format]',
'#type' => 'select',
'#required' => TRUE,
'#title' => 'Format',
'#default_value' => 'default',
'#description' => FieldOperations::generateFormats($current_fields[$key]['type'], "description"),
'#description' => FieldOperations::generateFormats($current_dictionary_fields[$key]['type'], "description"),
'#value' => $value,
'#prefix' => '<div id = field-json-metadata-' . $key . '-format>',
'#suffix' => '</div>',
Expand All @@ -83,9 +83,9 @@ private static function createFormat($key, $current_fields) {
/**
* Create Format Other field.
*/
private static function createFormatOther($key, $current_fields) {
$format_options = FieldOperations::generateFormats($current_fields[$key]['type'], "options");
$value = !in_array($current_fields[$key]['format'], $format_options) ? $current_fields[$key]['format'] : NULL;
private static function createFormatOther($key, $current_dictionary_fields) {
$format_options = FieldOperations::generateFormats($current_dictionary_fields[$key]['type'], "options");
$value = !in_array($current_dictionary_fields[$key]['format'], $format_options) ? $current_dictionary_fields[$key]['format'] : NULL;

return [
'#name' => 'field_json_metadata[0][dictionary_fields][data][' . $key . '][field_collection][format_other]',
Expand Down Expand Up @@ -119,11 +119,11 @@ private static function createActionFields($key) {
/**
* Create Description field.
*/
private static function createDescriptionField($key, $current_fields) {
private static function createDescriptionField($key, $current_dictionary_fields) {
return [
'#name' => 'field_json_metadata[0][dictionary_fields][data][' . $key . '][field_collection][description]',
'#type' => 'textfield',
'#value' => $current_fields[$key]['description'],
'#value' => $current_dictionary_fields[$key]['description'],
'#required' => TRUE,
'#title' => 'Description',
'#description' => t('Information about the field data.'),
Expand Down
4 changes: 2 additions & 2 deletions modules/data_dictionary_widget/src/Fields/FieldOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public static function setAddFormState($add_new_field, $element) {
* Create edit and update fields where needed.
*/
public static function createDictionaryFieldOptions($op_index, $data_results, $fields_being_modified, $element) {
$current_fields = $element['current_fields'];
$current_fields = $element['current_dictionary_fields'];
// Creating ajax buttons/fields to be placed in correct location later.
foreach ($data_results as $key => $data) {
if (self::checkEditingField($key, $op_index, $fields_being_modified)) {
Expand Down Expand Up @@ -367,4 +367,4 @@ public static function resetFieldValues(array &$form, FormStateInterface $form_s
}
}

}
}
2 changes: 1 addition & 1 deletion modules/data_dictionary_widget/src/Fields/FieldValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class FieldValues {
/**
* Return updated field values after edit.
*/
public static function updateValues($field_index, $update_values, $current_fields) {
public static function updateValues($field_index, $update_values, $current_dictionary_fields) {
$format = $update_values['field_json_metadata'][0]['dictionary_fields']['data'][$field_index]['field_collection']['format'];
$format_other = $update_values['field_json_metadata'][0]['dictionary_fields']['data'][$field_index]['field_collection']['format_other'];
$name = $update_values['field_json_metadata'][0]['dictionary_fields']['data'][$field_index]['field_collection']['name'];
Expand Down
Loading