Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Fields/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,7 @@ private function addEnsuredFieldToContents($contents, $ensured)
}
}

$targetSectionIndex = $existingField['section']
?? ($prepend ? 0 : count($contents['tabs'][$tab]['sections'] ?? []) - 1);
$targetSectionIndex = $existingField['section'] ?? 0;

$fields = collect($tabs[$tab]['sections'][$targetSectionIndex]['fields'] ?? [])->keyBy(function ($field) {
return (isset($field['import'])) ? 'import:'.($field['prefix'] ?? null).$field['import'] : $field['handle'];
Expand All @@ -293,6 +292,7 @@ private function addEnsuredFieldToContents($contents, $ensured)
$importKey = 'import:'.$importedField['partial'];
$field = $allFields->get($importKey);
$tab = $field['tab'];
$targetSectionIndex = $field['section'];
$fields = collect($tabs[$tab]['sections'][$targetSectionIndex]['fields'])->keyBy(function ($field) {
return (isset($field['import'])) ? 'import:'.($field['prefix'] ?? null).$field['import'] : $field['handle'];
});
Expand Down
59 changes: 58 additions & 1 deletion tests/Fields/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -783,12 +783,12 @@ public function it_ensures_a_field_exists()
[
'fields' => [
['handle' => 'existing_in_section_one', 'field' => ['type' => 'text']],
['handle' => 'new', 'field' => ['type' => 'textarea']],
],
],
[
'fields' => [
['handle' => 'existing_in_section_two', 'field' => ['type' => 'text']],
['handle' => 'new', 'field' => ['type' => 'textarea']],
],
],
],
Expand Down Expand Up @@ -1203,6 +1203,63 @@ public function it_merges_undefined_config_overrides_when_ensuring_a_field_that_
$this->assertEquals(['type' => 'text', 'foo' => 'bar'], $blueprint->fields()->get('one')->config());
}

#[Test]
public function it_merges_config_overrides_when_ensuring_a_field_inside_an_imported_fieldset_in_a_later_section()
{
FieldsetRepository::shouldReceive('find')->with('the_partial')->andReturn(
(new Fieldset)->setContents(['fields' => [
[
'handle' => 'one',
'field' => ['type' => 'text'],
],
]])
);

$blueprint = (new Blueprint)->setContents(['tabs' => [
'tab_one' => [
'sections' => [
[
'fields' => [
['handle' => 'existing', 'field' => ['type' => 'text']],
],
],
[
'fields' => [
['import' => 'the_partial'],
],
],
],
],
]]);

$return = $blueprint->ensureField('one', ['type' => 'textarea', 'foo' => 'bar']);

$this->assertEquals($blueprint, $return);
$this->assertTrue($blueprint->hasField('one'));
$this->assertEquals(['tabs' => [
'tab_one' => [
'sections' => [
[
'fields' => [
['handle' => 'existing', 'field' => ['type' => 'text']],
],
],
[
'fields' => [
[
'import' => 'the_partial',
'config' => [
'one' => ['foo' => 'bar'],
],
],
],
],
],
],
]], $blueprint->contents());
$this->assertEquals(['type' => 'text', 'foo' => 'bar'], $blueprint->fields()->get('one')->config());
}

#[Test]
public function it_ensures_a_field_exists_if_it_doesnt_and_prepends_it()
{
Expand Down
Loading