From bb4070038b2176d8bf4eb2cefce896eeadf0c4f6 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Mon, 4 Nov 2024 14:22:45 -0500 Subject: [PATCH] dictionaries have keywords. the dictionary fieldtype pulls all of them in. --- src/Dictionaries/Countries.php | 1 + src/Dictionaries/Currencies.php | 1 + src/Dictionaries/Dictionary.php | 6 ++++++ src/Dictionaries/File.php | 2 ++ src/Dictionaries/Timezones.php | 1 + src/Fieldtypes/Dictionary.php | 11 +++++++++++ 6 files changed, 22 insertions(+) diff --git a/src/Dictionaries/Countries.php b/src/Dictionaries/Countries.php index 651543d473..b0476a43df 100644 --- a/src/Dictionaries/Countries.php +++ b/src/Dictionaries/Countries.php @@ -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; diff --git a/src/Dictionaries/Currencies.php b/src/Dictionaries/Currencies.php index ba528b07e0..f18918ab1b 100644 --- a/src/Dictionaries/Currencies.php +++ b/src/Dictionaries/Currencies.php @@ -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 { diff --git a/src/Dictionaries/Dictionary.php b/src/Dictionaries/Dictionary.php index dd704bc380..269b00d04f 100644 --- a/src/Dictionaries/Dictionary.php +++ b/src/Dictionaries/Dictionary.php @@ -15,6 +15,7 @@ abstract class Dictionary protected array $fields = []; protected array $config = []; + protected array $keywords = []; abstract public function options(?string $search = null): array; @@ -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; + } } diff --git a/src/Dictionaries/File.php b/src/Dictionaries/File.php index 57bdc173b6..62765f74b9 100644 --- a/src/Dictionaries/File.php +++ b/src/Dictionaries/File.php @@ -7,6 +7,8 @@ class File extends BasicDictionary { + protected array $keywords = ['files', 'file', 'json', 'csv', 'yaml', 'yml']; + protected function fieldItems() { return [ diff --git a/src/Dictionaries/Timezones.php b/src/Dictionaries/Timezones.php index 8a4e9420d0..d333633b54 100644 --- a/src/Dictionaries/Timezones.php +++ b/src/Dictionaries/Timezones.php @@ -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 { diff --git a/src/Fieldtypes/Dictionary.php b/src/Fieldtypes/Dictionary.php index 5d7bbcddae..c74cf9b564 100644 --- a/src/Fieldtypes/Dictionary.php +++ b/src/Fieldtypes/Dictionary.php @@ -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(); + } }