Skip to content

Commit

Permalink
Handle case where only a single field is being imported
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Oct 31, 2024
1 parent f7d16ce commit 6403f8c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/Transformers/BardTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,35 @@ 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()
Expand Down
38 changes: 38 additions & 0 deletions src/WordPress/Gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,44 @@ 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()
Expand Down

0 comments on commit 6403f8c

Please sign in to comment.