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
25 changes: 24 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 @@ -210,6 +211,14 @@ private function configSections()
return [];
}

if ($this->extraConfigFieldsUseSections()) {
$extraSections = collect($extras)->filter(fn ($field) => Arr::has($field, 'fields'));

$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 @@ -255,20 +264,34 @@ private function configFieldsUseSections()
return array_keys($fields)[0] === 0;
}

private function extraConfigFieldsUseSections()
{
if (empty($fields = $this->extraConfigFieldItems())) {
return false;
}

return array_keys($fields)[0] === 0;
}

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 ($this->extraConfigFieldsUseSections()) {
$extraFields = $extraFields->flatMap(fn ($section) => $section['fields']);
}

$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