Skip to content

Step 3 - Create a form for editing Field Type

Create a form

To edit your new Field Type, create a Point2DType.php form in the src/Form/Type directory. Next, add a Point2DType class that extends the AbstractType and implements the buildForm() method. This method adds fields for x and y coordinates.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<?php
declare(strict_types=1);

namespace App\Form\Type;

use App\FieldType\Point2D\Value;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class Point2DType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder->add('x', NumberType::class);
        $builder->add('y', NumberType::class);
    }

}

Add a Form Mapper Interface

The FormMapper adds the Field definitions into Symfony forms using the add() method. The FieldValueFormMapperInterface provides an edit form for your Field Type in the administration interface. For more information about the FormMappers, see Field Type form and template.

First, implement a FieldValueFormMapperInterface interface (Ibexa\Contracts\ContentForms\FieldType\FieldValueFormMapperInterface) to Field Type definition in the src/FieldType/Point2D/Type.php.

Next, implement a mapFieldValueForm() method and invoke FormInterface::add method with the following arguments (highlighted lines):

  • Name of the property the Field value will map to: value
  • Type of the Field: Point2DType::class
  • Custom options: required and label

Final version of the Type class should have the following statements and functions:

 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
<?php
declare(strict_types=1);

namespace App\FieldType\Point2D;

use App\Form\Type\Point2DType;
use Ibexa\Contracts\ContentForms\Data\Content\FieldData;
use Ibexa\Contracts\ContentForms\FieldType\FieldValueFormMapperInterface;
use Ibexa\Contracts\Core\FieldType\Generic\Type as GenericType;
use Symfony\Component\Form\FormInterface;

final class Type extends GenericType implements FieldValueFormMapperInterface
{
    public function getFieldTypeIdentifier(): string
    {
        return 'point2d';
    }

    public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
    {
        $definition = $data->fieldDefinition;
        $fieldForm->add('value', Point2DType::class, [
            'required' => $definition->isRequired,
            'label' => $definition->getName(),
        ]);
    }
}

Finally, add a configureOptions method and set default value of data_class to Value::class in src/Form/Type/Point2DType.php. It will allow your form to work on this object.

 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
<?php
declare(strict_types=1);

namespace App\Form\Type;

use App\FieldType\Point2D\Value;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class Point2DType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder->add('x', NumberType::class);
        $builder->add('y', NumberType::class);
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Value::class,
        ]);
    }
}

Add a new tag

Next, add the ibexa.admin_ui.field_type.form.mapper.value tag to config/services.yaml:

1
2
3
4
    App\FieldType\Point2D\Type:
        tags:
            - { name: ibexa.field_type, alias: point2d }
            - { name: ibexa.admin_ui.field_type.form.mapper.value, fieldType: point2d }