Configuration

Below is a list of available configuration with default values:

"example" => [
    "type"   => "anomaly.field_type.relationship",
    "config" => [
        "related"        => null,
        "mode"           => "lookup",
        "key_name"       => null,
        "title_name"     => null,
        "value_table"    => null,
        "selected_table" => null,
        "lookup_table"   => null,
        "default_value"  => null,
        "handler"        => "\Anomaly\RelationshipFieldType\Handler\[email protected]",
    ]
]
Configuration
Key Example Description
related `\Anomaly\UsersModule\User\UserModel` The related model or stream dot notation (`namespace.stream`).
mode dropdown The input mode. Valid options are `lookup`, `search`, and `dropdown`.
key_name slug The name of the key field. Default is `id`. Only applies to `dropdown` mode.
title_name name The name of the title field. Default is the `title_column`. Only applies to `dropdown` mode.
value_table `\App\Example\MyValueTable` The builder for the value table.
selected_table `\App\Example\MySelectedTable` The builder for the selections table.
lookup_table `\App\Example\MyLookupTable` The builder for the lookup table.
handler `\App\Example\[email protected]` The option 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 @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 (RelationshipFieldType $fieldType, ExampleRepositoryInterface $entries) {
            $fieldType->setOptions($entries->getCustomEntries()->pluck('title', 'id')->all());
        }
    ]
]

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" => "App/Example/[email protected]"

The handler string is called via Laravel's service container. The RelationshipFieldType $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 MyOptions
{
    public function handle(RelationshipFieldType $fieldType, ExampleRepositoryInterface $entries) {
        $fieldType->setOptions(
            $entries->getCustomEntries()->pluck('title', 'id')->all()
        );
    }
}