From 7452ac93fec2cec0ce50734d8b89989795d2cb88 Mon Sep 17 00:00:00 2001 From: Guillermo Azurdia Date: Wed, 19 Feb 2025 23:50:56 -0500 Subject: [PATCH 1/8] Add Dictionary Fieldtype support Add support for Dictionary Fieldtype on the front-end forms. --- config/livewire-forms.php | 1 + .../views/default/fields/dictionary.blade.php | 1 + src/Fields/Dictionary.php | 42 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 resources/views/default/fields/dictionary.blade.php create mode 100644 src/Fields/Dictionary.php diff --git a/config/livewire-forms.php b/config/livewire-forms.php index e12daff3..3038741f 100644 --- a/config/livewire-forms.php +++ b/config/livewire-forms.php @@ -23,6 +23,7 @@ Statamic\Fieldtypes\Text::class => Aerni\LivewireForms\Fields\Text::class, Statamic\Fieldtypes\Textarea::class => Aerni\LivewireForms\Fields\Textarea::class, Statamic\Fieldtypes\Toggle::class => Aerni\LivewireForms\Fields\Toggle::class, + Statamic\Fieldtypes\Dictionary::class => Aerni\LivewireForms\Fields\Dictionary::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..05c07b63 --- /dev/null +++ b/resources/views/default/fields/dictionary.blade.php @@ -0,0 +1 @@ +@include('livewire.forms.default.fields.select') \ No newline at end of file diff --git a/src/Fields/Dictionary.php b/src/Fields/Dictionary.php new file mode 100644 index 00000000..f45a4a1c --- /dev/null +++ b/src/Fields/Dictionary.php @@ -0,0 +1,42 @@ +max_items > 1; + } + + public function dictionary(): DictionaryInstance + { + $config = $this->field->config(); + $config = is_array($config) ? Arr::get($config, 'dictionary') : $config; + $config = is_array($config) ? $config : ['type' => $config]; + + if (! $handle = Arr::pull($config, 'type')) { + throw new UndefinedDictionaryException; + } + + if ($dictionary = Dictionaries::find($handle, $config)) { + return $dictionary; + } + + throw new DictionaryNotFoundException($handle); + } + + public function optionsProperty(?array $options = null): array + { + return $this->dictionary()->options(); + } +} From 43e779c1f2f018c0b340ca6e1b3bc7bf15b5309b Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Thu, 20 Feb 2025 10:00:05 -0500 Subject: [PATCH 2/8] Sort alphabetically --- config/livewire-forms.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/livewire-forms.php b/config/livewire-forms.php index 3038741f..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, @@ -23,7 +24,6 @@ Statamic\Fieldtypes\Text::class => Aerni\LivewireForms\Fields\Text::class, Statamic\Fieldtypes\Textarea::class => Aerni\LivewireForms\Fields\Textarea::class, Statamic\Fieldtypes\Toggle::class => Aerni\LivewireForms\Fields\Toggle::class, - Statamic\Fieldtypes\Dictionary::class => Aerni\LivewireForms\Fields\Dictionary::class, ], /* From 1fbcc8a49cdbe24e931f3e85f20d52020ff1d1a6 Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Thu, 20 Feb 2025 10:41:08 -0500 Subject: [PATCH 3/8] Allow multiple selection if max_items is null --- src/Fields/Dictionary.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fields/Dictionary.php b/src/Fields/Dictionary.php index f45a4a1c..c2331ee8 100644 --- a/src/Fields/Dictionary.php +++ b/src/Fields/Dictionary.php @@ -15,7 +15,7 @@ class Dictionary extends Select protected function multipleProperty(?bool $multiple = null): bool { - return $multiple ?? (int) $this->max_items > 1; + return $multiple ?? is_null($this->max_items) || $this->max_items > 1; } public function dictionary(): DictionaryInstance From 285dfe16d96fc52894f0d05c120a35e25eb0ea96 Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Thu, 20 Feb 2025 10:47:48 -0500 Subject: [PATCH 4/8] =?UTF-8?q?Ensure=20we=E2=80=99re=20getting=20the=20co?= =?UTF-8?q?rrect=20theme=20view?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resources/views/default/fields/dictionary.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/default/fields/dictionary.blade.php b/resources/views/default/fields/dictionary.blade.php index 05c07b63..ebd95962 100644 --- a/resources/views/default/fields/dictionary.blade.php +++ b/resources/views/default/fields/dictionary.blade.php @@ -1 +1 @@ -@include('livewire.forms.default.fields.select') \ No newline at end of file +@formView('fields.select') From 88b067d46b70bec8f85f32a383201a27ff5b8926 Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Thu, 20 Feb 2025 11:04:39 -0500 Subject: [PATCH 5/8] Simplify code --- src/Fields/Dictionary.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Fields/Dictionary.php b/src/Fields/Dictionary.php index c2331ee8..196e0d24 100644 --- a/src/Fields/Dictionary.php +++ b/src/Fields/Dictionary.php @@ -20,9 +20,7 @@ protected function multipleProperty(?bool $multiple = null): bool public function dictionary(): DictionaryInstance { - $config = $this->field->config(); - $config = is_array($config) ? Arr::get($config, 'dictionary') : $config; - $config = is_array($config) ? $config : ['type' => $config]; + $config = is_array($config = $this->field->get('dictionary')) ? $config : ['type' => $config]; if (! $handle = Arr::pull($config, 'type')) { throw new UndefinedDictionaryException; From 24929eba19c67dbb1711e77627b07360acbdaa90 Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Thu, 20 Feb 2025 11:17:26 -0500 Subject: [PATCH 6/8] =?UTF-8?q?Be=20more=20graceful=20when=20dictionary=20?= =?UTF-8?q?doesn=E2=80=99t=20exist?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Fields/Dictionary.php | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/Fields/Dictionary.php b/src/Fields/Dictionary.php index 196e0d24..4cdf8dea 100644 --- a/src/Fields/Dictionary.php +++ b/src/Fields/Dictionary.php @@ -18,23 +18,15 @@ protected function multipleProperty(?bool $multiple = null): bool return $multiple ?? is_null($this->max_items) || $this->max_items > 1; } - public function dictionary(): DictionaryInstance + public function dictionary(): ?DictionaryInstance { $config = is_array($config = $this->field->get('dictionary')) ? $config : ['type' => $config]; - if (! $handle = Arr::pull($config, 'type')) { - throw new UndefinedDictionaryException; - } - - if ($dictionary = Dictionaries::find($handle, $config)) { - return $dictionary; - } - - throw new DictionaryNotFoundException($handle); + return Dictionaries::find($config['type'], $config); } public function optionsProperty(?array $options = null): array { - return $this->dictionary()->options(); + return $this->dictionary()?->options() ?? []; } } From b7d34dd7f9a77dca82121286863eaff606c1666a Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Thu, 20 Feb 2025 11:18:10 -0500 Subject: [PATCH 7/8] Simplify code --- src/Fields/Dictionary.php | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/Fields/Dictionary.php b/src/Fields/Dictionary.php index 4cdf8dea..0634e9ca 100644 --- a/src/Fields/Dictionary.php +++ b/src/Fields/Dictionary.php @@ -2,12 +2,7 @@ namespace Aerni\LivewireForms\Fields; -use Aerni\LivewireForms\Fields\Select; -use Statamic\Dictionaries\Dictionary as DictionaryInstance; -use Statamic\Exceptions\DictionaryNotFoundException; -use Statamic\Exceptions\UndefinedDictionaryException; use Statamic\Facades\Dictionary as Dictionaries; -use Statamic\Support\Arr; class Dictionary extends Select { @@ -18,15 +13,12 @@ protected function multipleProperty(?bool $multiple = null): bool return $multiple ?? is_null($this->max_items) || $this->max_items > 1; } - public function dictionary(): ?DictionaryInstance + protected function optionsProperty(?array $options = null): array { $config = is_array($config = $this->field->get('dictionary')) ? $config : ['type' => $config]; - return Dictionaries::find($config['type'], $config); - } + $dictionary = Dictionaries::find($config['type'], $config); - public function optionsProperty(?array $options = null): array - { - return $this->dictionary()?->options() ?? []; + return $dictionary?->options() ?? []; } } From 62726c90a3b2c7c6d08e0472ae420dec3dd1fc7f Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Thu, 20 Feb 2025 12:15:28 -0500 Subject: [PATCH 8/8] Add update script --- src/ServiceProvider.php | 33 ++------------------ src/UpdateScripts/AddDictionaryFieldView.php | 26 +++++++++++++++ src/ViewManager.php | 31 ++++++++++++++++++ 3 files changed, 60 insertions(+), 30 deletions(-) create mode 100755 src/UpdateScripts/AddDictionaryFieldView.php 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(); + } }