Configuration

Below is the full configuration available with defaults values:

"example" => [
    "type"   => "anomaly.field_type.colorpicker",
    "config" => [
        "default_value" => null,
        "format"        => "hex",
        "colors"        => null,
        "handler"       => "Anomaly\ColorpickerFieldType\Handler\[email protected]",
    ]
]
Configuration
Key Example Description
default_value `#61259e` The default value.
format code The color format to display. Valid options are `hex`, `rgb`, and `rgba`.
colors `["#61259e", "#38b5e6", "#24ce7b"]` Predefined color swatches.
handler `Example\Test\[email protected]` The predefined color handler.

Color Handlers

Color handlers are responsible for setting the predefined colors on the field type. You can define your own handler to add your own logic to predefined color options.

You can define custom handlers as a callable string where @handle will be assumed if no method is provided:

"handler" => "App/Example/[email protected]"

Color handlers can also a handler with a closure:

"example" => [
    "config" => [
        "handler" => function (ColorpickerFieldType $fieldType) {
            $fieldType->setOptions(
                [
                    "#61259e",
                    "#38b5e6",
                    "#24ce7b"
                ]
            );
        }
    ]
]

Closures can not be stored in the database so your closure type handlers must be set / overridden from the form builder.

Writing Color Handlers

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

"handler" => "App/Example/[email protected]"

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

Handlers are called through Laravel's service container so method and class injection is supported.

<?php namespace App/Example;

class MyColors
{
    public function handle(ColorpickerFieldType $fieldType)
    {
        $fieldType->setOptions(
            [
                "#61259e",
                "#38b5e6",
                "#24ce7b"
            ]
        );
    }
}