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

Tags Field Type

The tag field type provides a flexible tag management input.

Introduction

The tag field type provides a flexible tag management input.

Configuration

Below is the full configuration available with defaults values:

"example" => [
    "type"   => "anomaly.field_type.tags",
    "config" => [
        "min"           => null,
        "max"           => null,
        "options"       => [],
        "free_input"    => true,
        "filter"        => null,
        "default_value" => null,
        "handler"       => "Anomaly\TagsFieldType\[email protected]"
    ]
]
Configuration
Key Example Description

min

2

The minimum number of allowed tags.

max

2

The maximum number of allowed tags.

options

["foo", "bar"]

An array of available options.

free_input

false

If enabled tags not defined in available options can be entered.

filter

FILTER_VALIDATE_EMAIL

A validation filter to apply. Valid options are FILTER_VALIDATE_EMAIL, FILTER_VALIDATE_URL, and FILTER_VALIDATE_IP.

default_value

["foo"]

The default value.

handler

App\[email protected]

The options handler.

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 [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 (TagsFieldType $fieldType) {
            $fieldType->setOptions(
                [
                    "foo",
                    "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 TagsFieldType $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 MyTags
{
    public function handle(TagsFieldType $fieldType)
    {
        $fieldType->setOptions(
            [
                "foo",
                "bar",
            ]
        );
    }
}

Usage

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

Setting Values

You can set the tags field type value with an array of selected options:

$entry->example = ["foo", "bar"];

Basic Output

The tags field type returns the option values:

$entry->example; // ["foo", "bar"]

Presenter Output

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

TagsFieldTypePresenter::labels()

The labels method returns an array of labels from select tags.

Returns: array
Arguments
Key Required Type Default Description

$context

false

string

defaut

The label context color. Valid options are success, info, warning, danger, and default.

$size

false

string

sm

The label size. Valid options are xs, sm, and lg.

Example
foreach ($decorated->labels('success') as $label) {
    echo $label;
}
Twig
{{ decorated.labels('success')|join(' ')|raw }}