Skip to content

Commit

Permalink
Add Dictionary Fieldtype support
Browse files Browse the repository at this point in the history
Add support for Dictionary Fieldtype on the front-end forms.
  • Loading branch information
nopticon committed Feb 20, 2025
1 parent 08eddf9 commit 7452ac9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/livewire-forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],

/*
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 @@
@include('livewire.forms.default.fields.select')
42 changes: 42 additions & 0 deletions src/Fields/Dictionary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

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
{
protected string $view = 'dictionary';

protected function multipleProperty(?bool $multiple = null): bool
{
return $multiple ?? (int) $this->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();
}
}

0 comments on commit 7452ac9

Please sign in to comment.