Enumerations

Target group: Integrators, Developers

New in version 3.9.0

This feature is considered experimental and may change at any time until it is declared stable. However, feedback is welcome.

Introduction

The schema.org vocabulary provides enumerations types. An enumeration has one or more members, for example, the GenderType used for the gender property has the members Male and Female.

These enumerations can be used in your code instead of plain strings. This has the advantage of avoiding typos because you can use your IDE's capabilities. Also these members are part of a common vocabulary.

You can find the enums provided by the TYPO3 schema extensions in the Classes/Model/Enumeration/ folder.

Usage in PHP

Usage in PHP is straightforward, in this example we are using the \Brotkrueml\Schema\Model\Enumeration\GenderType enum in the gender property:

EXT:my_extension/Classes/Controller/MyController.php
<?php

declare(strict_types=1);

namespace MyVendor\MyExtension\Controller;

use Brotkrueml\Schema\Model\Enumeration\GenderType;
use Brotkrueml\Schema\Type\TypeFactory;

final class MyController
{
    public function __construct(
        private readonly TypeFactory $typeFactory,
    ) {}

    public function doSomething(): void
    {
        // ...

        $person = $this->typeFactory->create('Person');
        $person
            ->setId('https://example.org/#person-42')
            ->setProperty('givenName', 'John')
            ->setProperty('familyName', 'Smith')
            ->setProperty('gender', GenderType::Male);

        // ...
    }
}
Copied!

This results in the following output:

{
   "@context": "https://schema.org/",
   "@type": "Person",
   "@id": "https://example.org/#person-42",
   "givenName": "John",
   "familyName": "Smith",
   "gender": "https://schema.org/Male",
}
Copied!

Usage in Fluid templates

The example in the PHP section above can also be adapted for a Fluid template. We make use of the constant view helper (which is available since Fluid v2.12):

<schema:type.person
   -id="https://example.org/#person-42"
   givenName="John"
   familyName="Smith"
   gender="{f:constant(name: '\Brotkrueml\Schema\Model\Enumeration\GenderType::Male')}"
/>
Copied!

This results in the following output:

{
   "@context": "https://schema.org/",
   "@type": "Person",
   "@id": "https://example.org/#person-42",
   "givenName": "John",
   "familyName": "Smith",
   "gender": "https://schema.org/Male",
}
Copied!

Create your own enumeration type

Not all enumerations defined by schema.org are provided by this extension (or by the section extensions), but only those who have at least one member defined by schema.org. You may find a schema.org enumeration type which references members from the GoodRelations or from other vocabularies. If you need them, you can create a custom enum.

Have a look into Add a new enumeration.