SR
Click to open github profile
Usage

Custom Transformers

Learn how you can add your own transformers for custom fields.


You can use the EVENT_REGISTER_FIELD_TRANSFORMERS event to define custom transformers for specific field types. A transformer is responsible for processing the data for your json response for a given field. Here's how to set it up:

Initialize Event Listener

Add the following code to your module or plugin to register your custom transformer:

use modules\queryapiextension\transformers\HyperTransformer;
use samuelreichoer\queryapi\transformers\BaseTransformer;
use samuelreichoer\queryapi\events\FieldTransformerEvent;

Event::on(
    BaseTransformer::class,
    BaseTransformer::EVENT_REGISTER_FIELD_TRANSFORMERS,
    function (FieldTransformerEvent $event) {
        $event->transformers[] = [
            'fieldClass' => 'verbb\hyper\fields\HyperField', // class of your field you want to transform
            'transformer' => HyperTransformer::class, // class of your transformer
        ];
    }
);

In this example:

  • fieldClass specifies the fully qualified class name of the field you want to transform (e.g., verbb\hyper\fields\HyperField).
  • transformer defines the custom transformer class that will handle the transformation.

Creating a Transformer

A transformer class processes the field's data. Here’s an example for a HyperField transformer:

<?php

namespace modules\queryapiextension\transformers;

use verbb\hyper\models\LinkCollection;

class HyperTransformer
{
    private LinkCollection $hyper;

    public function __construct(LinkCollection $hyper)
    {
        $this->hyper = $hyper;
    }

    /**
     * Transforms the Hyper field data.
     *
     * @return array
     */
    public function getTransformedData(): array
    {
        return [
            'metadata' => $this->getMetaData(),
            'linkText' => $this->hyper->text,
            'linkUrl' => $this->hyper->url,
            'linkTarget' => $this->hyper->target,
        ];
    }

    /**
     * Retrieves metadata from the Hyper field.
     *
     * @return array
     */
    protected function getMetaData(): array
    {
        return [
            'type' => $this->hyper->type,
        ];
    }
}

Extending the Base Transformer

If your custom transformer requires shared functionality, you can extend the BaseTransformer class to inherit common logic. For example:

use samuelreichoer\queryapi\transformers\BaseTransformer;

class HyperTransformer extends BaseTransformer
{
    public function getTransformedData(): array
    {
        return [
            'linkText' => $this->hyper->text,
            'linkUrl' => $this->hyper->url,
        ];
    }
}

Important Notes

  1. Method Naming: The plugin uses the getTransformedData() method to fetch transformed data. This method is required in all custom transformers.
  2. Error Logging: If a transformer is not properly registered or does not implement getTransformedData(), an error will be logged, but it will not throw it.

Copyright © 2024 Samuel Reichör