-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Dictionary Fieldtype on the front-end forms.
- Loading branch information
Showing
3 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@include('livewire.forms.default.fields.select') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |