Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Dictionary Fieldtype support #83

Merged
merged 8 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/livewire-forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions resources/views/default/fields/dictionary.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@formView('fields.select')
24 changes: 24 additions & 0 deletions src/Fields/Dictionary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Aerni\LivewireForms\Fields;

use Statamic\Facades\Dictionary as Dictionaries;

class Dictionary extends Select
{
protected string $view = 'dictionary';

protected function multipleProperty(?bool $multiple = null): bool
{
return $multiple ?? is_null($this->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() ?? [];
}
}
33 changes: 3 additions & 30 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -95,15 +96,15 @@ 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,
],
'theme' => [
'type' => 'select',
'display' => __('Theme'),
'instructions' => __('Choose the theme for this form.'),
'options' => $this->themeOptions(),
'options' => ViewManager::themes(),
'clearable' => true,
'width' => 50,
],
Expand All @@ -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();
}
}
26 changes: 26 additions & 0 deletions src/UpdateScripts/AddDictionaryFieldView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Aerni\LivewireForms\UpdateScripts;

use Illuminate\Support\Facades\File;
use Statamic\UpdateScripts\UpdateScript;
use Aerni\LivewireForms\Facades\ViewManager;

class AddDictionaryFieldView extends UpdateScript
{
public function shouldUpdate($newVersion, $oldVersion)
{
return $this->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.');
}
}
31 changes: 31 additions & 0 deletions src/ViewManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Aerni\LivewireForms;

use Illuminate\Support\Str;
use Illuminate\Support\Facades\File;

class ViewManager
{
public function viewPath(string $view): string
Expand Down Expand Up @@ -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();
}
}