Skip to content

Create custom Field Type comparison

In the Back Office, you can compare the contents of Fields. Comparing is possible only between two versions of the same Field that are in the same language.

You can add the possibility to compare custom and other unsupported Field Types.

Note

The following task uses the custom "Hello World" Field Type. The configuration is based on the comparison mechanism created for the ezstring Field Type.

Create Comparable class

First, create a Comparable.php class in src/FieldType/HelloWorld/Comparison.

This class implements the EzSystems\EzPlatformVersionComparison\FieldType\Comparable interface with the getDataToCompare() method:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php

declare(strict_types=1);

namespace App\FieldType\HelloWorld\Comparison;

use eZ\Publish\SPI\FieldType\Value as SPIValue;
use EzSystems\EzPlatformVersionComparison\ComparisonValue\StringComparisonValue;
use EzSystems\EzPlatformVersionComparison\FieldType\Comparable as ComparableInterface;
use EzSystems\EzPlatformVersionComparison\FieldType\FieldTypeComparisonValue;

final class Comparable implements ComparableInterface
{
    public function getDataToCompare(SPIValue $value): FieldTypeComparisonValue
    {
        return new Value([
            'name' => new StringComparisonValue([
                'value' => $value->getName(),
            ]),
        ]);
    }
}

The getDataToCompare() fetches the data to compare and determines which comparison engines should be used.

Register this class as a service:

1
2
3
4
services:
    App\FieldType\HelloWorld\Comparison\Comparable:
        tags:
            - { name: ezplatform.field_type.comparable, alias: hello_world }

Create comparison value

Next, create a src/FieldType/HelloWorld/Comparison/Value.php file that holds the comparison value:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<?php

declare(strict_types=1);

namespace App\FieldType\HelloWorld\Comparison;

use EzSystems\EzPlatformVersionComparison\FieldType\FieldTypeComparisonValue;

class Value extends FieldTypeComparisonValue
{
    /** @var \EzSystems\EzPlatformVersionComparison\ComparisonValue\StringComparisonValue */
    public $name;
}

Create comparison engine

The comparison engine handles the operations required for comparing the contents of Fields. Each Field Type requires a separate comparison engine, which implements the EzSystems\EzPlatformVersionComparison\Engine\FieldTypeComparisonEngine interface.

For the "Hello World" Field Type, create the following comparison engine based on the engine for the TextLine Field Type. Place it in src/FieldType/HelloWorld/Comparison/HelloWorldComparisonEngine.php:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php

declare(strict_types=1);

namespace App\FieldType\HelloWorld\Comparison;

use EzSystems\EzPlatformVersionComparison\Engine\FieldTypeComparisonEngine;
use EzSystems\EzPlatformVersionComparison\Engine\Value\StringComparisonEngine;
use EzSystems\EzPlatformVersionComparison\FieldType\FieldTypeComparisonValue;
use EzSystems\EzPlatformVersionComparison\Result\ComparisonResult;

final class HelloWorldComparisonEngine implements FieldTypeComparisonEngine
{
    /** @var \EzSystems\EzPlatformVersionComparison\Engine\Value\StringComparisonEngine */
    private $stringValueComparisonEngine;

    public function __construct(StringComparisonEngine $stringValueComparisonEngine)
    {
        $this->stringValueComparisonEngine = $stringValueComparisonEngine;
    }

    /**
     * @param \EzSystems\EzPlatformVersionComparison\FieldType\TextLine\Value $comparisonDataA
     * @param \EzSystems\EzPlatformVersionComparison\FieldType\TextLine\Value $comparisonDataB
     */
    public function compareFieldsTypeValues(FieldTypeComparisonValue $comparisonDataA, FieldTypeComparisonValue $comparisonDataB): ComparisonResult
    {
        return new HelloWorldComparisonResult(
            $this->stringValueComparisonEngine->compareValues($comparisonDataA->name, $comparisonDataB->name)
        );
    }

    public function shouldRunComparison(FieldTypeComparisonValue $comparisonDataA, FieldTypeComparisonValue $comparisonDataB): bool
    {
        return $comparisonDataA->name->value !== $comparisonDataB->name->value;
    }
}

Register the comparison engine as a service:

1
2
3
4
services:
    App\FieldType\HelloWorld\Comparison\HelloWorldComparisonEngine:
        tags:
            - { name: ezplatform.field_type.comparable.engine, supported_type: App\FieldType\HelloWorld\Comparison\Value }

Add comparison result

Next, create a comparison result class in src/FieldType/HelloWorld/Comparison/HelloWorldComparisonResult.php.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php

declare(strict_types=1);

namespace App\FieldType\HelloWorld\Comparison;

use EzSystems\EzPlatformVersionComparison\Result\ComparisonResult;
use EzSystems\EzPlatformVersionComparison\Result\Value\StringComparisonResult;

final class HelloWorldComparisonResult implements ComparisonResult
{
    /** @var \EzSystems\EzPlatformVersionComparison\Result\Value\StringComparisonResult */
    private $stringDiff;

    public function __construct(StringComparisonResult $stringDiff)
    {
        $this->stringDiff = $stringDiff;
    }

    public function getHelloWorldDiff(): StringComparisonResult
    {
        return $this->stringDiff;
    }

    public function isChanged(): bool
    {
        return $this->stringDiff->isChanged();
    }
}

Provide templates

Finally, create a template for the new comparison view in templates/field_type_comparison.html.twig:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
{% extends '@ezdesign/version_comparison/comparison_result_blocks.html.twig' %}

{% block hello_world_field_comparison %}
    {% apply spaceless %}
        <span {{ block( 'field_attributes' ) }}>
            {% with {
                'comparison_result': comparison_result.getHelloWorldDiff()
                } %}
                {{ block('string_diff_render') }}
            {% endwith %}
        </span>
    {% endapply %}
{% endblock %}

Add configuration for this template in config/packages/ezplatform.yaml:

1
2
3
4
5
ezplatform:
    system:
        default:
            field_comparison_templates:
                - { template: 'field_type_comparison.html.twig', priority: 10 }