Skip to content

Commit

Permalink
Add config options to control panel (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni authored Oct 31, 2024
1 parent 73d80ea commit f8a6294
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 6 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"laravel/prompts": "^0.1.13",
"livewire/livewire": "^3.2",
"spatie/invade": "^2.0",
"statamic/cms": "^5.22"
"statamic/cms": "^5.36"
},
"require-dev": {
"orchestra/testbench": "^8.19",
Expand Down
14 changes: 13 additions & 1 deletion src/Livewire/Concerns/WithRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@

namespace Aerni\LivewireForms\Livewire\Concerns;

use Illuminate\Support\Str;
use Livewire\Attributes\Locked;
use Statamic\Facades\Entry;

trait WithRedirect
{
#[Locked]
public string $redirect = '';
public string $redirect;

public function mountWithRedirect(): void
{
$this->redirect ??= $this->form->redirect ?? '';

/* This is a workaround because the form config fields are not augmented. So we have to manually get the entry. */
if (Str::startsWith($this->redirect, 'entry::')) {
$this->redirect = Entry::find(Str::after($this->redirect, 'entry::'))?->permalink ?? '';
}
}
}
5 changes: 5 additions & 0 deletions src/Livewire/Concerns/WithTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ protected function theme(): string
return $this->theme;
}

/* Use the theme configured in the form config if it exists. */
if (ViewManager::themeExists($this->form->theme)) {
return $this->form->theme;
}

/* Autoload the theme by form handle if it exists. */
if (ViewManager::themeExists($this->handle)) {
return $this->handle;
Expand Down
2 changes: 1 addition & 1 deletion src/Livewire/Concerns/WithType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function mountWithType(): void

protected function type(): string
{
return match ($this->type ?? null) {
return match ($this->type ?? $this->form->type) {
'wizard' => 'wizard',
default => 'basic',
};
Expand Down
5 changes: 5 additions & 0 deletions src/Livewire/Concerns/WithView.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ protected function view(): string
return $this->view;
}

/* Use the view configured in the form config if it exists. */
if (ViewManager::viewExists($this->form->view)) {
return $this->form->view;
}

/* Autoload the view by form handle if it exists. */
if (ViewManager::viewExists($this->handle)) {
return $this->handle;
Expand Down
2 changes: 2 additions & 0 deletions src/Livewire/DynamicForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Aerni\LivewireForms\Livewire;

use Aerni\LivewireForms\Livewire\Concerns\WithComponent;
use Aerni\LivewireForms\Livewire\Concerns\WithForm;
use Aerni\LivewireForms\Livewire\Concerns\WithHandle;
use Aerni\LivewireForms\Livewire\Concerns\WithRedirect;
use Aerni\LivewireForms\Livewire\Concerns\WithTheme;
Expand All @@ -14,6 +15,7 @@
class DynamicForm extends Component
{
use WithComponent;
use WithForm;
use WithHandle;
use WithTheme;
use WithType;
Expand Down
49 changes: 48 additions & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
use Aerni\LivewireForms\Livewire\Synthesizers\FieldSynth;
use Aerni\LivewireForms\Livewire\Synthesizers\RuleSynth;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use Livewire\Livewire;
use Statamic\Facades\Form;
use Statamic\Providers\AddonServiceProvider;

class ServiceProvider extends AddonServiceProvider
Expand All @@ -35,7 +38,8 @@ public function bootAddon()
->bootBladeDirectives()
->bootValidators()
->bootLivewire()
->bootSelectableFieldtypes();
->bootSelectableFieldtypes()
->bootFormConfigFields();
}

protected function bootBladeDirectives(): self
Expand Down Expand Up @@ -73,4 +77,47 @@ protected function bootSelectableFieldtypes(): self

return $this;
}

protected function bootFormConfigFields(): self
{
Form::appendConfigFields('*', __('Livewire Forms'), [
'type' => [
'type' => 'button_group',
'display' => __('Type'),
'instructions' => __('Choose the desired type for this form.'),
'options' => [
'basic' => __('Basic'),
'wizard' => __('Wizard'),
],
'default' => 'basic',
],
'view' => [
'type' => 'select',
'display' => __('View'),
'instructions' => __('Choose the view for this form.'),
'options' => collect(File::files(resource_path('views/'.config('livewire-forms.view_path'))))
->map(fn ($file) => Str::before($file->getBasename(), '.'))
->mapWithKeys(fn ($view) => [$view => str($view)->replace(['_', '-'], ' ')->title()->toString()]),
'clearable' => true,
'width' => 50,
],
'theme' => [
'type' => 'select',
'display' => __('Theme'),
'instructions' => __('Choose the theme for this form.'),
'options' => collect(File::directories(resource_path('views/'.config('livewire-forms.view_path'))))
->map(fn ($directory) => basename($directory))
->mapWithKeys(fn ($theme) => [$theme => str($theme)->replace(['_', '-'], ' ')->title()->toString()]),
'clearable' => true,
'width' => 50,
],
'redirect' => [
'type' => 'link',
'display' => __('Redirect URL'),
'instructions' => __('The users will be redirected to this URL after the form was submitted.'),
],
]);

return $this;
}
}
12 changes: 10 additions & 2 deletions src/ViewManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,21 @@ public function themeViewExists(string $theme, string $view): bool
return view()->exists($this->themeViewPath($theme, $view));
}

public function viewExists(string $view): bool
public function viewExists(?string $view): bool
{
if (empty($view)) {
return false;
}

return view()->exists($this->viewPath($view));
}

public function themeExists(string $theme): bool
public function themeExists(?string $theme): bool
{
if (empty($theme)) {
return false;
}

return is_dir(resource_path("views/{$this->viewPath($theme)}"));
}

Expand Down

0 comments on commit f8a6294

Please sign in to comment.