Utilise API endpoints for your Select Fieldtype options.
API Select requires a license.
You can purchase one at https://statamic.com/marketplace/addons/api-select
You may use API Select without a license while Statamic is in Trial mode.
Require it using Composer.
composer require laraluke/statamic-api-select
Publish the assets:
php artisan vendor:publish --provider="Laraluke\ApiSelectFieldtype\ServiceProvider"
Add the fieldtype to your fieldset/blueprint. You will then need to configure the following settings:
This is to define whether or not your endpoint value is a URL or a config variable.
If your endpoint type is a URL your endpoint value will look like this:
https://jsonplaceholder.typicode.com/users
If your endpoint type is a config variable your endpoint value will look like this:
endpoints.users
and your endpoints.php file would look something like this:
return [
'users' => 'https://jsonplaceholder.typicode.com/users',
];This is how long API requests will be cached for in minutes.
Set this value to 0 if you don't want to cache results.
If your data set isn't in the top-level of your API response you can define it's location using dot syntax.
So if your API result looks like:
{
"data": {
"users": []
}
}You would set the data set key value to:
data.users
Define the unique identifier to be used as the option value.
So if the iteration in your API result looks like this:
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
}You might set your item key to:
id
Define the value to be used as the option label.
So if the iteration in your API result looks like this:
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
}You might set your item label to:
name
Let's assume your API select field handle is api_select_users API response is like so:
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
},
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "Shanna@melissa.tv",
}
]{{ api_select_users.name }}
// Returns
Leanne Graham
{{ api_select_multi }}
{{ name }}
{{ /api_select_multi }}
// Returns
Leanne Graham
Ervin Howell