Post Processors

A post processor is a data transformer that is used to convert a field value to a format suitable for the API. The 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:

  1. Create a class that implements PostProcessorInterface.
<?php

namespace Acme\Bundle\ProductBundle\Api\PostProcessor;

use Oro\Bundle\ApiBundle\PostProcessor\PostProcessorInterface;

class SomePostProcessor implements PostProcessorInterface
{
    /**
     * {@inheritDoc}
     */
    public function process($value, array $options)
    {
    }
}
  1. Register the post processor in the dependency injection container using the oro.api.post_processor tag with the alias attribute that contains a unique name of the post processor:
acme.api.post_processor.some:
    class: Acme\Bundle\ProductBundle\Api\PostProcessor\SomePostProcessor
    tags:
        - { name: oro.api.post_processor, alias: some }
  1. Create a config extension if you need to validate the post processor options.
<?php

namespace Acme\Bundle\ProductBundle\Api\PostProcessor;

use Oro\Bundle\ApiBundle\Util\ConfigUtil;
use Symfony\Component\Config\Definition\Builder\NodeBuilder;

class SomePostProcessorConfigExtension extends AbstractConfigExtension
{
    /**
     * {@inheritDoc}
     */
    public function getConfigureCallbacks()
    {
        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();
    }
}
  1. Register the config extension as a service in the dependency injection container.
acme.api.config_extension.post_processor.some:
    class: Acme\Bundle\ProductBundle\Api\PostProcessor\SomePostProcessorConfigExtension
  1. 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