Adding indexes to your datastore tables can improve response times from common queries.
'),
];
@@ -63,7 +63,7 @@ public static function createIndexDataRows($current_indexes, $index_data_results
'#access' => ((bool) $current_indexes || (bool) $index_data_results),
'#type' => 'table',
'#header' => ['NAME', 'TYPE', 'FIELDS'],
- '#prefix' => '
',
+ '#prefix' => '
',
'#suffix' => '
',
'#rows' => $form_state->get('cancel_index') ? $current_indexes : ($index_data_results ?? []),
'#tree' => TRUE,
diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php
index 598bc1c4f9..4cd203b928 100644
--- a/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php
+++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldEditCreation.php
@@ -3,15 +3,15 @@
namespace Drupal\data_dictionary_widget\Indexes;
/**
- * Various operations for creating Data Dictionary Widget add fields.
+ * Various operations for creating edit fields for indexes.
*/
class IndexFieldEditCreation {
/**
- * Create edit fields for Data Dictionary Widget.
+ * Create edit 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";
+ $id = $current_index_fields ? "field-json-metadata-index-fields" : "field-json-metadata-index-fields-new";
$indexKeyExplode = explode("_", $indexKey);
$edit_index_fields['name'] = [
'#name' => 'field_json_metadata[0][indexes][fields][edit_index_fields][' . $indexKeyExplode[3] . '][name]',
@@ -28,31 +28,53 @@ public static function editIndexFields($indexKey, $current_index_fields, $index_
'#required' => TRUE,
];
- $edit_index_fields['update_index_field']['actions'] = self::createIndexActionFields($indexKey, $id);
+ $edit_index_fields['update_index_field']['actions'] = self::createIndexFieldsActionFields($indexKey, $id);
return $edit_index_fields;
}
/**
- * Create edit fields for Data Dictionary Widget.
+ * Create edit index.
*/
- public static function editIndex($indexKey, $current_index) {
- $id = $current_index ? "field-json-metadata-dictionary-index-fields-new" : "field-json-metadata-dictionary-index-fields";
+ public static function editIndex($indexKey, $current_index, $index_being_modified, $form_state) {
+ $id = $current_index ? "field-json-metadata-index-new" : "field-json-metadata-index";
$indexKeyExplode = explode("_", $indexKey);
- $edit_index['name'] = [
- '#name' => 'field_json_metadata[0][index][data][' . $indexKeyExplode[3] . '][field_collection][name]',
+
+ $edit_index['description'] = [
+ '#name' => "field_json_metadata[0][indexes][edit_index][index_key_" . $indexKeyExplode[2] . "][description]",
'#type' => 'textfield',
- '#value' => $current_index[$indexKeyExplode[3]]['name'],
+ '#value' => $current_index[$indexKeyExplode[2]]['description'],
'#title' => 'Name',
];
- $edit_index['length'] = [
- '#name' => 'field_json_metadata[0][index][data]['. $indexKeyExplode[3] .'][field_collection][length]',
- '#type' => 'number',
- '#value' => $current_index[$indexKeyExplode[3]]['length'],
- '#title' => 'Length',
+ $edit_index['type'] = [
+ '#name' => "field_json_metadata[0][indexes][edit_index][index_key_" . $indexKeyExplode[2] . "][type]",
+ '#type' => 'select',
+ '#description' => t('Index type.'),
+ '#title' => 'Index Type',
+ '#default_value' => 'index',
+ '#op' => 'index_type',
+ '#required' => TRUE,
+ '#options' => [
+ 'index' => t('index'),
+ 'fulltext' => t('fulltext'),
+ ],
+ '#value' => $current_index[$indexKeyExplode[2]]['type'],
];
- $edit_index['update_index_field']['actions'] = self::createIndexActionFields($indexKey, $id );
+ $edit_index['group'] = [
+ '#type' => 'fieldset',
+ '#title' => t('Fields'),
+ '#prefix' => '
',
+ '#suffix' => '
',
+ '#markup' => t('
One or more fields included in index. Must be keys from the fields object.
'),
+ '#attributes' => [
+ 'class' => ['index-fields-form'],
+ ],
+ ];
+
+ $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 );
return $edit_index;
}
@@ -61,11 +83,23 @@ public static function editIndex($indexKey, $current_index) {
* Create Action buttons.
*/
private static function createIndexActionFields($indexKey, $id) {
+ return [
+ '#type' => 'actions',
+ 'save_update' => IndexFieldButtons::submitIndexButton('edit', $indexKey),
+ 'cancel_updates' => IndexFieldButtons::cancelIndexButton('edit', $indexKey, $id),
+ 'delete_index' => IndexFieldButtons::deleteIndexButton($indexKey),
+ ];
+ }
+
+ /**
+ * Create Action buttons.
+ */
+ private static function createIndexFieldsActionFields($indexKey, $id) {
return [
'#type' => 'actions',
'save_update' => IndexFieldButtons::submitIndexFieldButton('edit', $indexKey),
- 'cancel_updates' => IndexFieldButtons::cancelIndexFieldButton('edit', $indexKey, $id),
- 'delete_field' => IndexFieldButtons::deleteIndexButton($indexKey),
+ 'cancel_updates' => IndexFieldButtons::cancelIndexButton('edit', $indexKey, $id),
+ 'delete_field' => IndexFieldButtons::deleteIndexFieldButton($indexKey),
];
}
diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php
index 381472dbbb..e6529c669d 100644
--- a/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php
+++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldOperations.php
@@ -3,62 +3,89 @@
namespace Drupal\data_dictionary_widget\Indexes;
/**
- * Various operations for the Data Dictionary Widget.
+ * Various operations for structuring indexes and fields before rendering.
*/
class IndexFieldOperations {
/**
- * Setting ajax elements.
+ * Setting ajax elements when editing newly added index fields.
*/
- public static function setIndexFieldsAjaxElements(array $dictionaryIndexFields) {
- if ($dictionaryIndexFields["data"]) {
- foreach ($dictionaryIndexFields['data']['#rows'] as $row => $data) {
- $edit_index_button = $dictionaryIndexFields['edit_index_buttons']['index_field_key_' . $row] ?? NULL;
- $edit_index_fields = $dictionaryIndexFields['edit_index_fields']['index_field_key_' . $row] ?? NULL;
+ public static function setIndexFieldsAjaxElementsOnAdd(array $indexFields) {
+ 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;
// Setting the ajax fields if they exsist.
if ($edit_index_button) {
- $dictionaryIndexFields['data']['#rows'][$row] = array_merge($data, $edit_index_button);
- unset($dictionaryIndexFields['edit_index_buttons']['index_field_key_' . $row]);
+ $indexFields['data']['#rows'][$row] = array_merge($data, $edit_index_button);
+ unset($indexFields['edit_index_buttons']['index_field_key_' . $row]);
}
elseif ($edit_index_fields) {
- unset($dictionaryIndexFields['data']['#rows']['index_field_key_' . $row]);
- $dictionaryIndexFields['data']['#rows'][$row]['field_collection'] = $edit_index_fields;
+ unset($indexFields['data']['#rows']['index_field_key_' . $row]);
+ $indexFields['data']['#rows'][$row]['field_collection'] = $edit_index_fields;
// Remove the buttons so they don't show up twice.
- unset($dictionaryIndexFields['edit_index_fields']['index_field_key_' . $row]);
- ksort($dictionaryIndexFields['data']['#rows']);
+ unset($indexFields['edit_index_fields']['index_field_key_' . $row]);
+ ksort($indexFields['data']['#rows']);
}
}
}
- return $dictionaryIndexFields;
+ return $indexFields;
}
/**
- * Setting ajax elements.
+ * Setting ajax elements when editing existing index fields.
*/
- public static function setIndexAjaxElements(array $dictionaryIndexes) {
- foreach ($dictionaryIndexes['data']['#rows'] as $row => $data) {
- $edit_index_button = $dictionaryIndexes['edit_index_buttons']['index_key_' . $row] ?? NULL;
- $edit_index_fields = $dictionaryIndexes['edit_index_fields']['index_key_' . $row] ?? NULL;
+ public static function setIndexFieldsAjaxElements(array $indexFields) {
+ 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 exsist.
+ if ($edit_index_fields_button) {
+ //$indexFields["data"]["#rows"][0] = array_merge($data, $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]);
+ }
+ elseif ($edit_index_fields) {
+ unset($indexFields['data']['#rows']['index_field_key_' . $row]);
+ $indexFields['data']['#rows'][$row]['field_collection'] = $edit_index_fields;
+ // Remove the buttons so they don't show up twice.
+ unset($indexFields['edit_index_fields']['index_field_key_' . $row]);
+ ksort($indexFields['data']['#rows']);
+ }
+ }
+ }
+
+ return $indexFields;
+ }
+
+ /**
+ * Setting index ajax elements.
+ */
+ public static function setIndexAjaxElements(array $indexes) {
+ foreach ($indexes['data']['#rows'] as $row => $data) {
+ $edit_index_button = $indexes['edit_index_buttons']['index_key_' . $row] ?? NULL;
+ $edit_index = $indexes['edit_index']['index_key_' . $row] ?? NULL;
// Setting the ajax fields if they exsist.
if ($edit_index_button) {
- $dictionaryIndexes['data']['#rows'][$row] = array_merge($data, $edit_index_button);
- unset($dictionaryIndexes['edit_index_buttons']['index_key_' . $row]);
+ $indexes['data']['#rows'][$row] = array_merge($data, $edit_index_button);
+ unset($indexes['edit_index_buttons']['index_key_' . $row]);
}
- elseif ($edit_index_fields) {
- unset($dictionaryIndexes['data']['#rows']['index_key_' . $row]);
- $dictionaryIndexes['data']['#rows'][$row]['field_collection'] = $edit_index_fields;
+ elseif ($edit_index) {
+ unset($indexes['data']['#rows']['index_key_' . $row]);
+ $indexes['data']['#rows'][$row]['field_collection'] = $edit_index;
// Remove the buttons so they don't show up twice.
- unset($dictionaryIndexes['edit_index_fields']['index_key_' . $row]);
- ksort($dictionaryIndexes['data']['#rows']);
+ unset($indexes['edit_index']['index_key_' . $row]);
+ ksort($indexes['data']['#rows']);
}
}
- return $dictionaryIndexes;
+ return $indexes;
}
/**
- * Cleaning the data up.
+ * Prepare index field data results.
*/
public static function processIndexFieldsDataResults($index_data_results, $current_index_fields, $index_field_values, $op) {
if (isset($current_index_fields)) {
@@ -84,7 +111,7 @@ public static function processIndexFieldsDataResults($index_data_results, $curre
}
/**
- * Cleaning the data up.
+ * Prepare index data results.
*/
public static function processIndexDataResults($index_results, $current_indexes, $index_values, $index_fields_data_results, $op) {
if (isset($current_indexes)) {
@@ -124,7 +151,18 @@ public static function editIndexActions() {
}
/**
- * Set the elements associated with adding a new field.
+ * Set the elements associated with editing an index.
+ */
+ public static function editIndexFormState($edit_index, $element) {
+ if ($edit_index) {
+ unset($element["indexes"]["edit_index_buttons"]);
+ }
+
+ return $element;
+ }
+
+ /**
+ * Set the elements associated with adding a new index field.
*/
public static function setAddIndexFieldFormState($add_new_index_field, $element) {
if ($add_new_index_field) {
@@ -140,10 +178,11 @@ public static function setAddIndexFieldFormState($add_new_index_field, $element)
}
/**
- * Set the elements associated with adding a new field.
+ * Set the elements associated with adding a new index
*/
public static function setAddIndexFormState($add_new_index, $element) {
if ($add_new_index) {
+ unset($element["indexes"]["edit_index_buttons"]);
$element['indexes']['field_collection'] = $add_new_index;
$element['indexes']['field_collection']['#access'] = TRUE;
$element['indexes']['add_row_button']['#access'] = FALSE;
@@ -155,9 +194,9 @@ public static function setAddIndexFormState($add_new_index, $element) {
}
/**
- * Create edit and update fields where needed.
+ * Create edit and update fields for index fields.
*/
- public static function createDictionaryIndexFieldOptions($op_index, $index_data_results, $index_fields_being_modified, $element) {
+ public static function createIndexFieldOptions($op_index, $index_data_results, $index_fields_being_modified, $element) {
$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) {
@@ -165,6 +204,7 @@ public static function createDictionaryIndexFieldOptions($op_index, $index_data_
$element['edit_index_fields']['index_field_key_' . $indexKey] = IndexFieldEditCreation::editIndexFields('index_field_key_' . $indexKey, $current_index_fields, $index_fields_being_modified);
}
else {
+ //$element['edit_index_fields_buttons']['index_field_key_' . $indexKey]['edit_index_fields_button'] = IndexFieldButtons::editIndexButtons('index_field_key_' . $indexKey);
$element['edit_index_buttons']['index_field_key_' . $indexKey]['edit_index_button'] = IndexFieldButtons::editIndexButtons('index_field_key_' . $indexKey);
}
}
@@ -174,14 +214,15 @@ public static function createDictionaryIndexFieldOptions($op_index, $index_data_
}
/**
- * Create edit and update fields where needed.
+ * Create edit and update fields for indexes.
*/
- public static function createDictionaryIndexOptions($op_index, $index_data_results, $index_fields_being_modified, $element) {
+ public static function createIndexOptions($op_index, $index_data_results, $index_being_modified, $index_field_being_modified, $element, $form_state) {
$current_indexes = $element['current_index'];
+
// Creating ajax buttons/fields to be placed in correct location later.
foreach ($index_data_results as $indexKey => $data) {
- if (self::checkIndexEditingField('index_key_' . $indexKey, $op_index, $index_fields_being_modified)) {
- $element['edit_index']['index_key_' . $indexKey] = IndexFieldEditCreation::editIndex('index_key_' . $indexKey, $current_indexes, $index_fields_being_modified);
+ if (self::checkIndexEditing('index_key_' . $indexKey, $op_index, $index_being_modified)) {
+ $element['edit_index']['index_key_' . $indexKey] = IndexFieldEditCreation::editIndex('index_key_' . $indexKey, $current_indexes, $index_being_modified, $form_state);
}
else {
$element['edit_index_buttons']['index_key_' . $indexKey]['edit_index_button'] = IndexFieldButtons::editIndexButtons('index_key_' . $indexKey);
@@ -193,7 +234,7 @@ public static function createDictionaryIndexOptions($op_index, $index_data_resul
}
/**
- * Return true if field is being edited.
+ * Return true if index field is being edited.
*/
public static function checkIndexEditingField($indexKey, $op_index, $index_fields_being_modified) {
$action_list = IndexFieldOperations::editIndexActions();
@@ -205,4 +246,22 @@ public static function checkIndexEditingField($indexKey, $op_index, $index_field
return FALSE;
}
}
+
+ /**
+ * Return true if index is being edited.
+ */
+ public static function checkIndexEditing($indexKey, $op_index, $index_being_modified) {
+ if (isset($op_index)) {
+ $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;
+ }
+ }
+ }
+
+ return FALSE;
+ }
}
\ No newline at end of file
diff --git a/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php b/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php
index 4a5dcac5e8..66cb7840c6 100644
--- a/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php
+++ b/modules/data_dictionary_widget/src/Indexes/IndexFieldValues.php
@@ -3,7 +3,7 @@
namespace Drupal\data_dictionary_widget\Indexes;
/**
- * Various operations for creating index fields.
+ * Various operations for returning index values when editing the field.
*/
class IndexFieldValues {
/**
@@ -18,4 +18,19 @@ public static function updateIndexFieldValues($field_index, $update_values, $cur
'length' => (int)$length,
];
}
+
+ /**
+ * Return updated index values after edit.
+ */
+ public static function updateIndexValues($field_index, $update_values, $current_index) {
+ $description = $update_values["field_json_metadata"][0]["indexes"]["edit_index"]['index_key_' . $field_index]["description"];
+ $type = $update_values["field_json_metadata"][0]["indexes"]["edit_index"]['index_key_' . $field_index]["type"];
+ $fields = $current_index[$field_index]["fields"];
+
+ return [
+ 'description' => $description,
+ 'type' => $type,
+ 'fields' => $fields,
+ ];
+ }
}
diff --git a/modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php b/modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php
index c5ac9577f8..8fc0837e3a 100644
--- a/modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php
+++ b/modules/data_dictionary_widget/src/Plugin/Field/FieldWidget/DataDictionaryWidget.php
@@ -30,59 +30,80 @@ class DataDictionaryWidget extends WidgetBase implements TrustedCallbackInterfac
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
- // Retrieve values from form state
+ // Retrieve data-dictionary form_state values to be used for various operations.
$dictionary_field_values = $form_state->get("new_dictionary_fields");
- $index_values = $form_state->get('new_index');
- $index_field_values = $form_state->get("new_index_fields");
- $add_index_field = $form_state->get('add_new_index_field');
- $add_new_index = $form_state->get('add_new_index');
$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;
+
+ // 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;
+
+ // Retrieve index fields form_state values to be used for various operations.
$current_index_fields = $form_state->get('current_index_fields');
- $dictionary_fields_being_modified = $form_state->get("dictionary_fields_being_modified") ?? NULL;
+ $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;
+ // Retrieve triggered element to be used for various operations.
$op = $form_state->getTriggeringElement()['#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) : [];
- $op_index = isset($form_state->getTriggeringElement()['#op']) ? explode("_", $form_state->getTriggeringElement()['#op']) : NULL;
- // Retrieve initial data results from field JSON metadata
+ // 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"] ?? [];
- // Process data results
+ // Process data results.
$data_results = FieldOperations::processDataResults($data_results, $current_dictionary_fields, $dictionary_field_values, $op);
$index_fields_data_results = IndexFieldOperations::processIndexFieldsDataResults($index_fields_results, $current_index_fields, $index_field_values, $op);
$index_data_results = IndexFieldOperations::processIndexDataResults($index_results, $current_indexes, $index_values, $index_fields_data_results, $op);
- // Create form elements
+ // Create form elements.
$element = FieldCreation::createGeneralFields($element, $field_json_metadata, $current_dictionary_fields, $form_state);
$element = IndexFieldCreation::createGeneralIndex($element, $current_indexes);
- $element = IndexFieldCreation::createGeneralIndexFields($element);
+ if ($index_field_values || $add_index_field || $index_fields_being_modified) {
+ $element = IndexFieldCreation::createGeneralIndexFields($element);
+ }
- // Add pre-render functions
+ // Add pre-render functions.
$element['dictionary_fields']['#pre_render'] = [[$this, 'preRenderForm']];
$element['indexes']['#pre_render'] = [[$this, 'preRenderIndexForm']];
- $element['indexes']['fields']['#pre_render'] = [[$this, 'preRenderIndexFieldForm']];
+ $element['indexes']['fields']['#pre_render'] = [[$this, 'preRenderIndexFieldFormOnAdd']];
- // Add data rows to display in tables
+ // Add data rows to display in tables.
$element['dictionary_fields']['data'] = FieldCreation::createDictionaryDataRows($current_dictionary_fields, $data_results, $form_state);
$element['indexes']['data'] = IndexFieldCreation::createIndexDataRows($current_indexes, $index_data_results, $form_state);
$element['indexes']['fields']['data'] = IndexFieldCreation::createIndexFieldsDataRows($index_field_values, $current_index_fields, $index_fields_data_results, $form_state);
- // Create dictionary fields/buttons for editing
+ // 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;
- // Create index fields/buttons for editing
- $element['indexes'] = IndexFieldOperations::createDictionaryIndexOptions($op_index, $index_data_results, $index_fields_being_modified, $element['indexes']);
+ // 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_fields_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::createDictionaryIndexFieldOptions($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']);
}
- $element['indexes']['fields']['add_row_button']['#access'] = $index_fields_being_modified == NULL ? TRUE : FALSE;
-
+
+ // Set access to index fields Add button when index fields being modified.
+ $element['indexes']['add_row_button']['#access'] = $index_being_modified == NULL ? TRUE : FALSE;
+
+ // if ($index_field_values || $current_index_fields || $index_being_modified || $index_fields_being_modified) {
+ // $element["indexes"]["edit_index"]["index_key_0"]["group"]["fields"]["fields"] = IndexFieldOperations::createDictionaryIndexFieldOptions($op_index, $index_fields_data_results, $index_fields_being_modified, $element['indexes']['fields']);
+ // $element["indexes"]["edit_index"]["index_key_0"]["group"]["fields"]['#pre_render'] = [[$this, 'preRenderIndexFieldForm']];
+ // //$element["indexes"]['edit_index_fields']["index_field_key_0"]['#pre_render'] = [[$this, 'preRenderIndexFieldForm']];
+ // }
+
// Get form entity
$form_object = $form_state->getFormObject();
if (!($form_object instanceof EntityFormInterface)) {
@@ -96,9 +117,11 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
}
// Set form state for adding fields and indexes
- $element = FieldOperations::setAddFormState($add_new_dictionary_field, $element);
+ $element = FieldOperations::setAddDictionaryFieldFormState($add_new_dictionary_field, $element);
+ $element = FieldOperations::editDictionaryFieldFormState($dictionary_fields_being_modified, $element);
$element = IndexFieldOperations::setAddIndexFormState($add_new_index, $element);
$element = IndexFieldOperations::setAddIndexFieldFormState($add_index_field, $element);
+ $element = IndexFieldOperations::editIndexFormState($index_being_modified, $element);
// Display index fields only when new index fields are being created.
if ($add_index_field || $index_field_values) {
@@ -114,42 +137,40 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
* {@inheritdoc}
*/
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
- $current_dictionary_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"];
- $current_indexes = $form["field_json_metadata"]["widget"][0]["indexes"]["data"]["#rows"];
+ $current_dictionary_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"] ?? [];
+ $current_indexes = $form["field_json_metadata"]["widget"][0]["indexes"]["data"]["#rows"] ?? [];
$field_collection = $values[0]['dictionary_fields']["field_collection"]["group"] ?? [];
$indexes_collection = $values[0]["indexes"]["fields"]["field_collection"]["group"] ?? [];
- $fields_input = !empty($field_collection) ? [
+ if (!empty($indexes_collection) && !empty($indexes_collection["indexes"]["fields"])) {
+ $index_fields = $indexes_collection["indexes"]["fields"];
+ }
+
+ $dictionary_fields_input = !empty($field_collection) ? [
[
- "name" => $field_collection["name"],
- "title" => $field_collection["title"],
- "type" => $field_collection["type"],
- "format" => $field_collection["format"],
- "description" => $field_collection["description"],
+ "name" => $field_collection["name"] ?? '',
+ "title" => $field_collection["title"] ?? '',
+ "type" => $field_collection["type"] ?? '',
+ "format" => $field_collection["format"] ?? '',
+ "description" => $field_collection["description"] ?? '',
],
] : [];
$index_inputs = !empty($indexes_collection) ? [
[
- "name" => $indexes_collection["indexes"]["fields"]["name"],
- "length" => (int)$indexes_collection["indexes"]["fields"]["length"],
+ "name" => $index_fields["name"] ?? '',
+ "length" => isset($index_fields["length"]) ? (int)$index_fields["length"] : 0,
],
] : [];
- if (isset($fields_input)) {
- $fields = array_merge($current_dictionary_fields ?? [], $fields_input);
- }
- else {
- $fields = $current_dictionary_fields ?? [];
- }
-
+ $dictionary_fields = array_merge($current_dictionary_fields ?? [], $dictionary_fields_input);
$indexes = array_merge($current_indexes ?? [], $index_inputs);
$json_data = [
'identifier' => $values[0]['identifier'] ?? '',
'data' => [
'title' => $values[0]['title'] ?? '',
- 'fields' => $fields,
+ 'fields' => $dictionary_fields,
'indexes' => $indexes,
],
];
@@ -175,6 +196,15 @@ public function preRenderIndexFieldForm(array $indexFields) {
return IndexFieldOperations::setIndexFieldsAjaxElements($indexFields);
}
+ /**
+ * Prerender callback for the index field form.
+ *
+ * Moves the buttons into the table.
+ */
+ public function preRenderIndexFieldFormOnAdd(array $indexFields) {
+ return IndexFieldOperations::setIndexFieldsAjaxElementsOnAdd($indexFields);
+ }
+
/**
* Prerender callback for the index form.
*
@@ -188,7 +218,7 @@ public function preRenderIndexForm(array $indexes) {
* {@inheritdoc}
*/
public static function trustedCallbacks() {
- return ['preRenderForm', 'preRenderIndexFieldForm', 'preRenderIndexForm'];
+ return ['preRenderForm', 'preRenderIndexFieldFormOnAdd', 'preRenderIndexFieldForm', 'preRenderIndexForm'];
}
}
diff --git a/modules/data_dictionary_widget/templates/custom-index-fields-table.html.twig b/modules/data_dictionary_widget/templates/custom-index-fields-table.html.twig
index 1b401daf01..436a40437a 100644
--- a/modules/data_dictionary_widget/templates/custom-index-fields-table.html.twig
+++ b/modules/data_dictionary_widget/templates/custom-index-fields-table.html.twig
@@ -6,7 +6,7 @@
{{ header[2] }} |
-
+
{% for row in rows %}
{% if row.field_collection %}
@@ -20,6 +20,7 @@
{{ row.length }} |
{{ row.edit_index_button }}
+ {# {{ row.edit_index_fields_button }} #}
|
{% endif %}
diff --git a/modules/data_dictionary_widget/templates/custom-index-table.html.twig b/modules/data_dictionary_widget/templates/custom-index-table.html.twig
index fe13096e3f..7248bdbeca 100644
--- a/modules/data_dictionary_widget/templates/custom-index-table.html.twig
+++ b/modules/data_dictionary_widget/templates/custom-index-table.html.twig
@@ -6,7 +6,7 @@
{{ header[2] }} |
-
+
{% for row in rows %}
{% if row.field_collection %}
diff --git a/modules/data_dictionary_widget/tests/src/Unit/DataDictionaryWidgetBuildTest.php b/modules/data_dictionary_widget/tests/src/Unit/DataDictionaryWidgetBuildTest.php
index 387c07818d..9e50495603 100644
--- a/modules/data_dictionary_widget/tests/src/Unit/DataDictionaryWidgetBuildTest.php
+++ b/modules/data_dictionary_widget/tests/src/Unit/DataDictionaryWidgetBuildTest.php
@@ -112,7 +112,7 @@ public function testFieldCollectionDictionaryWidget() {
$add_fields = FieldAddCreation::addFields();
- $element = FieldOperations::setAddFormState($add_fields, $element);
+ $element = FieldOperations::setAddDictionaryFieldFormState($add_fields, $element);
$this->assertNotNull($element);
$this->assertNotNull($element["dictionary_fields"]["field_collection"]);
@@ -238,7 +238,7 @@ public function testAddNewFieldDictionaryWidget() {
$add_fields = FieldAddCreation::addFields();
- $element = FieldOperations::setAddFormState($add_fields, $element);
+ $element = FieldOperations::setAddDictionaryFieldFormState($add_fields, $element);
// Set up a triggering element with '#op' set to 'add'.
$trigger = ['#op' => 'add'];
@@ -320,9 +320,9 @@ public function testEditButtonsCreation() {
->method('set')
->with('field_data_type', 'data-dictionary');
- $formState->expects($this->exactly(13))
+ $formState->expects($this->exactly(14))
->method('get')
- ->willReturnOnConsecutiveCalls(NULL, NULL, NULL, FALSE, NULL, NULL, $current_fields);
+ ->willReturnOnConsecutiveCalls(NULL, NULL, $current_fields, FALSE, NULL, NULL, NULL);
$dataDictionaryWidget = new DataDictionaryWidget (
$plugin_id,
@@ -472,15 +472,41 @@ public function testEditDataDictionaryField() {
$formState->expects($this->any())
->method('get')
->willReturnOnConsecutiveCalls(
- [], [], $user_input, [], [], [], [], [], $current_dictionary_fields, [], [], $current_dictionary_fields, [],
- [], [], $user_input, [], [], [], [], [], [], [], [], $updated_dictionary_fields, [], [], $current_dictionary_fields, [],
+ [],
+ $current_dictionary_fields,
+ [],
+ [],
+ [],
+ $current_dictionary_fields,
+ [],
+ [],
+ [],
+ [],
+ [],
+ [],
+ [],
+ [],
+ [],
+ $user_input,
+ [],
+ $current_dictionary_fields,
+ [],
+ [],
+ [],
+ [],
+ $updated_dictionary_fields,
+ [],
+ [],
+ [],
+ [],
+ []
);
$formState->expects($this->any())
->method('getTriggeringElement')
->willReturnOnConsecutiveCalls(
- ['#op' => $op], ['#op' => $op], ['#op' => $op], ['#op' => $op], ['#op' => $op], ['#op' => $op], ['#op' => $op], ['#op' => $op], ['#op' => $op], ['#op' => $op],
- ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'],
+ ['#op' => $op], ['#op' => $op], ['#op' => $op], ['#op' => $op], ['#op' => $op], ['#op' => $op],
+ ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'], ['#op' => 'update_0'],
);
$formState->expects($this->any())