Skip to content

Commit 70d6ee3

Browse files
committed
handle $selectableInForms a different way
1 parent a10880c commit 70d6ee3

7 files changed

Lines changed: 70 additions & 1 deletion

File tree

resources/js/pages/forms/Connect.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ defineOptions({ layout: [Layout, PanelLayout, FormsLayout] });
77
88
const props = defineProps({
99
form: Object,
10+
fieldtypes: Array,
1011
});
1112
</script>
1213

1314
<template>
1415
<div class="py-4">
15-
<p>TODO: Connect</p>
16+
<!-- TOOD: Remove from this component when wiring up the form builder. -->
17+
<ul>
18+
<li v-for="fieldtype in fieldtypes" :key="fieldtype.handle">
19+
{{ fieldtype.title }}
20+
</li>
21+
</ul>
1622
</div>
1723
</template>

src/Forms/Fields/Email.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
class Email extends FormFieldtype
88
{
9+
protected static $fieldtype = 'text';
910
protected $icon = 'mail-sign-at';
1011
protected $categories = ['Contact Info'];
1112

src/Forms/Fields/Fallback.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,37 @@
22

33
namespace Statamic\Forms\Fields;
44

5+
use Statamic\Fields\Fieldtype;
6+
57
class Fallback extends FormFieldtype
68
{
9+
protected ?Fieldtype $wrappedFieldtype = null;
10+
11+
public function wrapping(Fieldtype $fieldtype): static
12+
{
13+
$this->wrappedFieldtype = $fieldtype;
14+
15+
return $this;
16+
}
17+
718
public function toFieldArray(): array
819
{
920
return $this->config();
1021
}
22+
23+
public function toArray(): array
24+
{
25+
if (! $this->wrappedFieldtype) {
26+
return parent::toArray();
27+
}
28+
29+
return [
30+
'handle' => $this->wrappedFieldtype->handle(),
31+
'title' => $this->wrappedFieldtype->title(),
32+
'categories' => $this->wrappedFieldtype->categories(),
33+
'keywords' => $this->wrappedFieldtype->keywords(),
34+
'icon' => $this->wrappedFieldtype->icon(),
35+
'config' => $this->wrappedFieldtype->configFields()->toPublishArray(),
36+
];
37+
}
1138
}

src/Forms/Fields/FormFieldtype.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Statamic\Forms\Fields;
44

55
use Facades\Statamic\Forms\Fields\FormFieldtypeRepository;
6+
use Illuminate\Contracts\Support\Arrayable;
67
use Statamic\Extend\HasHandle;
78
use Statamic\Extend\RegistersItself;
89
use Statamic\Facades\Blink;
@@ -21,6 +22,7 @@ abstract class FormFieldtype implements Arrayable
2122

2223
protected static $title;
2324
protected static $binding = 'form-fields';
25+
protected static $fieldtype;
2426

2527
protected $field;
2628
protected $selectable = true;
@@ -61,6 +63,11 @@ public static function handle(): string
6163
return Str::removeRight(static::traitHandle(), '_form_field');
6264
}
6365

66+
public static function fieldtype(): ?string
67+
{
68+
return static::$fieldtype;
69+
}
70+
6471
public function categories(): array
6572
{
6673
return $this->categories;

src/Forms/Fields/LongAnswer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class LongAnswer extends FormFieldtype
88
{
9+
protected static $fieldtype = 'textarea';
10+
911
public function configFieldItems(): array
1012
{
1113
return [

src/Forms/Fields/ShortAnswer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class ShortAnswer extends FormFieldtype
88
{
9+
protected static $fieldtype = 'text';
10+
911
public function configFieldItems(): array
1012
{
1113
return [

src/Http/Controllers/CP/Forms/FormConnectController.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,39 @@
22

33
namespace Statamic\Http\Controllers\CP\Forms;
44

5+
use Facades\Statamic\Fields\FieldtypeRepository;
56
use Inertia\Inertia;
7+
use Statamic\Forms\Fields\Fallback;
8+
use Statamic\Forms\Fields\FormFieldtype;
69
use Statamic\Http\Controllers\CP\CpController;
710

811
class FormConnectController extends CpController
912
{
1013
public function __invoke($form)
1114
{
15+
// TOOD: Remove from this controller when wiring up the form builder.
16+
$formFieldtypes = app('statamic.form-fieldtypes')
17+
->unique()
18+
->map(fn ($class) => app($class))
19+
->filter->isSelectable()
20+
->values();
21+
22+
$fieldtypesPortedToFormFieldtypes = $formFieldtypes
23+
->map(fn (FormFieldtype $fieldtype) => $fieldtype::fieldtype())
24+
->filter()
25+
->unique()
26+
->values();
27+
28+
$legacySelectableFieldtypes = FieldtypeRepository::classes()
29+
->map(fn ($class) => app($class))
30+
->filter->selectableInForms()
31+
->reject(fn ($fieldtype) => $fieldtypesPortedToFormFieldtypes->contains($fieldtype->handle()))
32+
->map(fn ($fieldtype) => (new Fallback)->wrapping($fieldtype))
33+
->values();
34+
1235
return Inertia::render('forms/Connect', [
1336
'form' => $form,
37+
'fieldtypes' => $formFieldtypes->merge($legacySelectableFieldtypes)->sortBy->handle()->values(),
1438
]);
1539
}
1640
}

0 commit comments

Comments
 (0)