diff --git a/config/livewire-forms.php b/config/livewire-forms.php index e12daff3..333f2d39 100644 --- a/config/livewire-forms.php +++ b/config/livewire-forms.php @@ -15,6 +15,7 @@ Aerni\LivewireForms\Fieldtypes\Captcha::class => Aerni\LivewireForms\Fields\Captcha::class, Statamic\Fieldtypes\Assets\Assets::class => Aerni\LivewireForms\Fields\Assets::class, Statamic\Fieldtypes\Checkboxes::class => Aerni\LivewireForms\Fields\Checkboxes::class, + Statamic\Fieldtypes\Dictionary::class => Aerni\LivewireForms\Fields\Dictionary::class, Statamic\Fieldtypes\Hidden::class => Aerni\LivewireForms\Fields\Hidden::class, Statamic\Fieldtypes\Integer::class => Aerni\LivewireForms\Fields\Integer::class, Statamic\Fieldtypes\Radio::class => Aerni\LivewireForms\Fields\Radio::class, diff --git a/resources/views/default/fields/dictionary.blade.php b/resources/views/default/fields/dictionary.blade.php new file mode 100644 index 00000000..ebd95962 --- /dev/null +++ b/resources/views/default/fields/dictionary.blade.php @@ -0,0 +1 @@ +@formView('fields.select') diff --git a/src/Fields/Dictionary.php b/src/Fields/Dictionary.php new file mode 100644 index 00000000..0634e9ca --- /dev/null +++ b/src/Fields/Dictionary.php @@ -0,0 +1,24 @@ +max_items) || $this->max_items > 1; + } + + protected function optionsProperty(?array $options = null): array + { + $config = is_array($config = $this->field->get('dictionary')) ? $config : ['type' => $config]; + + $dictionary = Dictionaries::find($config['type'], $config); + + return $dictionary?->options() ?? []; + } +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index c6826fcc..76c180fa 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -3,6 +3,7 @@ namespace Aerni\LivewireForms; use Aerni\LivewireForms\Facades\Captcha; +use Aerni\LivewireForms\Facades\ViewManager; use Aerni\LivewireForms\Livewire\BaseForm; use Aerni\LivewireForms\Livewire\DynamicForm; use Aerni\LivewireForms\Livewire\Synthesizers\FieldSynth; @@ -95,7 +96,7 @@ protected function bootFormConfigFields(): self 'type' => 'select', 'display' => __('View'), 'instructions' => __('Choose the view for this form.'), - 'options' => $this->viewOptions(), + 'options' => ViewManager::views(), 'clearable' => true, 'width' => 50, ], @@ -103,7 +104,7 @@ protected function bootFormConfigFields(): self 'type' => 'select', 'display' => __('Theme'), 'instructions' => __('Choose the theme for this form.'), - 'options' => $this->themeOptions(), + 'options' => ViewManager::themes(), 'clearable' => true, 'width' => 50, ], @@ -116,32 +117,4 @@ protected function bootFormConfigFields(): self return $this; } - - protected function viewOptions(): ?array - { - $path = resource_path('views/'.config('livewire-forms.view_path')); - - if (! File::isDirectory($path)) { - return null; - } - - return collect(File::files($path)) - ->map(fn ($file) => Str::before($file->getBasename(), '.')) - ->mapWithKeys(fn ($view) => [$view => str($view)->replace(['_', '-'], ' ')->title()->toString()]) - ->all(); - } - - protected function themeOptions(): ?array - { - $path = resource_path('views/'.config('livewire-forms.view_path')); - - if (! File::isDirectory($path)) { - return null; - } - - return collect(File::directories($path)) - ->map(fn ($directory) => basename($directory)) - ->mapWithKeys(fn ($theme) => [$theme => str($theme)->replace(['_', '-'], ' ')->title()->toString()]) - ->all(); - } } diff --git a/src/UpdateScripts/AddDictionaryFieldView.php b/src/UpdateScripts/AddDictionaryFieldView.php new file mode 100755 index 00000000..a322b0ef --- /dev/null +++ b/src/UpdateScripts/AddDictionaryFieldView.php @@ -0,0 +1,26 @@ +isUpdatingTo('9.6.0'); + } + + public function update() + { + collect(ViewManager::themes()) + ->each(function ($name, $handle) { + $view = ViewManager::viewPath("{$handle}/fields/dictionary.blade.php"); + File::copy(__DIR__.'/../../resources/views/default/fields/dictionary.blade.php', resource_path("views/{$view}")); + }); + + $this->console()->info('Added dictionary field view.'); + } +} diff --git a/src/ViewManager.php b/src/ViewManager.php index 8d136850..254d088c 100644 --- a/src/ViewManager.php +++ b/src/ViewManager.php @@ -2,6 +2,9 @@ namespace Aerni\LivewireForms; +use Illuminate\Support\Str; +use Illuminate\Support\Facades\File; + class ViewManager { public function viewPath(string $view): string @@ -49,4 +52,32 @@ public function defaultTheme(): string { return config('livewire-forms.theme', 'default'); } + + public function views(): ?array + { + $path = resource_path('views/'.config('livewire-forms.view_path')); + + if (! File::isDirectory($path)) { + return null; + } + + return collect(File::files($path)) + ->map(fn ($file) => Str::before($file->getBasename(), '.')) + ->mapWithKeys(fn ($view) => [$view => str($view)->replace(['_', '-'], ' ')->title()->toString()]) + ->all(); + } + + public function themes(): ?array + { + $path = resource_path('views/'.config('livewire-forms.view_path')); + + if (! File::isDirectory($path)) { + return null; + } + + return collect(File::directories($path)) + ->map(fn ($directory) => basename($directory)) + ->mapWithKeys(fn ($theme) => [$theme => str($theme)->replace(['_', '-'], ' ')->title()->toString()]) + ->all(); + } }