Skip to content

Commit

Permalink
feat(structure-form-group): improve StructureFormGroup.php
Browse files Browse the repository at this point in the history
Added support to prioritize "form_group" over "default_form_group" to allow for custom one-off form groups.
  • Loading branch information
JoelAlphonso authored and mcaskill committed Mar 5, 2024
1 parent 8ad05a0 commit c2f385e
Showing 1 changed file with 39 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -427,26 +427,7 @@ protected function finalizeStructure($reload = false)
if ($reload || !$this->isStructureFinalized) {
$this->isStructureFinalized = true;

$property = $this->storageProperty();

$struct = $property->getStructureMetadata();
$formGroup = null;
if (isset($struct['admin']['default_form_group'])) {
$groupName = $struct['admin']['default_form_group'];
if (isset($struct['admin']['form_groups'][$groupName])) {
$formGroup = $struct['admin']['form_groups'][$groupName];
}
} elseif (isset($struct['admin']['form_group'])) {
if (is_string($struct['admin']['form_group'])) {
$groupName = $struct['admin']['form_group'];
if (isset($struct['admin']['form_groups'][$groupName])) {
$formGroup = $struct['admin']['form_groups'][$groupName];
}
} else {
$formGroup = $struct['admin']['form_group'];
}
}

$formGroup = $this->findStructureFormGroup();
if ($formGroup) {
if (is_array($this->rawData)) {
$widgetData = array_replace($formGroup, $this->rawData);
Expand All @@ -458,6 +439,38 @@ protected function finalizeStructure($reload = false)
}
}

/**
* @return ?array<string, mixed>
*/
protected function findStructureFormGroup(): ?array
{
$struct = $this->storageProperty()->getStructureMetadata();

$formGroup = null;
if (isset($struct['admin']['form_group'])) {
if (\is_string($struct['admin']['form_group'])) {
$groupName = $struct['admin']['form_group'];
if (isset($struct['admin']['form_groups'][$groupName])) {
return $struct['admin']['form_groups'][$groupName];
}
} else {
return $struct['admin']['form_group'];
}
}

if (isset($struct['admin']['default_form_group'])) {
$groupName = $struct['admin']['default_form_group'];
if (
\is_string($groupName) &&
isset($struct['admin']['form_groups'][$groupName])
) {
return $struct['admin']['form_groups'][$groupName];
}
}

return null;
}

/**
* Parse the form group and model properties.
*
Expand Down Expand Up @@ -532,6 +545,11 @@ public function formProperties()
$entry = $store->parseVal($entry);
}

$entry = $store->structureVal($entry, [ 'default_data' => true ]);
if (!$entry) {
$entry = clone $store->structureProto();
}

$propertyIdentPattern = '%1$s[%2$s]';

$propPreferences = $this->propertiesOptions();
Expand Down Expand Up @@ -582,7 +600,7 @@ public function formProperties()
}
}

if (!empty($entry) && isset($entry[$propertyIdent])) {
if ($entry && isset($entry[$propertyIdent])) {
$val = $entry[$propertyIdent];
$formProperty->setPropertyVal($val);
}
Expand Down

0 comments on commit c2f385e

Please sign in to comment.