---
title: Field Types
---

### 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

<table class="table table-bordered table-striped">

<thead>

<tr>

<th>Key</th>

<th>Required</th>

<th>Type</th>

<th>Description</th>

</tr>

</thead>

<tbody>

<tr>

<td>

$value

</td>

<td>

true

</td>

<td>

mixed

</td>

<td>

The value as provided by the field type setting the attribute

</td>

</tr>

</tbody>

</table>

###### Example

    public function modify($value)
    {
    	return serialize((array)$value);
    }

##### FieldTypeModifier::restore()

The `restore` method restores the value _from_ the database.

###### Returns: `mixed`

###### Arguments

<table class="table table-bordered table-striped">

<thead>

<tr>

<th>Key</th>

<th>Required</th>

<th>Type</th>

<th>Description</th>

</tr>

</thead>

<tbody>

<tr>

<td>

$value

</td>

<td>

true

</td>

<td>

mixed

</td>

<td>

The value from the database.

</td>

</tr>

</tbody>

</table>

###### 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

<table class="table table-bordered table-striped">

<thead>

<tr>

<th>Key</th>

<th>Required</th>

<th>Type</th>

<th>Description</th>

</tr>

</thead>

<tbody>

<tr>

<td>

$value

</td>

<td>

true

</td>

<td>

mixed

</td>

<td>

The value from the field type modifier.

</td>

</tr>

</tbody>

</table>

###### 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

<table class="table table-bordered table-striped">

<thead>

<tr>

<th>Key</th>

<th>Type</th>

<th>Description</th>

</tr>

</thead>

<tbody>

<tr>

<td>

$table

</td>

<td>

\Illuminate\Database\Schema\Blueprint

</td>

<td>

The table blueprint utility.

</td>

</tr>

<tr>

<td>

$assignment

</td>

<td>

\Anomaly\Streams\Platform\Assignment\Contract\AssignmentInterface

</td>

<td>

The assignment object representing the assigned field.

</td>

</tr>

</tbody>

</table>

##### FieldTypeSchema::updateColumn()

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

###### Returns: `void`

###### Arguments

<table class="table table-bordered table-striped">

<thead>

<tr>

<th>Key</th>

<th>Type</th>

<th>Description</th>

</tr>

</thead>

<tbody>

<tr>

<td>

$table

</td>

<td>

\Illuminate\Database\Schema\Blueprint

</td>

<td>

The table blueprint utility.

</td>

</tr>

<tr>

<td>

$assignment

</td>

<td>

\Anomaly\Streams\Platform\Assignment\Contract\AssignmentInterface

</td>

<td>

The updated assignment object representing the assigned field.

</td>

</tr>

</tbody>

</table>

##### FieldTypeSchema::renameColumn()

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

###### Returns: `void`

###### Arguments

<table class="table table-bordered table-striped">

<thead>

<tr>

<th>Key</th>

<th>Type</th>

<th>Description</th>

</tr>

</thead>

<tbody>

<tr>

<td>

$table

</td>

<td>

\Illuminate\Database\Schema\Blueprint

</td>

<td>

The table blueprint utility.

</td>

</tr>

<tr>

<td>

$from

</td>

<td>

\Anomaly\Streams\Platform\Addon\FieldType\FieldType

</td>

<td>

The field type from the updated fields.

</td>

</tr>

<tr>

<td>

INTERNAL $to

</td>

<td>

\Anomaly\Streams\Platform\Addon\FieldType\FieldType

</td>

<td>

The current field type is always available as $this->fieldType

</td>

</tr>

</tbody>

</table>

##### FieldTypeSchema::changeColumn()

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

###### Returns: `void`

###### Arguments

<table class="table table-bordered table-striped">

<thead>

<tr>

<th>Key</th>

<th>Type</th>

<th>Description</th>

</tr>

</thead>

<tbody>

<tr>

<td>

$table

</td>

<td>

\Illuminate\Database\Schema\Blueprint

</td>

<td>

The table blueprint utility.

</td>

</tr>

<tr>

<td>

$assignment

</td>

<td>

\Anomaly\Streams\Platform\Assignment\Contract\AssignmentInterface

</td>

<td>

The updated assignment object representing the changed field.

</td>

</tr>

</tbody>

</table>

##### FieldTypeSchema::dropColumn()

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

###### Returns: `void`

###### Arguments

<table class="table table-bordered table-striped">

<thead>

<tr>

<th>Key</th>

<th>Type</th>

<th>Description</th>

</tr>

</thead>

<tbody>

<tr>

<td>

$table

</td>

<td>

\Illuminate\Database\Schema\Blueprint

</td>

<td>

The table blueprint utility.

</td>

</tr>

</tbody>

</table>

##### FieldTypeSchema::backupColumn()

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

###### Returns: `void`

###### Arguments

<table class="table table-bordered table-striped">

<thead>

<tr>

<th>Key</th>

<th>Type</th>

<th>Description</th>

</tr>

</thead>

<tbody>

<tr>

<td>

$table

</td>

<td>

\Illuminate\Database\Schema\Blueprint

</td>

<td>

The table blueprint utility.

</td>

</tr>

</tbody>

</table>

##### FieldTypeSchema::restoreColumn()

The `restoreColumn` restores the backup data from the `backupColumn` method.

###### Returns: `void`

###### Arguments

<table class="table table-bordered table-striped">

<thead>

<tr>

<th>Key</th>

<th>Type</th>

<th>Description</th>

</tr>

</thead>

<tbody>

<tr>

<td>

$table

</td>

<td>

\Illuminate\Database\Schema\Blueprint

</td>

<td>

The table blueprint utility.

</td>

</tr>

</tbody>

</table>

#### 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

<table class="table table-bordered table-striped">

<thead>

<tr>

<th>Key</th>

<th>Type</th>

<th>Description</th>

</tr>

</thead>

<tbody>

<tr>

<td>

$assignment

</td>

<td>

\Anomaly\Streams\Platform\Assignment\Contract\AssignmentInterface

</td>

<td>

The assignment object representing the assigned field.

</td>

</tr>

</tbody>

</table>

#### 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

<table class="table table-bordered table-striped">

<thead>

<tr>

<th>Key</th>

<th>Type</th>

<th>Description</th>

</tr>

</thead>

<tbody>

<tr>

<td>

$query

</td>

<td>

\Illuminate\Database\Eloquent\Builder

</td>

<td>

The query builder for the table entries.

</td>

</tr>

<tr>

<td>

$filter

</td>

<td>

\Anomaly\Streams\Platform\Ui\Table\Component\Filter\Contract\FilterInterface

</td>

<td>

The table filter interface.

</td>

</tr>

</tbody>

</table>
