Skip to content

Commit

Permalink
dictionaries have keywords. the dictionary fieldtype pulls all of the…
Browse files Browse the repository at this point in the history
…m in.
  • Loading branch information
jasonvarga committed Nov 4, 2024
1 parent 44b4355 commit bb40700
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Dictionaries/Countries.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Countries extends BasicDictionary
{
protected string $valueKey = 'iso3';
protected array $searchable = ['name', 'iso3'];
protected array $keywords = ['countries', 'country'];
private array $regions;
private array $subregions;

Expand Down
1 change: 1 addition & 0 deletions src/Dictionaries/Currencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class Currencies extends BasicDictionary
{
protected string $valueKey = 'code';
protected array $keywords = ['currencies', 'currency', 'money', 'dollar'];

protected function getItemLabel(array $item): string
{
Expand Down
6 changes: 6 additions & 0 deletions src/Dictionaries/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ abstract class Dictionary

protected array $fields = [];
protected array $config = [];
protected array $keywords = [];

abstract public function options(?string $search = null): array;

Expand Down Expand Up @@ -75,4 +76,9 @@ public function optionItems(?string $search = null): array
->map(fn ($label, $value) => new Item($value, $label, $this->get($value)->extra()))
->all();
}

public function keywords(): array
{
return $this->keywords;
}
}
2 changes: 2 additions & 0 deletions src/Dictionaries/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class File extends BasicDictionary
{
protected array $keywords = ['files', 'file', 'json', 'csv', 'yaml', 'yml'];

protected function fieldItems()
{
return [
Expand Down
1 change: 1 addition & 0 deletions src/Dictionaries/Timezones.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class Timezones extends BasicDictionary
{
protected string $valueKey = 'name';
protected array $keywords = ['timezone', 'tz', 'zone', 'time', 'date'];

protected function getItemLabel(array $item): string
{
Expand Down
11 changes: 11 additions & 0 deletions src/Fieldtypes/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,15 @@ public function addGqlTypes()
{
GraphQL::addType($this->dictionary()->getGqlType());
}

public function keywords(): array
{
return \Statamic\Facades\Dictionary::all()
->flatMap(fn ($dictionary) => [
str($dictionary->handle())->replace('_', ' ')->toString(),
...$dictionary->keywords(),
])
->merge(['select', 'option', 'choice', 'dropdown', 'list'])
->unique()->values()->all();
}
}

0 comments on commit bb40700

Please sign in to comment.