-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix error when updating Bard configs inside fieldsets (#22)
* Handle updating bard configs when field is from fieldset * Add tests * Handle case where only a single field is being imported * Fix styling * Update tests. * Add tests to cover importing single fields. * Extract field update logic into a class. * Add comment for future me. * Fix styling --------- Co-authored-by: duncanmcclean <[email protected]>
- Loading branch information
1 parent
86fb0ca
commit d35d87b
Showing
5 changed files
with
375 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
<?php | ||
|
||
namespace Statamic\Importer\Support; | ||
|
||
use Statamic\Facades\Blink; | ||
use Statamic\Facades\Fieldset; | ||
use Statamic\Fields\Blueprint; | ||
use Statamic\Fields\Field; | ||
use Statamic\Support\Str; | ||
|
||
class FieldUpdater | ||
{ | ||
protected $field; | ||
protected $blueprint; | ||
|
||
public function field(Field $field): self | ||
{ | ||
$this->field = $field; | ||
|
||
return $this; | ||
} | ||
|
||
public function blueprint(Blueprint $blueprint): self | ||
{ | ||
$this->blueprint = $blueprint; | ||
|
||
return $this; | ||
} | ||
|
||
public function updateFieldConfig(array $config): void | ||
{ | ||
if ($prefix = $this->field->prefix()) { | ||
$this->updatePrefixedField($prefix, $config); | ||
|
||
return; | ||
} | ||
|
||
if ($importedField = $this->getImportedField()) { | ||
$this->updateImportedField($importedField, $config); | ||
|
||
return; | ||
} | ||
|
||
$this->blueprint->ensureFieldHasConfig( | ||
handle: $this->field->handle(), | ||
config: $config | ||
); | ||
|
||
$this->blueprint->save(); | ||
} | ||
|
||
private function getImportedField(): ?array | ||
{ | ||
return $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->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(); | ||
|
||
$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 | ||
{ | ||
/** @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->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(); | ||
|
||
$this->clearBlinkCaches(); | ||
} | ||
|
||
/** | ||
* 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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.