Skip to content

Commit

Permalink
Extract field update logic into a class.
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Nov 12, 2024
1 parent 24f962e commit fc06a03
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 149 deletions.
138 changes: 138 additions & 0 deletions src/Support/FieldUpdater.php
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();
}
}
70 changes: 4 additions & 66 deletions src/Transformers/BardTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Statamic\Facades\Fieldset;
use Statamic\Fields\Field;
use Statamic\Fieldtypes\Bard\Augmentor as BardAugmentor;
use Facades\Statamic\Importer\Support\FieldUpdater;
use Statamic\Importer\WordPress\Gutenberg;
use Statamic\Support\Str;

Expand Down Expand Up @@ -76,72 +77,9 @@ private function enableBardButtons(): void
'superscript',
];

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

if ($importedField) {
/** @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 ($buttons, $fieldHandle) {
if ($field['handle'] === $fieldHandle) {
return [
'handle' => $field['handle'],
'field' => array_merge($field['field'], ['buttons' => $buttons]),
];
}

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

return;
}

if ($prefix = $this->field->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->setContents([
...$fieldset->contents(),
'fields' => collect($fieldset->contents()['fields'])
->map(function (array $field) use ($buttons, $prefix) {
if ($field['handle'] === Str::after($this->field->handle(), $prefix)) {
return [
'handle' => $field['handle'],
'field' => array_merge($field['field'], ['buttons' => $buttons]),
];
}

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

return;
}

$this->blueprint->ensureFieldHasConfig(
handle: $this->field->handle(),
config: array_merge($this->field->config(), ['buttons' => $buttons])
);

$this->blueprint->save();
FieldUpdater::field($this->field)
->blueprint($this->blueprint)
->updateFieldConfig(array_merge($this->field->config(), ['buttons' => $buttons]));
}

private function isGutenbergValue(string $value): bool
Expand Down
90 changes: 7 additions & 83 deletions src/WordPress/Gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Statamic\Importer\WordPress;

use Facades\Statamic\Importer\Support\FieldUpdater;
use Statamic\Facades\AssetContainer;
use Statamic\Facades\Blink;
use Statamic\Facades\Blueprint as BlueprintFacade;
use Statamic\Facades\Fieldset;
use Statamic\Fields\Blueprint;
Expand Down Expand Up @@ -399,7 +401,7 @@ protected static function renderHtmlToProsemirror(Field $field, string $html)

protected static function ensureBardSet(Blueprint $blueprint, Field $field, string $handle, array $config): void
{
$blueprint = BlueprintFacade::find("{$blueprint->namespace()}.{$blueprint->handle()}");
$blueprint = BlueprintFacade::find($blueprint->fullyQualifiedHandle());
$field = $blueprint->field($field->handle());

$setExists = collect($field->get('sets', []))->contains(
Expand All @@ -410,86 +412,9 @@ protected static function ensureBardSet(Blueprint $blueprint, Field $field, stri
return;
}

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

if ($importedField) {
/** @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 $fieldsetField) use ($field, $handle, $config, $fieldHandle) {
if ($fieldsetField['handle'] === $fieldHandle) {
return [
'handle' => $fieldsetField['handle'],
'field' => array_merge($fieldsetField['field'], [
...$field->config(),
'sets' => array_merge($field->get('sets', []), [
'main' => array_merge($field->get('sets.main', []), [
'sets' => array_merge($field->get('sets.main.sets', []), [
$handle => $config,
]),
]),
]),
]),
];
}

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

return;
}

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

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

return $fieldsetField;
})
->all(),
])->save();

return;
}

$blueprint
->ensureFieldHasConfig($field->handle(), [
FieldUpdater::field($field)
->blueprint($blueprint)
->updateFieldConfig([
...$field->config(),
'sets' => array_merge($field->get('sets', []), [
'main' => array_merge($field->get('sets.main', []), [
Expand All @@ -498,7 +423,6 @@ protected static function ensureBardSet(Blueprint $blueprint, Field $field, stri
]),
]),
]),
])
->save();
]);
}
}

0 comments on commit fc06a03

Please sign in to comment.