Skip to content

Step 1 - Implement the Point 2D Value class

Project installation

To start the tutorial, you need to make a clean Ibexa DXP installation. Follow the guide for your system to install Ibexa DXP, configure a server, and start the web server. Remember to install using the dev environment.

Open your project with a clean installation and create the base directory for a new Point 2D Field Type in src/FieldType/Point2D.

The Value class

The Value class of a Field Type is by design very simple. It is used to represent an instance of the Field Type within a content item. Each Field presents its data using an instance of the Type's Value class. For more information about Field Type Value, see Value handling.

Tip

According to the convention, the class representing Field Type Value should be named Value and should be placed in the same namespace as the Type definition.

Simple hash values

A simple hash value always means an array of scalar values and/or nested arrays of scalar values. To avoid issues with format conversion, don't use objects inside the simple hash values.

The Point 2D Value class contains:

  • private properties, used to store the actual data
  • an implementation of the __toString() method, required by the Value interface

By default, the constructor from FieldType\Value is used.

The Point 2D is going to store two elements (coordinates for point 2D):

  • x value
  • y value

At this point, it does not matter where they are stored. You want to focus on what the Field Type exposes as an API.

src/FieldType/Point2D/Value.php should have the following properties:

1
2
3
4
5
    /** @var float|null */
    private $x;

    /** @var float|null */
    private $y;

A Value class must also implement the Ibexa\Contracts\Core\FieldType\Value interface. To match the FieldType\Value interface, you need to implement __toString() method. You also need to add getters and setters for x and y properties. This class represents the point 2D.

The final code should look like this:

 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
38
39
40
41
42
43
44
45
46
<?php
declare(strict_types=1);

namespace App\FieldType\Point2D;

use Ibexa\Contracts\Core\FieldType\Value as ValueInterface;

final class Value implements ValueInterface
{
    /** @var float|null */
    private $x;

    /** @var float|null */
    private $y;

    public function __construct(?float $x = null, ?float $y = null)
    {
        $this->x = $x;
        $this->y = $y;
    }

    public function getX(): ?float
    {
        return $this->x;
    }

    public function setX(?float $x): void
    {
        $this->x = $x;
    }

    public function getY(): ?float
    {
        return $this->y;
    }

    public function setY(?float $y): void
    {
        $this->y = $y;
    }

    public function __toString()
    {
        return "({$this->x}, {$this->y})";
    }
}