Geography-related suggest modes.
Installation
Copy the GeoSuggestions
folder into site/addons/
and run php please update:addons
.
Usage
Example Fieldset
fields: country: type: suggest display: Country mode: geo-suggestions.country
This will get you a list of countries with corresponding 2-letter ISO codes as values.
Customization
You can easily create your own suggest modes. This addon comes with rinvex/countries library which has a lot of country-related data.
For instance, say you want to have a nationality field with a suggest mode.
You can use countries()
helper to get the data for all countries and pick a value that you need.
For this particular scenario we need a key called demonym
.
-
Create a class
DemonymSuggestMode
inSuggestModes
directory extendingCountrySuggestMode
. -
Play around with
countries()
helper usingphp please tinker
to get the data you need. I came up with this:collect(countries(true))->pluck('demonym')->filter()->values()->all();For this case I'm calling
countries(true)
to get a bigger data set which includes a demonym for each country. I'm wrapping it insidecollect()
to turn the array into a Laravel Collection to get many convenience methods it provides. Then I'm picking (plucking) ademonym
key from each country record and filter the collection to get rid of empty records. After then I callvalues()
to get only values from the collection stripping the keys and finallyall()
turn the collection back to an array. -
Implement
suggestions()
method like you would do for a suggest mode. -
Wrap the array you got in step 2 in
$this->map()
call. This will make sure you data will be formatted in a way that Statamic expects. If you have a simple array, both values and text labels will be the same. But if you want to have different values simply make an assosiative array. -
Finally, use your suggest mode in a fieldset with a suggest field:
fields:country:type: suggestdisplay: Nationalitymode: geo-suggestions.demonym