Have you seen our new video tutorials? Check it out!

Country Field Type

The country field type provides a versatile country input.

Introduction

anomaly.field_type.country

The country field type provides a customizable input that ties into the country support in the Streams Platform.

Configuration

Below is the full configuration available with defaults values:

"example" => [
    "type"   => "anomaly.field_type.country",
    "config" => [
        "default_value" => null,
        "top_options"   => null,
        "mode"          => "text",
        "handler"       => "Anomaly\CountryFieldType\Handler\[email protected]"
    ]
]
Configuration
Key Example Description

default_value

US

The default value.

top_options

["US", "CA", "MX"]

The top dropdown options.

mode

dropdown

The input mode. Valid options are input and dropdown.

handler

App\Example\[email protected]

The options handler.

Addon Configuration

The country options are controlled by the Streams Platform countries.php configuration file.

You can override these options by publishing the Streams Platform with:

php artisan streams:publish
Contribute: If you have countries to add or have found an error, submit a pull request to https://github.com/anomalylabs/streams-platform

Option Handlers

Option handlers are responsible for setting the available country options on the field type. You can define your own option handler to add your own logic to available options.

You can define custom handlers as a callable string where [email protected] will be assumed if no method is provided:

"handler" => "[email protected]"

Option handlers can also a handler with a closure:

"example" => [
    "config" => [
        "handler" => function (CountryFieldType $fieldType) {
            $fieldType->setOptions(
                [
                    "US" => "streams::country.us",
                    "CA" => "streams::country.ca"
                ]
            );
        }
    ]
]
Remember: Closures can not be stored in the database so your closure type handlers must be set / overridden from the form builder.
Writing Option Handlers

Writing custom option handlers is easy. To begin create a class with the method you defined in the config option.

"handler" => "[email protected]"

The handler string is called via Laravel's service container. The CountryFieldType $fieldType is passed as an argument.

Pro Tip: Handlers are called through Laravel's service container so method and class injection is supported.
<?php namespace App\Example;

class MyCountries
{
    public function handle(CountryFieldType $fieldType)
    {
        $fieldType->setOptions(
            [
                "US" => "anomaly.field_type.country::country.us",
                "CA" => "anomaly.field_type.country::country.ca",
            ]
        );
    }
}

Usage

This section will show you how to use the field type via API and in the view layer.

Setting Values

You must set the country field type value with a valid ISO-2 country code from the available options.

$entry->example = "US";
Note: The value is case insensitive and is uppercased when set.

Basic Output

The country field type returns null or the selected ISO-2 country code.

$entry->example; // US

Presenter Output

This section will show you how to use the decorated value provided by the \Anomaly\CountryFieldType\CountryFieldTypePresenter class.

CountryFieldTypePresenter::code()

The code method returns the selected country's ISO code.

Returns: string
Example
$decorated->example->code();
Twig
{{ decorated.example.code() }}

CountryFieldTypePresenter::name()

The name method returns the name of the selected country.

Returns: string
Arguments
Key Required Type Default Description

$locale

false

string

The active locale.

The locale code to translate the name to.

Example
$decorated->example->name('es');
Twig
{{ decorated.example.name('es') }}

CountryFieldTypePresenter::currency()

The currency method returns the country's currency information.

Contribute: If you have currency information to add or have found an error, submit a pull request to https://github.com/anomalylabs/streams-platform
Returns: array or null
Example
$decorated->example->currency()['symbol'];
Twig
{{ decorated.example.currency().symbol }}

CountryFieldTypePresenter::locale()

The locale method returns the country's locale information.

Contribute: If you have locale information to add or have found an error, submit a pull request to https://github.com/anomalylabs/streams-platform
Example
$decorated->example->locale()['direction'];
Twig
{{ decorated.example.locale().direction }}