Skip to content

Commit f8a6294

Browse files
authored
Add config options to control panel (#73)
1 parent 73d80ea commit f8a6294

File tree

8 files changed

+85
-6
lines changed

8 files changed

+85
-6
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"laravel/prompts": "^0.1.13",
2424
"livewire/livewire": "^3.2",
2525
"spatie/invade": "^2.0",
26-
"statamic/cms": "^5.22"
26+
"statamic/cms": "^5.36"
2727
},
2828
"require-dev": {
2929
"orchestra/testbench": "^8.19",

src/Livewire/Concerns/WithRedirect.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,22 @@
22

33
namespace Aerni\LivewireForms\Livewire\Concerns;
44

5+
use Illuminate\Support\Str;
56
use Livewire\Attributes\Locked;
7+
use Statamic\Facades\Entry;
68

79
trait WithRedirect
810
{
911
#[Locked]
10-
public string $redirect = '';
12+
public string $redirect;
13+
14+
public function mountWithRedirect(): void
15+
{
16+
$this->redirect ??= $this->form->redirect ?? '';
17+
18+
/* This is a workaround because the form config fields are not augmented. So we have to manually get the entry. */
19+
if (Str::startsWith($this->redirect, 'entry::')) {
20+
$this->redirect = Entry::find(Str::after($this->redirect, 'entry::'))?->permalink ?? '';
21+
}
22+
}
1123
}

src/Livewire/Concerns/WithTheme.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ protected function theme(): string
2222
return $this->theme;
2323
}
2424

25+
/* Use the theme configured in the form config if it exists. */
26+
if (ViewManager::themeExists($this->form->theme)) {
27+
return $this->form->theme;
28+
}
29+
2530
/* Autoload the theme by form handle if it exists. */
2631
if (ViewManager::themeExists($this->handle)) {
2732
return $this->handle;

src/Livewire/Concerns/WithType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function mountWithType(): void
1616

1717
protected function type(): string
1818
{
19-
return match ($this->type ?? null) {
19+
return match ($this->type ?? $this->form->type) {
2020
'wizard' => 'wizard',
2121
default => 'basic',
2222
};

src/Livewire/Concerns/WithView.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ protected function view(): string
2222
return $this->view;
2323
}
2424

25+
/* Use the view configured in the form config if it exists. */
26+
if (ViewManager::viewExists($this->form->view)) {
27+
return $this->form->view;
28+
}
29+
2530
/* Autoload the view by form handle if it exists. */
2631
if (ViewManager::viewExists($this->handle)) {
2732
return $this->handle;

src/Livewire/DynamicForm.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Aerni\LivewireForms\Livewire;
44

55
use Aerni\LivewireForms\Livewire\Concerns\WithComponent;
6+
use Aerni\LivewireForms\Livewire\Concerns\WithForm;
67
use Aerni\LivewireForms\Livewire\Concerns\WithHandle;
78
use Aerni\LivewireForms\Livewire\Concerns\WithRedirect;
89
use Aerni\LivewireForms\Livewire\Concerns\WithTheme;
@@ -14,6 +15,7 @@
1415
class DynamicForm extends Component
1516
{
1617
use WithComponent;
18+
use WithForm;
1719
use WithHandle;
1820
use WithTheme;
1921
use WithType;

src/ServiceProvider.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
use Aerni\LivewireForms\Livewire\Synthesizers\FieldSynth;
99
use Aerni\LivewireForms\Livewire\Synthesizers\RuleSynth;
1010
use Illuminate\Support\Facades\Blade;
11+
use Illuminate\Support\Facades\File;
1112
use Illuminate\Support\Facades\Validator;
13+
use Illuminate\Support\Str;
1214
use Livewire\Livewire;
15+
use Statamic\Facades\Form;
1316
use Statamic\Providers\AddonServiceProvider;
1417

1518
class ServiceProvider extends AddonServiceProvider
@@ -35,7 +38,8 @@ public function bootAddon()
3538
->bootBladeDirectives()
3639
->bootValidators()
3740
->bootLivewire()
38-
->bootSelectableFieldtypes();
41+
->bootSelectableFieldtypes()
42+
->bootFormConfigFields();
3943
}
4044

4145
protected function bootBladeDirectives(): self
@@ -73,4 +77,47 @@ protected function bootSelectableFieldtypes(): self
7377

7478
return $this;
7579
}
80+
81+
protected function bootFormConfigFields(): self
82+
{
83+
Form::appendConfigFields('*', __('Livewire Forms'), [
84+
'type' => [
85+
'type' => 'button_group',
86+
'display' => __('Type'),
87+
'instructions' => __('Choose the desired type for this form.'),
88+
'options' => [
89+
'basic' => __('Basic'),
90+
'wizard' => __('Wizard'),
91+
],
92+
'default' => 'basic',
93+
],
94+
'view' => [
95+
'type' => 'select',
96+
'display' => __('View'),
97+
'instructions' => __('Choose the view for this form.'),
98+
'options' => collect(File::files(resource_path('views/'.config('livewire-forms.view_path'))))
99+
->map(fn ($file) => Str::before($file->getBasename(), '.'))
100+
->mapWithKeys(fn ($view) => [$view => str($view)->replace(['_', '-'], ' ')->title()->toString()]),
101+
'clearable' => true,
102+
'width' => 50,
103+
],
104+
'theme' => [
105+
'type' => 'select',
106+
'display' => __('Theme'),
107+
'instructions' => __('Choose the theme for this form.'),
108+
'options' => collect(File::directories(resource_path('views/'.config('livewire-forms.view_path'))))
109+
->map(fn ($directory) => basename($directory))
110+
->mapWithKeys(fn ($theme) => [$theme => str($theme)->replace(['_', '-'], ' ')->title()->toString()]),
111+
'clearable' => true,
112+
'width' => 50,
113+
],
114+
'redirect' => [
115+
'type' => 'link',
116+
'display' => __('Redirect URL'),
117+
'instructions' => __('The users will be redirected to this URL after the form was submitted.'),
118+
],
119+
]);
120+
121+
return $this;
122+
}
76123
}

src/ViewManager.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,21 @@ public function themeViewExists(string $theme, string $view): bool
2222
return view()->exists($this->themeViewPath($theme, $view));
2323
}
2424

25-
public function viewExists(string $view): bool
25+
public function viewExists(?string $view): bool
2626
{
27+
if (empty($view)) {
28+
return false;
29+
}
30+
2731
return view()->exists($this->viewPath($view));
2832
}
2933

30-
public function themeExists(string $theme): bool
34+
public function themeExists(?string $theme): bool
3135
{
36+
if (empty($theme)) {
37+
return false;
38+
}
39+
3240
return is_dir(resource_path("views/{$this->viewPath($theme)}"));
3341
}
3442

0 commit comments

Comments
 (0)