Skip to content

Commit

Permalink
Simplify fieldset ensuring logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Nov 12, 2024
1 parent 1af9ac9 commit 479077b
Showing 1 changed file with 4 additions and 34 deletions.
38 changes: 4 additions & 34 deletions src/Support/FieldUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,7 @@ private function updateImportedField(array $importedField, array $config): void
$fieldHandle = Str::after($importedField['field'], '.');
$fieldset = Fieldset::find(Str::before($importedField['field'], '.'));

$fieldset->setContents([
...$fieldset->contents(),
'fields' => collect($fieldset->contents()['fields'])
->map(function (array $field) use ($config, $fieldHandle) {
if ($field['handle'] === $fieldHandle) {
return [
'handle' => $field['handle'],
'field' => $config,
];
}

return $field;
})
->all(),
]);

$fieldset->save();
$fieldset->ensureFieldHasConfig($fieldHandle, $config)->save();

$this->clearBlinkCaches();
}
Expand All @@ -98,6 +82,8 @@ private function updateImportedField(array $importedField, array $config): void
*/
private function updatePrefixedField(string $prefix, array $config): void
{
$fieldHandle = Str::after($this->field->handle(), $prefix);

/** @var \Statamic\Fields\Fieldset $fieldset */
$fieldset = $this->blueprint->fields()->items()
->filter(fn (array $field) => isset($field['import']))
Expand All @@ -109,23 +95,7 @@ private function updatePrefixedField(string $prefix, array $config): void
})
->first();

$fieldset->setContents([
...$fieldset->contents(),
'fields' => collect($fieldset->contents()['fields'])
->map(function (array $field) use ($config, $prefix) {
if ($field['handle'] === Str::after($this->field->handle(), $prefix)) {
return [
'handle' => $field['handle'],
'field' => $config,
];
}

return $field;
})
->all(),
]);

$fieldset->save();
$fieldset->ensureFieldHasConfig($fieldHandle, $config)->save();

$this->clearBlinkCaches();
}
Expand Down

0 comments on commit 479077b

Please sign in to comment.