PSR-14 events

Target group: Developers

Introduction

You can enhance the functionality in the schema extension with PSR-14 event listeners. An event listener receives an event that provides methods for retrieving and setting dedicated properties.

Register additional properties for a type

Sometimes it can be necessary to use properties which are not standardised or pending, or to add property annotations. Therefore an PSR-14 event is available which can be used in an event listener.

These additional properties are not only available in the API but also as arguments in the view helpers.

The event \Brotkrueml\Schema\Event\RegisterAdditionalTypePropertiesEvent provides the following methods:

getType(): string

Returns the class name of the type. You can use this to add a property only to one type.

getAdditionalProperties(): array

Retrieve the already defined additionalProperties for this type, for example, by other event listeners.

registerAdditionalProperty(string $propertyName): void

This method registers an additional property for one or more types.

Example

  1. Create the event listener

    EXT:my_extension/Classes/EventListener/AdditionalPropertiesForPerson.php
    <?php
    
    declare(strict_types=1);
    
    namespace MyVendor\MyExtension\EventListener;
    
    use Brotkrueml\Schema\Event\RegisterAdditionalTypePropertiesEvent;
    use Brotkrueml\Schema\Model\Type\Person;
    
    final class AdditionalPropertiesForPerson
    {
        public function __invoke(RegisterAdditionalTypePropertiesEvent $event): void
        {
            if ($event->getType() === Person::class) {
                $event->registerAdditionalProperty('gender');
                $event->registerAdditionalProperty('jobTitle');
            }
        }
    }
    
    Copied!

    The method __invoke() implements the logic for registering additional properties for one or more types. It receives the RegisterAdditionalTypePropertiesEvent. You can register as many properties as you want.

  2. Register your event listener in Configuration/Services.yaml

    services:
       # Place here the default dependency injection configuration
    
       MyVendor\MyExtension\EventListener\AdditionalPropertiesForPerson:
          tags:
             - name: event.listener
               identifier: 'myAdditionalPropertiesForPerson'
    Copied!

Read how to configure dependency injection in extensions.