Skip to content

Commit

Permalink
Support choosing data dictionaries in dataset node form (#4067)
Browse files Browse the repository at this point in the history
  • Loading branch information
dafeder authored Apr 12, 2024
1 parent 117b5f9 commit 48ce39c
Show file tree
Hide file tree
Showing 13 changed files with 362 additions and 64 deletions.
2 changes: 1 addition & 1 deletion cypress/support/helpers/dkan.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ export function generateKeyword(uuid) {
export function generateDataDictionary(uuid) {
return {
"identifier": uuid,
"title": "Title for " + uuid,
"data": {
"title": "Title for " + uuid,
"fields": [
{
"name": generateRandomString(),
Expand Down
2 changes: 1 addition & 1 deletion docs/source/user-guide/guide_data_dictionaries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ We will define a list of fields based on the example header row below.
Authorization: Basic username:password

{
"title": "Demo Dictionary",
"data": {
"title": "Demo Dictionary",
"fields": [
{
"name": "project_id",
Expand Down
2 changes: 1 addition & 1 deletion modules/common/tests/src/Traits/GetDataTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ private function getDataset(string $identifier, string $title, array $downloadUr
private function getDataDictionary(array $fields, array $indexes, string $identifier, string $title = 'Test DataDict') {
return json_encode([
'identifier' => $identifier,
'title' => $title,
'data' => [
'title' => $title,
'fields' => $fields,
'indexes' => $indexes,
],
Expand Down
36 changes: 35 additions & 1 deletion modules/json_form_widget/src/SchemaUiHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,46 @@ public function applyOnBaseField($spec, array $element) {
$element = $this->changeFieldDescriptions($spec->{"ui:options"}, $element);
$element = $this->changeFieldTitle($spec->{"ui:options"}, $element);
if (isset($spec->{"ui:options"}->hideActions)) {
$element = $this->widgetRouter->flattenArrays($spec->{"ui:options"}, $element);
$element = $this->flattenArrays($spec->{"ui:options"}, $element);
}
}
return $element;
}

/**
* Flatten array elements and unset actions if hideActions is set.
*
* @param mixed $spec
* Object with spec for UI options.
* @param array $element
* Element to apply UI options.
*
* @return array
* Return flattened element without actions.
*/
public function flattenArrays($spec, array $element) {
unset($element['actions']);
$default_value = [];
foreach ($element[$spec->child] as $key => $item) {
$default_value = array_merge($default_value, $this->formatArrayDefaultValue($item));
if ($key != 0) {
unset($element[$spec->child][$key]);
}
}
$element[$spec->child][0]['#default_value'] = $default_value;
return $element;
}

/**
* Format default values for arrays (flattened).
*/
private function formatArrayDefaultValue($item) {
if (!empty($item['#default_value'])) {
return [$item['#default_value'] => $item['#default_value']];
}
return [];
}

/**
* Apply schema UI to object fields.
*
Expand Down
107 changes: 59 additions & 48 deletions modules/json_form_widget/src/WidgetRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,40 +102,6 @@ public function getWidgets() {
];
}

/**
* Flatten array elements and unset actions if hideActions is set.
*
* @param mixed $spec
* Object with spec for UI options.
* @param array $element
* Element to apply UI options.
*
* @return array
* Return flattened element without actions.
*/
public function flattenArrays($spec, array $element) {
unset($element['actions']);
$default_value = [];
foreach ($element[$spec->child] as $key => $item) {
$default_value = array_merge($default_value, $this->formatArrayDefaultValue($item));
if ($key != 0) {
unset($element[$spec->child][$key]);
}
}
$element[$spec->child][0]['#default_value'] = $default_value;
return $element;
}

/**
* Format default values for arrays (flattened).
*/
private function formatArrayDefaultValue($item) {
if (!empty($item['#default_value'])) {
return [$item['#default_value'] => $item['#default_value']];
}
return [];
}

/**
* Handle configuration for list elements.
*
Expand All @@ -148,14 +114,19 @@ private function formatArrayDefaultValue($item) {
* The element configured as a list element.
*/
public function handleListElement($spec, array $element) {
if (isset($spec->titleProperty)) {
if (isset($element[$spec->titleProperty])) {
$element[$spec->titleProperty] = $this->getDropdownElement($element[$spec->titleProperty], $spec, $spec->titleProperty);
}
$title_property = ($spec->titleProperty ?? FALSE);

if (isset($title_property, $element[$title_property])) {
$element[$title_property] = $this->getDropdownElement($element[$title_property], $spec, $title_property);
}
else {

if (isset($spec->source->returnValue)) {
$element = $this->getDropdownElement($element, $spec, $title_property);
}
elseif (!isset($spec->titleProperty)) {
$element = $this->getDropdownElement($element, $spec);
}

return $element;
}

Expand Down Expand Up @@ -245,19 +216,59 @@ public function getDropdownOptions($source, $titleProperty = FALSE) {
*/
public function getOptionsFromMetastore($source, $titleProperty = FALSE) {
$options = [];
$values = $this->metastore->getAll($source->metastoreSchema);
foreach ($values as $value) {
$value = json_decode($value);
if ($titleProperty) {
$options[$value->data->{$titleProperty}] = $value->data->{$titleProperty};
}
else {
$options[$value->data] = $value->data;
}
$metastore_items = $this->metastore->getAll($source->metastoreSchema);
foreach ($metastore_items as $item) {
$item = json_decode($item);
$title = $this->metastoreOptionTitle($item, $source, $titleProperty);
$value = $this->metastoreOptionValue($item, $source, $titleProperty);
$options[$value] = $title;
}
return $options;
}

/**
* Determine the title for the select option.
*
* @param object|string $item
* Single item from Metastore::getAll()
* @param object $source
* Source defintion from UI schema.
* @param string|false $titleProperty
* Title property defined in UI schema.
*
* @return string
* String to be used in title.
*/
private function metastoreOptionTitle($item, object $source, $titleProperty): string {
if ($titleProperty) {
return is_object($item) ? $item->data->$titleProperty : $item;
}
return $item->data;
}

/**
* Determine the value for the select option.
*
* @param object|string $item
* Single item from Metastore::getAll()
* @param object $source
* Source defintion from UI schema.
* @param string|false $titleProperty
* Title property defined in UI schema.
*
* @return string
* String to be used as option value.
*/
private function metastoreOptionValue($item, object $source, $titleProperty): string {
if (($source->returnValue ?? NULL) == 'url') {
return 'dkan://metastore/schemas/' . $source->metastoreSchema . '/items/' . $item->identifier;
}
if ($titleProperty) {
return is_object($item) ? $item->data->$titleProperty : $item;
}
return $item->data;
}

/**
* Helper function to add the value of other to current list of options.
*/
Expand Down
Loading

0 comments on commit 48ce39c

Please sign in to comment.