Extending Liform
If you need to transform custom Form Types and/or need extra data to be passed to your UI libraries you can easily extend the transformation with your own methods.
Transformer
A Transformer in Liform provides the JSON representation for a Form View. It must return a TransformResult
that should include all information that is specific for the rendered Form Types.
use Pitch\Liform\Transformer\TransformerInterface;
use Pitch\Liform\TransformResult;
use Symfony\Component\Form\FormView;
class MyTransformer implements TransformerInterface
{
public function transform(FormView $view): TransformResult
{
//...
}
}
Resolver
Liform delegates the decision which Tranformer it should use to the Resolver.
The built-in Resolver traverses block_prefixes
like a tranformation per Twig templates would.
You can write your own Transformers and bind them to a block prefix.
services:
# ...
App\MySpecialTransformer:
tags:
- { name: liform.transformer, block: my_block_prefix }
App\MyTextFieldTransformer:
tags:
- { name: liform.transformer, block: text }
If you want to decide which Transformer another way you can replace the Resolver.
namespace App;
use Pitch\Liform\Transformer\TransformerInterface;
use Symfony\Component\Form\FormView;
class MyLiformResolver implements ResolverInterface
{
public function resolve(FormView $view): TransformerInterface
{
/...
}
}
services:
# ...
Pitch\Liform\ResolverInterface:
class: App\MyLiformResolver
Extension
An Extension in Liform manipulates a TransformResult
. It should add data structures that are shared across multiple Form Types.
use Pitch\Liform\Extension\ExtensionInterface;
use Pitch\Liform\TransformResult;
use Symfony\Component\Form\FormView;
class MyLiformExtension implements ExtensionInterface
{
public function apply(TransformResult $transformResult, FormView $formView): void
{
// ...
}
}
services:
App\MyLiformExtension:
tags:
- { name: liform.extension }