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: 4 additions & 0 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ protected function addNullableRule($rules)

public function isRequired()
{
if (! isset($this->rules()[$this->handle])) {
return false;
}

return collect($this->rules()[$this->handle])->contains('required');
}

Expand Down
21 changes: 20 additions & 1 deletion src/Fields/Fieldtype.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Facades\Statamic\Fields\FieldtypeRepository;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
use Statamic\Extend\HasHandle;
use Statamic\Extend\RegistersItself;
use Statamic\Facades\Blink;
Expand Down Expand Up @@ -215,6 +216,11 @@ private function configSections()
return [];
}

if ($extraSections = $this->extraConfigFieldsUseSections($extras)) {
$fields = collect($fields)->merge($extraSections);
$extras = collect($extras)->diffKeys($extraSections);
}

$extras = collect($extras)
->map(fn ($field, $handle) => compact('handle', 'field'))
->values()->all();
Expand Down Expand Up @@ -260,20 +266,33 @@ private function configFieldsUseSections()
return array_keys($fields)[0] === 0;
}

private function extraConfigFieldsUseSections($extras)
{
return collect($extras)->filter(fn ($field) => Arr::has($field, 'fields'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this is the right way to check, as replicators and bards both have fields so this check would mark them both as sections.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ryanmitchell Are you referring to replicators and bard fields that you want to add with appendConfigFields method? Something like this:

image

This check does validate fields key on the first level of the array generated by

protected function extraConfigFieldItems(): array
but it's not recursive for other fields keys, so I haven't seen the behavior that you mentioned. This PR does not affect other existing fields beside the ones that you define calling appendConfigField or appendConfigFields method.

I did a lot of tests in my dev site and all fields seems to be working fine, if you see something weird is happening please let me know. Thanks for your review!

This is how it looks for my custom test calling Text::appendConfigFields():

image

}

public function configFields(): Fields
{
if ($cached = Blink::get($blink = 'config-fields-'.$this->handle())) {
return $cached;
}

$fields = collect($this->configFieldItems());
$extraFields = collect($this->extraConfigFieldItems());

if ($this->configFieldsUseSections()) {
$fields = $fields->flatMap(fn ($section) => $section['fields']);
}

if ($extraSections = $this->extraConfigFieldsUseSections($extraFields)) {
$mergeFields = $extraSections->flatMap(fn ($section) => $section['fields'] ?? null);

$fields = collect($fields)->merge($mergeFields);
$extraFields = collect($extraFields)->diffKeys($extraSections);
}

$fields = $fields
->merge($this->extraConfigFieldItems())
->merge($extraFields)
->map(function ($field, $handle) {
return compact('handle', 'field');
});
Expand Down
69 changes: 69 additions & 0 deletions tests/Fields/FieldtypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,67 @@ public function it_can_append_multiple_config_fields()
$this->assertEquals('textarea', $fields->get('description')->type());
}

#[Test]
public function it_can_append_new_section_config_fields()
{
TestAppendConfigSectionFields::appendConfigFields([
[
'display' => __('Extra section'),
'fields' => [
'more_options' => [
'display' => __('Options'),
'instructions' => __('Instructions for this field'),
'type' => 'array',
],
'extra_html_class' => [
'display' => __('Append HTML Classes options'),
'instructions' => __('Instructions for this field'),
'type' => 'textarea',
],
],
],
]);

$fields = (new TestAppendConfigSectionFields())->configFields();

$this->assertCount(4, $fields->all());
$this->assertEquals('array', $fields->get('more_options')->type());
$this->assertEquals('textarea', $fields->get('extra_html_class')->type());
}

#[Test]
public function it_can_append_new_sections_config_fields()
{
TestAppendConfigSectionFields::appendConfigFields([
[
'display' => __('Extra section'),
'fields' => [
'more_options' => [
'display' => __('Options'),
'instructions' => __('Instructions for this field'),
'type' => 'array',
],
],
],
[
'display' => __('New Extra section'),
'fields' => [
'extra_html_class' => [
'display' => __('Append HTML Classes options'),
'instructions' => __('Instructions for this field'),
'type' => 'textarea',
],
],
],
]);

$fields = (new TestAppendConfigSectionFields())->configFields();

$this->assertCount(4, $fields->all());
$this->assertEquals('array', $fields->get('more_options')->type());
$this->assertEquals('textarea', $fields->get('extra_html_class')->type());
}

#[Test]
public function it_wont_override_previously_appended_config_fields()
{
Expand Down Expand Up @@ -645,6 +706,14 @@ class TestAppendConfigFields extends Fieldtype
];
}

class TestAppendConfigSectionFields extends Fieldtype
{
protected $configFields = [
'foo' => ['type' => 'textarea'],
'max_items' => ['type' => 'integer'],
];
}

class TestFieldtypeWithConfigFieldsProperty extends Fieldtype
{
public function __construct($property)
Expand Down