-
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.
Extract field update logic into a class.
- Loading branch information
1 parent
24f962e
commit fc06a03
Showing
3 changed files
with
149 additions
and
149 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,138 @@ | ||
<?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(); | ||
} | ||
|
||
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