Skip to content

Commit

Permalink
Handle updating bard configs when field is from fieldset
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Oct 31, 2024
1 parent 3d64a75 commit 8e7abce
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 21 deletions.
73 changes: 52 additions & 21 deletions src/Transformers/BardTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Statamic\Importer\Transformers;

use Statamic\Facades\AssetContainer;
use Statamic\Facades\Fieldset;
use Statamic\Fields\Field;
use Statamic\Fieldtypes\Bard\Augmentor as BardAugmentor;
use Statamic\Importer\WordPress\Gutenberg;
Expand Down Expand Up @@ -56,29 +57,59 @@ public function transform(string $value): array

private function enableBardButtons(): void
{
$buttons = [
'h1',
'h2',
'h3',
'bold',
'italic',
'unorderedlist',
'orderedlist',
'removeformat',
'quote',
'anchor',
'image',
'table',
'horizontalrule',
'codeblock',
'underline',
'superscript',
];

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(), [
'container' => $this->field->get('container') ?? AssetContainer::all()->first()?->handle(),
'buttons' => [
'h1',
'h2',
'h3',
'bold',
'italic',
'unorderedlist',
'orderedlist',
'removeformat',
'quote',
'anchor',
'image',
'table',
'horizontalrule',
'codeblock',
'underline',
'superscript',
],
])
config: array_merge($this->field->config(), ['buttons' => $buttons])
);

$this->blueprint->save();
Expand Down
43 changes: 43 additions & 0 deletions src/WordPress/Gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Statamic\Facades\AssetContainer;
use Statamic\Facades\Blueprint as BlueprintFacade;
use Statamic\Facades\Fieldset;
use Statamic\Fields\Blueprint;
use Statamic\Fields\Field;
use Statamic\Fieldtypes\Bard\Augmentor as BardAugmentor;
Expand Down Expand Up @@ -405,6 +406,48 @@ protected static function ensureBardSet(Blueprint $blueprint, Field $field, stri
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(), [
...$field->config(),
Expand Down

0 comments on commit 8e7abce

Please sign in to comment.