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

Addon Field Type

The addon field type provides a dropdown input of addons.

Introduction

anomaly.field_type.addon

The addon field type provides a configurable select dropdown of addons. The field type acts very similar to a relationship between an addon and an entry.

Configuration

Below is a list of available configuration with default values.

"example" => [
    "type"   => "anomaly.field_type.addon",
    "config" => [
        "default_value" => null,
        "type"          => null,
        "search"        => null,
        "theme_type"    => null,
        "handler"       => "Anomaly\AddonFieldType\Handler\[email protected]"
    ]
]
Configuration
Key Example Description

default_value

anomaly.module.files

The default value.

type

module

If defined only addons of the specified type will be displayed. Valid options are module, field_type, plugin, module, and extension.

search

anomaly.module.users::authentication.*

The extension search option will search and return only extensions with a provision string that matches your search.

theme_type

admin

Restrict what type of themes are returned. Valid options are admin or standard.

handler

[email protected]

Handlers let you override control of the options logic.

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 dropdown options.

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

"handler" => /Example/Test/CustomOptions::class // Assumes @handle

Option handlers can also a handler with a closure:

"handler" => function (AddonFieldType $fieldType, ExampleModule $module) {
    $fieldType->setOptions(
        [
            "anomaly.module.example" => $module->getName()
        ]
    );
}
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 callable string is called via Laravel's service container. The AddonFieldType $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(AddonFieldType $fieldType)
    {
        $fieldType->setOptions(
            [
                "anomaly.module.example" => 'Example'
            ]
        );
    }
}

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 addon field type value with an addon's namespace:

$entry->example = "anomaly.module.files"

You can also set the value with an instance of an addon:

$entry->example = $module;

Lastly you can set the value with an instance of an addon presenter:

$entry->example = $decorated;

Basic Output

The value returned by the field type is either null or an instance of the selected addon.

Example
$entry->example->getNamespace(); // anomaly.module.files

Presenter Output

The addon field type will always return null or an instance of the addon. When accessing the field value from a decorated entry model the returned addon instance will be automatically decorated.

Example
$decorated->example->name; // Files Module
Twig
{{ decorated.example.name }} // Files Module