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
92 changes: 68 additions & 24 deletions src/Fields/Fieldtype.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ abstract class Fieldtype implements Arrayable
protected $defaultValue;
protected $configFields = [];
protected static $extraConfigFields = [];
protected static $extraConfigFieldPositions = [];
private static ?WeakMap $configDefaults = null;
protected $icon;

Expand Down Expand Up @@ -215,46 +216,43 @@ private function configSections()
$fields = $this->configFieldItems();
$extras = collect($this->extraConfigFieldItems());
$extraSections = $extras->filter(fn ($_, $key) => is_int($key));
$extraFields = $extras
$positions = $this->extraConfigFieldPositions();
[$positionedFields, $extraFields] = $extras
->reject(fn ($_, $key) => is_int($key))
->map(fn ($field, $handle) => compact('handle', 'field'))
->values()->all();
->partition(fn ($extra) => isset($positions[$extra['handle']]))
->map(fn ($group) => $group->values()->all())
->all();

if (empty($fields) && $extras->isEmpty()) {
return [];
}

if (! $this->configFieldsUseSections()) {
$sections = [];

if (! empty($fields) || ! empty($extraFields)) {
$sections[] = [
'fields' => collect($fields)
->map(fn ($field, $handle) => compact('handle', 'field'))
->merge($extraFields)
->values()->all(),
];
}
$sections = empty($fields) ? [] : [[
'fields' => collect($fields)
->map(fn ($field, $handle) => compact('handle', 'field'))
->values()->all(),
]];
} else {
$sections = collect($fields)->map(function ($section) {
$section['fields'] = collect($section['fields'])
->map(fn ($field, $handle) => compact('handle', 'field'))
->values()->all();

return $section;
});
})->all();
}

if (! empty($extraFields)) {
if ($sections->containsOneItem()) {
$section = $sections[0];
$section['fields'] = array_merge($section['fields'], $extraFields);
$sections[0] = $section;
} else {
$sections[] = ['fields' => $extraFields];
}
}
[$sections, $unplaced] = $this->placeExtraConfigFields($sections, $positionedFields, $positions);

$sections = $sections->all();
// Unplaced extras go at the end of the only section, or in a new one.
if (! empty($extraFields = array_merge($extraFields, $unplaced))) {
if (count($sections) === 1) {
$sections[0]['fields'] = array_merge($sections[0]['fields'], $extraFields);
} else {
$sections[] = ['fields' => $extraFields];
}
}

return array_merge($sections, $extraSections->map(function ($section) {
Expand Down Expand Up @@ -309,6 +307,40 @@ protected function configFieldItems(): array
return $this->configFields;
}

/**
* Puts each positioned field right after the field it names. Returns the ones that can't be placed.
*/
private function placeExtraConfigFields(array $sections, array $pending, array $positions): array
{
do {
$progress = false;

// Reversed, so fields after the same anchor keep their appended order.
foreach (array_reverse($pending, true) as $key => $extra) {
foreach ($sections as $index => $section) {
$at = array_search($positions[$extra['handle']], array_column($section['fields'], 'handle'), true);

if ($at !== false) {
array_splice($sections[$index]['fields'], $at + 1, 0, [$extra]);
unset($pending[$key]);
$progress = $pending !== [];
break;
}
}
}
} while ($progress);

return [$sections, array_values($pending)];
}

protected function extraConfigFieldPositions(): array
{
return array_merge(
self::$extraConfigFieldPositions[static::class] ?? [],
Fieldtype::$extraConfigFieldPositions[Fieldtype::class] ?? [],
);
}

protected function extraConfigFieldItems(): array
{
return array_merge(
Expand All @@ -322,11 +354,23 @@ public static function appendConfigFields(array $config): void
$existingConfig = self::$extraConfigFields[static::class] ?? [];

self::$extraConfigFields[static::class] = array_merge($existingConfig, $config);

// Appending again resets the position.
foreach (array_keys($config) as $field) {
unset(self::$extraConfigFieldPositions[static::class][$field]);
}
}

public static function appendConfigField(string $field, array $config): void
/**
* @param string|null $after Handle of the config field to place this one after.
*/
public static function appendConfigField(string $field, array $config, ?string $after = null): void
{
self::appendConfigFields([$field => $config]);

if ($after !== null) {
self::$extraConfigFieldPositions[static::class][$field] = $after;
}
}

public function icon()
Expand Down
117 changes: 117 additions & 0 deletions tests/Fields/FieldtypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,123 @@ public function appending_a_grid_config_field_is_not_treated_as_a_section()
$this->assertNull($fieldtype->configFields()->get('label'));
}

#[Test]
public function it_can_place_an_appended_config_field_after_an_existing_one()
{
$fieldtype = new class extends Fieldtype
{
protected function configFieldItems(): array
{
return [
['display' => 'Data', 'fields' => ['default' => ['type' => 'text'], 'format' => ['type' => 'text']]],
['display' => 'Advanced', 'fields' => ['antlers' => ['type' => 'toggle']]],
];
}
};

$fieldtype::appendConfigField('conditional', ['type' => 'text'], after: 'default');

$sections = $fieldtype->configBlueprint()->contents()['tabs']['main']['sections'];

$this->assertCount(2, $sections);
$this->assertEquals(['default', 'conditional', 'format'], array_column($sections[0]['fields'], 'handle'));
$this->assertEquals(['antlers'], array_column($sections[1]['fields'], 'handle'));
$this->assertEquals('text', $fieldtype->configFields()->get('conditional')->type());
}

#[Test]
public function it_can_place_an_appended_config_field_in_a_linear_fieldtype()
{
$fieldtype = new class extends Fieldtype
{
protected $configFields = [
'foo' => ['type' => 'textarea'],
'bar' => ['type' => 'text'],
];
};

$fieldtype::appendConfigField('baz', ['type' => 'text'], after: 'foo');

$fields = $fieldtype->configBlueprint()->contents()['tabs']['main']['sections'][0]['fields'];

$this->assertEquals(['foo', 'baz', 'bar'], array_column($fields, 'handle'));
}

#[Test]
public function it_appends_normally_when_the_field_to_place_after_is_missing()
{
$fieldtype = new class extends Fieldtype
{
protected $configFields = [
'foo' => ['type' => 'textarea'],
];
};

$fieldtype::appendConfigField('baz', ['type' => 'text'], after: 'missing');

$sections = $fieldtype->configBlueprint()->contents()['tabs']['main']['sections'];

$this->assertCount(1, $sections);
$this->assertEquals(['foo', 'baz'], array_column($sections[0]['fields'], 'handle'));
}

#[Test]
public function it_keeps_the_order_of_fields_placed_after_the_same_field()
{
$fieldtype = new class extends Fieldtype
{
protected $configFields = [
'foo' => ['type' => 'textarea'],
'bar' => ['type' => 'text'],
];
};

$fieldtype::appendConfigField('one', ['type' => 'text'], after: 'foo');
$fieldtype::appendConfigField('two', ['type' => 'text'], after: 'foo');

$fields = $fieldtype->configBlueprint()->contents()['tabs']['main']['sections'][0]['fields'];

$this->assertEquals(['foo', 'one', 'two', 'bar'], array_column($fields, 'handle'));
}

#[Test]
public function it_can_place_an_appended_config_field_after_another_appended_one()
{
$fieldtype = new class extends Fieldtype
{
protected $configFields = [
'foo' => ['type' => 'textarea'],
'bar' => ['type' => 'text'],
];
};

$fieldtype::appendConfigField('second', ['type' => 'text'], after: 'first');
$fieldtype::appendConfigField('first', ['type' => 'text'], after: 'foo');

$fields = $fieldtype->configBlueprint()->contents()['tabs']['main']['sections'][0]['fields'];

$this->assertEquals(['foo', 'first', 'second', 'bar'], array_column($fields, 'handle'));
}

#[Test]
public function appending_a_config_field_again_replaces_where_it_was_placed()
{
$fieldtype = new class extends Fieldtype
{
protected $configFields = [
'foo' => ['type' => 'textarea'],
'bar' => ['type' => 'text'],
];
};

$fieldtype::appendConfigField('baz', ['type' => 'text'], after: 'foo');
$fieldtype::appendConfigField('baz', ['type' => 'text']);

$fields = $fieldtype->configBlueprint()->contents()['tabs']['main']['sections'][0]['fields'];

$this->assertEquals(['foo', 'bar', 'baz'], array_column($fields, 'handle'));
}

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