Field Types

Field types are responsible for rendering form inputs and managing data transportation in and out of the database and it's models.

Displaying Inputs

The main aspect of field types from an end-user perspective is the form inputs they provide. This section will go over how to render inputs and filters for field types.

FieldType::render()

The render method returns the input wrapped in a field group wrapper for use in a form. This output method includes the label, required flag, instructions, warning, and the input.

Returns: string
Twig
{{ field_type.render()|raw }}
FieldType::getInput()

The getInput returns the rendered input view used for forms. The view rendered is determined by the field type's $inputView property.

This method returns only the input view. No surrounding field group wrapper.

Returns: string
Twig
{{ field_type.getInput()|raw }}
FieldType::getFilter()

The getFilter method returns the rendered input view for table filtering.

Returns: string
Twig
{{ field_type.getFilter()|raw }}

Presenters

Field type presenters decorate the field type and it's contained value. Because objects are automatically decorated on the way to views field type presenters will always be returned for entry values by default.

{{ entry.attribute }} // The field type presenter::__toString()
{{ entry.attribute.value }} // The raw value from the field type presenter

It is because of this you must using .value within if statements.

FieldType::getPresenter()

The getPresenter method returns a new presenter instance. By default method uses class transformation to convert YourFieldType class to YourFieldTypePresenter.

Returns: Anomaly\Streams\Platform\Addon\FieldType\FieldTypePresenter
Example
$fieldType->getPresenter()->foo();

Modifiers

Modifiers are classes that modify values for database storage and restore them before hydrating the model. By default no modification is done.

FieldType::getModifier()

The getModifier method returns a new modifier instance. By default this method uses class transformation to convert YourFieldType to YourFieldTypeModifier.

Returns: Anomaly\Streams\Platform\Addon\FieldType\FieldTypeModifier
FieldTypeModifier::modify()

The modify method modifies the $value for storage in the database.

Returns: mixed
Arguments
Key Required Type Description
$value true mixed The value as provided by the field type setting the attribute
Example
public function modify($value)
{
    return serialize((array)$value);
}
FieldTypeModifier::restore()

The restore method restores the value from the database.

Returns: mixed
Arguments
Key Required Type Description
$value true mixed The value from the database.
Example
public function restore($value)
{
    if (!$value) {
        return [];
    }

    if (is_array($value)) {
        return $value;
    }

    return (array)unserialize($value);
}

Accessors

Accessors are responsible for setting the value data on the entry model. By default the value is set on the model as an attribute with the same name as the field.

$entry->{field} = $value;
FieldType::getAccessor()

The getAccessor method returns a new accessor instance. By default the method uses class transformation to convert YourFieldType to YourFieldTypeAccessor.

Returns: Anomaly\Streams\Platform\Addon\FieldType\FieldTypeAccessor
FieldTypeAccessor::set()

The set method set's the $value on the entry.

Returns: void
Arguments
Key Required Type Description
$value true mixed The value from the field type modifier.
Example
public function set($value)
{
    $entry = $this->fieldType->getEntry();
    $attributes = $entry->getAttributes();
    if (is_numeric($value)) {
        $attributes[$this->fieldType->getColumnName()] = $value;
    }
    if (is_object($value) && $data = $this->toData($value)) {
        $attributes[$this->fieldType->getField() . '_data'] = json_encode($data);
    }
    if (is_array($value) && $data = $this->toData($value)) {
        $attributes[$this->fieldType->getField() . '_data'] = json_encode($data);
    }
    if (is_null($value)) {
        $attributes[$this->fieldType->getColumnName()]      = $value;
        $attributes[$this->fieldType->getField() . '_data'] = $value;
    }
    $entry->setRawAttributes($attributes);
}
FieldTypeAccessor::get()

The get method get's the value off the entry.

Returns: mixed
Example
public function get()
{
    $entry = $this->fieldType->getEntry();

    $attributes = $entry->getAttributes();

    return [
        'image' => array_get($attributes, $this->fieldType->getColumnName()),
        'data' => array_get($attributes, $this->fieldType->getColumnName() . '_data')
    ];
}

Schema

Field type schema classes help control the schema changes required by the field type.

FieldType::getSchema()

The getSchema method returns a new schema instance. By default this method uses class transformation to convert YourFieldType class to YourFieldTypeSchema.

Returns: Anomaly\Streams\Platform\Addon\FieldType\FieldTypeSchema
FieldTypeSchema::addColumn()

The addColumn method is responsible for adding the column required by the field type to the database table. By default this is automated and adds a column named after the field_slug and uses the column type as defined by the field type.

Returns: void
Arguments
Key Type Description
$table \Illuminate\Database\Schema\Blueprint The table blueprint utility.
$assignment \Anomaly\Streams\Platform\Assignment\Contract\AssignmentInterface The assignment object representing the assigned field.
FieldTypeSchema::updateColumn()

The updateColumn updates the columns features as required by the field or assignment changes.

Returns: void
Arguments
Key Type Description
$table \Illuminate\Database\Schema\Blueprint The table blueprint utility.
$assignment \Anomaly\Streams\Platform\Assignment\Contract\AssignmentInterface The updated assignment object representing the assigned field.
FieldTypeSchema::renameColumn()

The renameColumn is responsible for renaming the field type column(s) when a field object is updated.

Returns: void
Arguments
Key Type Description
$table \Illuminate\Database\Schema\Blueprint The table blueprint utility.
$from \Anomaly\Streams\Platform\Addon\FieldType\FieldType The field type from the updated fields.
INTERNAL $to \Anomaly\Streams\Platform\Addon\FieldType\FieldType The current field type is always available as $this->fieldType
FieldTypeSchema::changeColumn()

The changeColumn method changes the column type as needed when changing the field type for a field.

Returns: void
Arguments
Key Type Description
$table \Illuminate\Database\Schema\Blueprint The table blueprint utility.
$assignment \Anomaly\Streams\Platform\Assignment\Contract\AssignmentInterface The updated assignment object representing the changed field.
FieldTypeSchema::dropColumn()

The dropColumn method drops the field type's column from the database table when the assignment is deleted.

Returns: void
Arguments
Key Type Description
$table \Illuminate\Database\Schema\Blueprint The table blueprint utility.
FieldTypeSchema::backupColumn()

The backupColumn method temporarily backs up the column data in cache.

Returns: void
Arguments
Key Type Description
$table \Illuminate\Database\Schema\Blueprint The table blueprint utility.
FieldTypeSchema::restoreColumn()

The restoreColumn restores the backup data from the backupColumn method.

Returns: void
Arguments
Key Type Description
$table \Illuminate\Database\Schema\Blueprint The table blueprint utility.

Parsers

Field type parsers allow you to control what is parsed on the compiled entry model when the stream, assignments, or fields are modified. This method will also fire during manual compiling with the streams:compile artisan command.

FieldType::getParser()

The getParser method returns a new parser instance. By default this method uses class transformation to convert YourFieldType class to YourFieldTypeParser.

Returns: Anomaly\Streams\Platform\Addon\FieldType\FieldTypeParser
FieldTypeParser::relation()

The relation method is only ran when the field type provides a getRelation method.

Returns: string
Arguments
Key Type Description
$assignment \Anomaly\Streams\Platform\Assignment\Contract\AssignmentInterface The assignment object representing the assigned field.

Query Builders

The field type query builders provide methods for manipulating query builders. These methods are using for filtering field type values and for extending the functionality of the query builder through the field type.

FieldTypeQuery::filter()

The filter method filters a query builder with the value provided by a table filter interface.

Returns: void
Arguments
Key Type Description
$query \Illuminate\Database\Eloquent\Builder The query builder for the table entries.
$filter \Anomaly\Streams\Platform\Ui\Table\Component\Filter\Contract\FilterInterface The table filter interface.