8000 Add index definitions to the data dictionary form/Separate index form by beeyayjay · Pull Request #4375 · GetDKAN/dkan · GitHub
[go: up one dir, main page]

Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
2c2d0e4
Initial commit of updates to include indexes and index fields to the …
kaise-lafrai Apr 29, 2024
5396653
Added working cancel button for index, fixed errors appearing in log
kaise-lafrai May 14, 2024
bb42cf8
Removed unused code, fixed index fields cancel button
kaise-lafrai May 20, 2024
5e0ee4c
Added validation for index fields form, refactored code, refactored t…
kaise-lafrai May 24, 2024
e529acc
Fully functional edit experience for index fields (#4204)
kaise-lafrai Jul 8, 2024
620bced
Editable Index (#4239)
kaise-lafrai Aug 1, 2024
2250243
Index UI unit tests (#4262)
dmundra Aug 19, 2024
b7f844b
Updated data dictionary widget functional test to test all fields in …
dmundra Aug 29, 2024
b10bd49
Added Indexes edit unit tests (#4266)
dmundra Aug 30, 2024
567107f
Some simple code cleanup (white space, removal of commented out code,…
dmundra Sep 5, 2024
a9ccde9
WCMS-20047: Data Dictionary Documentation (#4271)
rovcase Sep 6, 2024
bb76f74
Data dictionary indexes code refactoring (#4280)
dmundra Sep 10, 2024
c7d7cfe
Reverted changes to checkIndexEditingField and checkIndexEditing. Upd…
dmundra Sep 10, 2024
4b4cf50
Adding code comments. (#4289)
dmundra Sep 18, 2024
ea018c7
Dictionary-Widget: Streamline the button labels for the Index and Ind…
rovcase Oct 24, 2024
ffb4d44
Provisional commit
Nov 27, 2024
ae7e367
In progress
Dec 3, 2024
a320129
Added indexes to admin view.
Dec 3, 2024
53fe56f
Temp save
Dec 4, 2024
0d46835
Temp save
Dec 4, 2024
2f45d07
Temp save
Dec 9, 2024
ec4fdf8
Temp save
Dec 9, 2024
058a91a
Index keyname changes and bug fixes.
Dec 12, 2024
1f3c275
Other indexes.
Jan 2, 2025
982d12f
Index edit works.
Jan 5, 2025
326d0b5
Add index field and cancel work.
Jan 5, 2025
69ee91f
Some cleanup.
Jan 6, 2025
622f4a9
More stuff
Jan 6, 2025
c9b743b
Some cleanup
Jan 6, 2025
3e82443
Updated data dictionary admin list. Added 'Add index' link.
Jan 6, 2025
a3bf332
Removed test field and made index length optional.
Jan 6, 2025
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
Prev Previous commit
Next Next commit
In progress
  • Loading branch information
Beth Jacobson committed Jan 6, 2025
commit ae7e367459688ee8914eda118f13dcc27d787def
15 changes: 12 additions & 3 deletions modules/data_dictionary_widget/data_dictionary_widget.module
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,18 @@ function data_dictionary_widget_theme($existing, $type, $theme, $path) {
*/
function data_dictionary_widget_entity_form_display_alter(EntityFormDisplayInterface $form_display, array $context) {
if (data_dictionary_widget__data_dictionary_data_type_checker($context) === 'data-dictionary') {
$form_display->setComponent('field_json_metadata', [
'type' => 'data_dictionary_widget',
]);
$index = \Drupal::request()->query->get('index');
if (isset($index)) {
$form_display->setComponent('field_json_metadata', [
'type' => 'data_index_widget',
'settings' => ['index' => $index],
]);
}
else {
$form_display->setComponent('field_json_metadata', [
'type' => 'data_dictionary_widget',
]);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\data_dictionary_widget\Fields\FieldCreation;
use Drupal\data_dictionary_widget\Fields\FieldOperations;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\data_dictionary_widget\Indexes\IndexFieldCreation;
use Drupal\data_dictionary_widget\Indexes\IndexFieldOperations;

/**
* A data-dictionary widget.
Expand All @@ -37,7 +34,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
$field_json_metadata = !empty($items[0]->value) ? json_decode($items[0]->value, TRUE) : [];

// Retrieve initial data results from field JSON metadata.
$data_results = $this->getFieldResults($field_json_metadata);
$data_results = $field_json_metadata['data']['fields'] ?? [];

// Process data results.
$data_results = $this->processDataResults($data_results, $current_fields, $field_values, $op);
Expand All @@ -49,10 +46,10 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
$element['dictionary_fields']['#pre_render'] = [[$this, 'preRenderForm']];

// Add data rows to display in tables.
$element['dictionary_fields']['data'] = $this->createDictionaryDataRows($current_fields, $data_results, $form_state);
$element['dictionary_fields']['data'] = $this->createDataRows($current_fields, $data_results, $form_state);

// Create dictionary fields/buttons for editing.
$element['dictionary_fields'] = $this->createDictionaryFieldOptions($op_index, $data_results, $fields_being_modified, $element['dictionary_fields']);
$element['dictionary_fields'] = $this->createFieldOptions($op_index, $data_results, $fields_being_modified, $element['dictionary_fields']);
$element['dictionary_fields']['add_row_button']['#access'] = $fields_being_modified == NULL;

// Get form entity.
Expand All @@ -67,24 +64,13 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
$form_entity->set('field_data_type', 'data-dictionary');
}

// Set form state for adding fields and indexes.
// Set form state for adding and editing fields.
$element = $this->setAddDictionaryFieldFormState($add_new_field, $element);
$element = $this->editDictionaryFieldFormState($fields_being_modified, $element);

return $element;
}

/**
* Get field array from json metadata
*
* @param array $field_json_metadata
* The json metadata array
*
* @return array
* Array asociative array of field names and field values.
*/
abstract protected function getFieldResults(array $field_json_metadata) : array;

/**
* Cleaning the data up.
*/
Expand Down Expand Up @@ -181,6 +167,6 @@ protected static function checkEditingField($key, $op_index, $fields_being_modif
}
}

abstract protected static function createDictionaryDataRows($current_dictionary_fields, $data_results, $form_state);
abstract protected static function createDataRows($current_dictionary_fields, $data_results, $form_state);

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\data_dictionary_widget\Fields\FieldButtons;
use Drupal\data_dictionary_widget\Fields\FieldCreation;
use Drupal\data_dictionary_widget\Fields\FieldEditCreation;
use Drupal\data_dictionary_widget\Fields\FieldOperations;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\data_dictionary_widget\Indexes\IndexFieldCreation;
use Drupal\data_dictionary_widget\Indexes\IndexFieldOperations;

/**
* A data-dictionary widget.
Expand All @@ -29,13 +25,6 @@
*/
class DataDictionaryWidget extends AbstractMetadataWidget implements TrustedCallbackInterface {

/**
* @inheritDoc
*/
protected function getFieldResults(array $field_json_metadata) : array {
return $field_json_metadata['data']['fields'] ?? [];
}

protected function processDataResults($data_results, $current_fields, $field_values, $op) {
if (isset($current_fields)) {
$data_results = $current_fields;
Expand Down Expand Up @@ -254,18 +243,6 @@ public function preRenderForm(array $dictionaryFields) {
// return IndexFieldOperations::setIndexAjaxElements($indexes);
// }

/**
* {@inheritdoc}
*/
public static function trustedCallbacks() {
return [
'preRenderForm',
// 'preRenderIndexFieldFormOnAdd',
// 'preRenderIndexFieldForm',
// 'preRenderIndexForm',
];
}

/**
* {@inheritdoc}
*/
Expand All @@ -292,7 +269,7 @@ protected function createGeneralFields($element, $field_json_metadata, $current_
/**
* @inheritDoc
*/
protected function createDictionaryFieldOptions($op_index, $data_results, $fields_being_modified, $element) {
protected function createFieldOptions($op_index, $data_results, $fields_being_modified, $element) {
// 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 @@ -336,7 +313,7 @@ protected function editDictionaryFieldFormState($fields_being_modified, $element
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
$current_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"];
$field_collection = $values[0]['dictionary_fields']["field_collection"]["group"] ?? [];
$indexes = isset($values[0]["indexes"]) ? json_decode($values[0]["indexes"]) : NULL;
$indexes = isset($values[0]["indexes"]) ? json_decode($values[0]["indexes"]) : [];

$data_results = !empty($field_collection) ? [
[
Expand All @@ -355,14 +332,14 @@ public function massageFormValues(array $values, array $form, FormStateInterface
'data' => [
'title' => $values[0]['title'] ?? '',
'fields' => $updated,
'indexes' => $indexes ?? [],
'indexes' => $indexes,
],
];

return json_encode($json_data);
}

protected static function createDictionaryDataRows($current_dictionary_fields, $data_results, $form_state) {
protected static function createDataRows($current_dictionary_fields, $data_results, $form_state) {

return [
'#access' => ((bool) $current_dictionary_fields || (bool) $data_results),
Expand All @@ -375,4 +352,23 @@ protected static function createDictionaryDataRows($current_dictionary_fields, $

}

/**
* @inheritDoc
*/
protected function createDictionaryFieldOptions($op_index, $data_results, $fields_being_modified, $element) {
$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)) {
$element['edit_fields'][$key] = FieldEditCreation::editFields($key, $current_fields, $fields_being_modified);
}
else {
$element['edit_buttons'][$key]['edit_button'] = FieldButtons::editButtons($key);
}
}
$element['add_row_button'] = FieldButtons::addButton();

return $element;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace Drupal\data_dictionary_widget\Plugin\Field\FieldWidget;

use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\data_dictionary_widget\Fields\FieldCreation;
use Drupal\data_dictionary_widget\Fields\FieldOperations;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\data_dictionary_widget\Indexes\IndexFieldCreation;
use Drupal\data_dictionary_widget\Indexes\IndexFieldOperations;

/**
* A data-dictionary widget.
*
* @FieldWidget(
* id = "data_index_widget",
* label = @Translation("Data-Dictionary Index Widget"),
* field_types = {
* "string_long"
* }
* )
*/
class DataIndexWidget extends AbstractMetadataWidget implements TrustedCallbackInterface {

/**
* {@inheritdoc}
*/
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
$current_fields = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["data"]["#rows"] ?? [];
$field_collection = $values[0]["indexes"]["fields"]["field_collection"]["group"] ?? [];
$dictionary_collection = $values[0]['dictionary_fields']["field_collection"]["group"] ?? [];

$data_results = !empty($field_collection) ? [
[
"name" => $index_fields["name"] ?? '',
"length" => isset($index_fields["length"]) ? (int) $index_fields["length"] : 0,
],
] : [];

$updated = array_merge($current_fields ?? [], $data_results);

$json_data = [
'identifier' => $values[0]['identifier'] ?? '',
'data' => [
'title' => $values[0]['title'] ?? '',
'fields' => $dictionary_collection,
'indexes' => $updated,
],
];

return json_encode($json_data);
}

/**
* Prerender callback for the index field form.
*
* Moves the buttons into the table.
*/
public function preRenderForm(array $dictionaryFields) {
return IndexFieldOperations::setIndexFieldsAjaxElements($dictionaryFields);
}

// /**
// * Prerender callback for the index field form.
// *
// * Moves the buttons into the table.
// */
// public function preRenderIndexFieldFormOnAdd(array $indexFields) {
// return IndexFieldOperations::setIndexFieldsAjaxElementsOnAdd($indexFields);
// }
//
// /**
// * {@inheritdoc}
// */
// public static function trustedCallbacks() {
// return [
// 'preRenderForm',
// 'preRenderIndexFieldFormOnAdd',
// 'preRenderIndexFieldForm',
// 'preRenderIndexForm',
// ];
// }


/**
* @inheritDoc
*/
protected function processDataResults($data_results, $current_fields, $field_values, $op) {
return IndexFieldOperations::processIndexFieldsDataResults($data_results, $current_fields, $field_values, $op);
}

protected function createGeneralFields($element, $field_json_metadata, $current_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'] = [
'#type' => 'fieldset',
'#title' => t('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>'),
'#required' => TRUE,
];
$element['dictionary_fields']['current_dictionary_fields'] = $current_fields;

return $element;
}

/**
* @inheritDoc
*/
protected function createDictionaryFieldOptions($op_index, $data_results, $fields_being_modified, $element) {
// TODO: Implement createDictionaryFieldOptions() method.
}

protected function setAddDictionaryFieldFormState($add_new_field, $element) {
// TODO: Implement setAddDictionaryFieldFormState() method.
}

/**
* @inheritDoc
*/
protected function editDictionaryFieldFormState($fields_being_modified, $element) {
// TODO: Implement editDictionaryFieldFormState() method.
}

protected static function createDataRows($current_dictionary_fields, $data_results, $form_state) {
// TODO: Implement createDataRows() method.
}

}
0