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.

Render additional types 

The event allows to add markup in cases where no controller is available, for example, if you want to enrich a page with structured data depending on the doktype of a page.

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

getRequest(): \Psr\Http\Message\ServerRequestInterface

getRequest(): \Psr\Http\Message\ServerRequestInterface

Returns the PSR-7 request object.

addType(TypeInterface ...$type): void

addType(TypeInterface ...$type): void

Add one or more type models.

addMainEntityOfWebPage(TypeInterface $mainEntity): void

addMainEntityOfWebPage(TypeInterface $mainEntity): void

Add a main entity.

Example 

In the following example we add structured data markup depending on the doktype of the page:

EXT:my_extension/Classes/EventListener/AddMarkupToArticlePages.php
<?php

declare(strict_types=1);

namespace MyVendor\MyExtension\EventListener;

use Brotkrueml\Schema\Event\RenderAdditionalTypesEvent;
use Brotkrueml\Schema\Type\TypeFactory;
use TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\Frontend\Page\PageInformation;

#[AsEventListener(
    identifier: 'my-extension/add-markup-to-article-pages',
)]
final readonly class AddMarkupToArticlePages
{
    public function __construct(
        private TypeFactory $typeFactory,
    ) {}

    public function __invoke(RenderAdditionalTypesEvent $event): void
    {
        /** @var PageInformation $pageInformation */
        $pageInformation = $event->getRequest()->getAttribute('frontend.page.information');
        $page = $pageInformation->getPageRecord();
        if ($page['doktype'] !== 12345) {
            return;
        }

        // Only for doktype 12345
        $article = $this->typeFactory->create('Article');
        $article->setProperty('name', $page['title']);
        // ... and set some other properties

        $event->addType($article);
    }
}
Copied!

The method __invoke() implements the logic for rendering additional types. It receives the RenderAdditionalTypesEvent. You can add as many types as you like.

Prevent embedding of markup 

New in version 4.2.0

Sometimes it is required to disable the embedding of markup on certain pages. If you have the need for that, the event \Brotkrueml\Schema\Event\IsMarkupToBeInjectedEvent is your friend. The event is stoppable: the event listener that excludes the markup from injection is the last one called in the chain.

The event provides the following methods:

getRequest(): \Psr\Http\Message\ServerRequestInterface

getRequest(): \Psr\Http\Message\ServerRequestInterface

Returns the PSR-7 request object.

excludeMarkupFromInjection(): void

excludeMarkupFromInjection(): void

Method to be called, if you want to exclude a page from embedding the markup.

Example 

The example excludes the markup from injection, if the page ID is 42:

EXT:my_extension/Classes/EventListener/ExcludeMarkupOnPage42.php
<?php

declare(strict_types=1);

namespace MyVendor\MyExtension\EventListener;

use Brotkrueml\Schema\Event\IsMarkupToBeInjectedEvent;
use TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\Frontend\Page\PageInformation;

#[AsEventListener(
    identifier: 'my-extension/exclude-markup-from-page-42',
)]
final readonly class ExcludeMarkupOnPage42
{
    public function __invoke(IsMarkupToBeInjectedEvent $event): void
    {
        /** @var PageInformation $pageInformation */
        $pageInformation = $event->getRequest()->getAttribute('frontend.page.information');
        if ($pageInformation->getId() === 42) {
            $event->excludeMarkupFromInjection();
        }
    }
}
Copied!