Skip to main content

Field Transformer

Field transformer can be used to transform fields by configuration.
For example this comes in handy if you're using the API Output Channel.

image

Service Registration​

AppBundle\FormBuilder\FieldTransformer\PhoneNumberTransformer:
autowire: true
public: false
tags:
- { name: form_builder.output_workflow.field_transform, identifier: phoneNumberTransformer }

Within the service, you're able to modify your value.

<?php

namespace AppBundle\FormBuilder\FieldTransformer;

use Symfony\Component\Form\FormInterface;
use OpenDxp\Bundle\FormBuilderBundle\OutputWorkflow\FieldTransformerInterface;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberUtil;

class PhoneNumberTransformer implements FieldTransformerInterface
{
public function getName(): string
{
return 'Phone Number Transformer';
}

public function getDescription(): ?string
{
return 'Add your description here';
}

public function transform(FormInterface $formField, array $context): mixed
{
$phoneUtil = PhoneNumberUtil::getInstance();
$phoneInstance = $phoneUtil->parse($formField->getData(), 'DE');

return $phoneUtil->format($phoneInstance, PhoneNumberFormat::E164);
}
}