Daniel Siepmann - Coding is Art

Blog Post

TYPO3 show additional information in dropdown

Published: , Updated:

Tested with TYPO3: 11 LTS

Topics: typo3

Introduction

TYPO3 provides an "Application Information" dropdown to show some information.

This dropdown can be extended with custom information, e.g. some API endpoints or currently deployed Git Commit Hash. This can ease debugging as you can easily check whether the system runs the newest version with all changes applied.

I did that for one of our customers and wanted to share how easy it is to add such information.

Our Goal

The goal is to add a custom entry to the drop down as shown in i56. It will show the Git SVG Icon, the title “Git Commit” and the actual Git commit.

Figure i56: The application context dropdown of a production System. It has a new entry "Git Commit" with an Git SVG Icon in front and a Git commit hash as value.

Adding EventListener

We will need to register a new event listener via Services.yaml of our Extension:

services:
  Vendor\ExtName\EventHandler\SystemInformationToolbarCollectorHandler:
   tags:
     - name: 'event.listener'
       event: 'TYPO3\CMS\Backend\Backend\Event\SystemInformationToolbarCollectorEvent'

That will ensure our own class Vendor\ExtName\EventHandler\SystemInformationToolbarCollectorHandler is called for the corresponding event.

The class itself needs to implement the PHP magic method __invoke(). That method is called for the event, as we didn't provide a dedicated method when registering our event listener. The class itself can look like:

<?php

namespace Vendor\ExtName\EventHandler;

use TYPO3\CMS\Backend\Backend\Event\SystemInformationToolbarCollectorEvent;
use TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem;
use TYPO3\CMS\Backend\Toolbar\Enumeration\InformationStatus;
use TYPO3\CMS\Core\Core\Environment;

class SystemInformationToolbarCollectorHandler
{
    public function __invoke(SystemInformationToolbarCollectorEvent $event): void
    {
        $status = 'unknown';
        $content = file_get_contents(Environment::getProjectPath() . DIRECTORY_SEPARATOR . 'REVISION');
        if (is_string($content)) {
            $status = mb_substr($content, 0, 7);
        }

        $event->getToolbarItem()->addSystemInformation(
            'Git Commit',
            $status,
            'actions-brand-git',
            $status === 'unknown' ? InformationStatus::STATUS_WARNING : InformationStatus::STATUS_NOTICE
        );
    }
}

It will read the contents of the file REVISION from project (not web) root. That way the deployment can create the file and add the commit hash as content. We will strip down to first 7 characters of the hash. That's usually enough to be unique within a project.

Further reading