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

Select Field Type

The select field type provides a dropdown options input.

Introduction

anomaly.field_type.select

The select field type provides dropdown options input.

Configuration

Below is the full configuration available with defaults values:

"example" => [
    "type"   => "anomaly.field_type.select",
    "config" => [
        "options"       => [],
        "default_value" => null,
        "mode"          => "dropdown",
        "handler"       => "Anomaly\SelectFieldType\[email protected]"
    ]
]
Configuration
Key Example Description

options

["foo" => "Foo", "bar" => "Bar"]

The option array. Values may optionally be non-keyed.

default_value

foo

The default value key.

mode

radio

The input mode. Valid options are dropdown and radio.

handler

\Example\Test\[email protected]

The option handler. You may also specify years, countries, states, layouts, timezones, orcurrencies` to use alternate built-in handlers.

Option Groups

Select options can be organized into groups by nesting the options into keyed groups. The key is translated as the group's label.

"options" => [
    "module::message.examples" => [
        "foo" => "FOO",
        "bar" => "BAR",
    ],
    "module::message.tests" => [
        "baz" => "BAZ",
    ],
]

Option Handlers

Option handlers are responsible for setting the available 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 @handle will be assumed if no method is provided:

"handler" => \App\Example\MyOptions::class // Assumes @handle

Option handlers can also a handler with a closure:

"example" => [
    "config" => [
        "handler" => function (SelectFieldType $fieldType) {
            $fieldType->setOptions(
                [
                    "foo" => "FOO",
                    "bar" => "BAR"
                ]
            );
        }
    ]
]
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 SelectFieldType $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 MyOptions
{
    public function handle(SelectFieldType $fieldType)
    {
        $fieldType->setOptions(
            [
                "foo" => "FOO",
                "bar" => "BAR"
            ]
        );
    }
}

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 value with a key from the available options:

$entry->example = "foo";

Basic Output

The addon field type returns the values option key.

$entry->example; // foo

Presenter Output

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

SelectFieldTypePresenter::key()

The key method returns the selected value's key.

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

SelectFieldTypePresenter::value()

The value method returns the selected value.

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

SelectFieldTypePresenter::currency()

The currency method returns the currency definition for the selected value when using the currency handler.

Returns: array or null
Example
$decorated->example->currency()['symbol'];
Twig
{{ decorated.example.currency().symbol }}