Post Processors
A post-processor is a data transformer that converts a field value to a format suitable for the API. Post-processors are used only in the get, get_list and get_subresource actions.
The following table shows all post processors provided out-of-the-box:
Name |
Description |
Options |
---|---|---|
twig |
Applies a TWIG template. |
template string - The name of the TWIG template. |
All post processors are registered in PostProcessorRegistry. You can use it when you need to get a specific post processor in your code.
Creating a New Post Processor
To create a new post processor, you need to do the following:
Create a class that implements PostProcessorInterface.
namespace Acme\Bundle\DemoBundle\Api\PostProcessor;
use Oro\Bundle\ApiBundle\PostProcessor\PostProcessorInterface;
class SomePostProcessor implements PostProcessorInterface
{
/**
* {@inheritDoc}
*/
public function process(mixed $value, array $options): mixed
{
}
}
Register the post-processor in the dependency injection container using the
oro.api.post_processor
tag with thealias
attribute that contains a unique name of the post-processor:
acme.api.post_processor.some:
class: Acme\Bundle\DemoBundle\Api\PostProcessor\SomePostProcessor
tags:
- { name: oro.api.post_processor, alias: some }
Create a config extension if you need to validate the post-processor options.
namespace Acme\Bundle\DemoBundle\Api\PostProcessor;
use Oro\Bundle\ApiBundle\Util\ConfigUtil;
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
class SomePostProcessorConfigExtension extends AbstractConfigExtension
{
/**
* {@inheritDoc}
*/
public function getConfigureCallbacks(): array
{
return [
'entities.entity.field' => function (NodeBuilder $node) {
$this->addValidationOfPostProcessorOptions($node);
},
'actions.action.field' => function (NodeBuilder $node) {
$this->addValidationOfPostProcessorOptions($node);
},
'subresources.subresource.action.field' => function (NodeBuilder $node) {
$this->addValidationOfPostProcessorOptions($node);
}
];
}
/**
* @param NodeBuilder $node
*/
private function addValidationOfPostProcessorOptions(NodeBuilder $node): void
{
$node->end()->validate()
->ifTrue(function ($v) {})
->thenInvalid();
}
}
Register the config extension as a service in the dependency injection container.
acme.api.config_extension.post_processor.some:
class: Acme\Bundle\DemoBundle\Api\PostProcessor\SomePostProcessorConfigExtension
Register the config extension in Resources/config/oro/app.yml in your bundle or config/config.yml of your application.
oro_api:
config_extensions:
- acme.api.config_extension.post_processor.some