Skip to content

Commit

Permalink
Trying to fix array flattening issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dafeder committed Dec 2, 2024
1 parent 5f7c701 commit d1aae54
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
12 changes: 12 additions & 0 deletions modules/json_form_widget/src/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,18 @@ protected function buildSimpleArrayElement(array $definition, $data, array $cont
];
}

/**
* Flatten array element fieldset w/buttons for processing.
*
* @param array $element
* A form element.
*/
public static function flattenArrayElementFieldset(array &$element): void {
if (isset($element['field']) && $element['#type'] == 'fieldset') {
$element = ['#required' => ($element['#required'] ?? FALSE)] + $element['field'];
}
}

/**
* Returns single complex element from array.
*
Expand Down
3 changes: 2 additions & 1 deletion modules/json_form_widget/src/SchemaUiHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function applyOnBaseField(mixed $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->flattenArrays($spec->{"ui:options"}, $element);
// $element = $this->flattenArrays($spec->{"ui:options"}, $element);
}
}
return $element;
Expand All @@ -194,6 +194,7 @@ public function flattenArrays(mixed $spec, array $element) {
unset($element['actions']);
$default_value = [];
foreach ($element[$spec->child] as $key => $item) {
ArrayHelper::flattenArrayElementFieldset($item);
$default_value = array_merge($default_value, $this->formatArrayDefaultValue($item));
if ($key != 0) {
unset($element[$spec->child][$key]);
Expand Down
6 changes: 1 addition & 5 deletions modules/json_form_widget/src/WidgetRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,7 @@ public function handleListElement(mixed $spec, array $element) {
* The dropdown element configured.
*/
public function getDropdownElement(mixed $element, mixed $spec, mixed $titleProperty = FALSE) {
// Array elements may be fieldsets, since by default they are simple array
// elements with Remove/Up/Down buttons.
if (isset($element['field']) && $element['#type'] == 'fieldset') {
$element = ['#required' => ($element['#required'] ?? FALSE)] + $element['field'];
}
ArrayHelper::flattenArrayElementFieldset($element);
$element['#type'] = $this->getSelectType($spec);
$element['#options'] = $this->getDropdownOptions($spec->source, $titleProperty);
if ($element['#type'] === 'select_or_other_select') {
Expand Down

0 comments on commit d1aae54

Please sign in to comment.