Skip to content

Commit

Permalink
Simplify some more.
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Nov 12, 2024
1 parent 479077b commit 58dd249
Showing 1 changed file with 31 additions and 60 deletions.
91 changes: 31 additions & 60 deletions src/Support/FieldUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ public function blueprint(Blueprint $blueprint): self

public function updateFieldConfig(array $config): void
{
if ($prefix = $this->field->prefix()) {
$this->updatePrefixedField($prefix, $config);
return;
}
if ([$fieldset, $fieldHandle] = $this->fieldImportedFrom($config)) {
$fieldset->ensureFieldHasConfig($fieldHandle, $config)->save();

Blink::store('blueprints.found')->flush();
Blink::store('blueprints.from-file')->flush();

if ($importedField = $this->getImportedField()) {
$this->updateImportedField($importedField, $config);
return;
}

Expand All @@ -47,66 +46,38 @@ public function updateFieldConfig(array $config): void
$this->blueprint->save();
}

private function getImportedField(): ?array
private function fieldImportedFrom(array $config): ?array
{
return $this->blueprint->fields()->items()
if ($prefix = $this->field->prefix()) {
$fieldHandle = Str::after($this->field->handle(), $prefix);

/** @var \Statamic\Fields\Fieldset $fieldset */
$fieldset = $this->blueprint->fields()->items()
->filter(fn (array $field) => isset($field['import']))
->map(fn (array $field) => Fieldset::find($field['import']))
->filter(function ($fieldset) use ($prefix) {
return collect($fieldset->fields()->items())
->where('handle', Str::after($this->field->handle(), $prefix))
->isNotEmpty();
})
->first();

return [$fieldset, $fieldHandle];
}

$importedField = $this->blueprint->fields()->items()
->where('handle', $this->field->handle())
->filter(fn (array $field) => isset($field['field']) && is_string($field['field']))
->first();
}

/**
* This method handles updating imported fields from fieldsets.
*
* -
* handle: foo
* field: fieldset.foo
*/
private function updateImportedField(array $importedField, array $config): void
{
/** @var \Statamic\Fields\Fieldset $fieldset */
$fieldHandle = Str::after($importedField['field'], '.');
$fieldset = Fieldset::find(Str::before($importedField['field'], '.'));

$fieldset->ensureFieldHasConfig($fieldHandle, $config)->save();
if ($importedField) {
/** @var \Statamic\Fields\Fieldset $fieldset */
$fieldHandle = Str::after($importedField['field'], '.');
$fieldset = Fieldset::find(Str::before($importedField['field'], '.'));

$this->clearBlinkCaches();
}

/**
* This method handles updating imported fields from fieldsets, which use a prefix.
*
* -
* import: fieldset
* prefix: foo_
*/
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']))
->map(fn (array $field) => Fieldset::find($field['import']))
->filter(function ($fieldset) use ($prefix) {
return collect($fieldset->fields()->items())
->where('handle', Str::after($this->field->handle(), $prefix))
->isNotEmpty();
})
->first();

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

$this->clearBlinkCaches();
}
return [$fieldset, $fieldHandle];
}

/**
* When fieldsets are updated, we need to clear the Blueprint Blink caches, so
* Blueprint::find() returns the updated field config.
*/
private function clearBlinkCaches(): void
{
Blink::store('blueprints.found')->flush();
Blink::store('blueprints.from-file')->flush();
return null;
}
}

0 comments on commit 58dd249

Please sign in to comment.