Create custom generic field type¶
The Generic field type is an abstract implementation of field types holding structured data for example, address. You can use it as a base for custom field types. The Generic field type comes with the implementation of basic methods, reduces the number of classes which must be created, and simplifies the tagging process.
A more in-depth, step-by-step tutorial can be viewed here: Creating a Point 2D field type.
Tip
You should not use the Generic field type when you need a very specific implementation or complete control over the way data is stored.
Simple hash values
A simple hash value always means an array of scalar values and/or nested arrays of scalar values. To avoid issues with format conversion, don't use objects inside the simple hash values.
Define value object¶
First, create Value.php
in the src/FieldType/HelloWorld
directory.
The Value class of a field type contains only the basic logic of a field type, the rest of it's handled by the Type
class.
For more information about field type Value, see Value handling.
The HelloWorld
Value class should contain:
- public properties that retrieve
name
- an implementation of the
__toString()
method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
Define fields and configuration¶
Next, implement a definition of a field type extending the Generic field type in the src/FieldType/HelloWorld/Type.php
class.
It provides settings for the field type and an implementation of the Ibexa\Contracts\Core\FieldType\FieldType
abstract class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
For more information about the Type class of a field type, see Type class.
Next, register the field type as a service and tag it with ibexa.field_type
:
1 2 3 4 5 |
|
Define form for value object¶
Create a src/Form/Type/HelloWorldType.php
form.
It enables you to edit the new field type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
Now you can map field definitions into Symfony forms with FormMapper.
Add the mapFieldValueForm()
method required by FieldValueFormMapperInterface
and the required use
statements to src/FieldType/HelloWorld/Type.php
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
For more information about the FormMappers, see field type form and template.
Next, add the ibexa.admin_ui.field_type.form.mapper.value
tag to the service definition:
1 2 3 4 5 6 |
|
Render fields¶
Create a template¶
Create a template for the new field type. It defines the default rendering of the HelloWorld
field.
In the templates/themes/standard/field_types
directory create a field_type.html.twig
file:
1 2 3 |
|
Template mapping¶
Provide the template mapping under the ibexa.system.<scope>.field_templates
configuration key:
1 2 3 4 5 |
|
Final results¶
Finally, you should be able to add a new content type in the back office interface. Navigate to Content types tab and under Content category create a new content type:
Next, define a Hello World field:
After saving, your Hello World content type should be available under Content in the sidebar menu.