Extending Field Types
Content Blocks already comes with pre-defined Field Types. You should utilize them as much as possible. But in case you need special types which cannot be covered from the basic ones, it is possible to extend these with your own types.
When to add new field types
You can add own field types whenever you have fields with a distinct set of
configuration options set. These options can be set as default in your type
so you gain a semantic naming for this field. For example type Money
could
be the core TCA type
number
with
format
set to
decimal
as
default value.
Another use case is having a custom made TCA renderType, which is not covered by existing field types. This could be e.g. TCA type user with a custom renderType. This way it is possible to use the renderType as a first-class type in Content Blocks.
Adding a new Field Type
To add a new Field Type it is required to implement the
\TYPO3\
. Have a look at
the Core implementations to get a feeling on how to implement them. You may
extend
\TYPO3\
for easier
usage. The registration itself happens through the PHP attribute
\TYPO3\
, which expects the
name
,
tca
and
searchable
arguments.
Note
The registration is based on dependency injection. Make sure your extension has it enabled in Configuration/Services.yaml.
interface FieldTypeInterface
{
public function getName(): string;
public function getTcaType(): string;
public function isSearchable(): bool;
public function setName(string $name): void;
public function setTcaType(string $tcaType): void;
public function setSearchable(bool $searchable): void;
public function createFromArray(array $settings): FieldTypeInterface;
public function getTca(): array;
public function getSql(string $column): string;
}
createFromArray
The incoming
$settings
array is the converted YAML definition. Apply your
logic here to instantiate a new type.
getTca
As the name suggests, you generate your TCA config here. This can be done based
on the settings provided when
create
was called. Of course this
can also be a static configuration, if you don't want to provide any settings.
getSql
The SQL definition for your database column. Use
$column
as the
column name. Return empty string to fall back to standard definition.
Defined by
\TYPO3\
to return
empty string for automatic detection. Override this, if you need a specific
column definition.
setName/getName
This is the actual type identifier for usage in the YAML
type
option.
It is recommended to use UpperCamelCase, but it's not required.
Defined by
\TYPO3\
.
setTcaType/getTcaType
The TCA type, the new Content Blocks type is based on.
Defined by
\TYPO3\
.
setSearchable/isSearchable
Whether the field contents should be searchable in global search.
Defined by
\TYPO3\
.
Example
Example for a field type "Money".
<?php
declare(strict_types=1);
namespace VENDOR\MyExtension\FieldType;
use TYPO3\CMS\ContentBlocks\FieldType\AbstractFieldType;
use TYPO3\CMS\ContentBlocks\FieldType\FieldType;
use TYPO3\CMS\ContentBlocks\FieldType\WithCommonProperties;
#[FieldType(name: 'Money', tcaType: 'number', searchable: false)]
final class MoneyFieldType extends AbstractFieldType
{
use WithCommonProperties;
private float $default = 0.00;
private bool $required = false;
private bool $nullable = false;
public function createFromArray(array $settings): self
{
// Clone the service instance, so that state for name, tcaType and searchable is carried over.
$self = clone $this;
$self->setCommonProperties($settings);
$default = $settings['default'] ?? $self->default;
$self->default = (float)$default;
$self->required = (bool)($settings['required'] ?? $self->required);
$self->nullable = (bool)($settings['nullable'] ?? $self->nullable);
return $self;
}
public function getTca(): array
{
$tca = $this->toTca();
$config['type'] = $this->getTcaType();
if ($this->default !== 0.0) {
$config['default'] = $this->default;
}
if ($this->required) {
$config['required'] = true;
}
$config['format'] = 'decimal';
$tca['config'] = array_replace($tca['config'] ?? [], $config);
return $tca;
}
}