eventstypo3event-listenerextbasetypo3-12.x

Which event is dispatched in Typo3 12.4, when an object is saved/changed/removed from the database?


I have a data structure including the models product and version, and each product can be connected to a number of versions. The version has a data point size. On the web page, we want to show for each product the maximum_size, i.e., the maximum over the sizes of all connected versions.

The idea to implement this efficiently in Typo3 12.4 is as follows:

Are there Typo3 PSR-14 events which are already suitable for this application?

In particular, to my understanding of the documentation, those would be the candidates:

I have tried the first one, but it does not seem to work:

Following https://docs.typo3.org/m/typo3/reference-coreapi/12.4/en-us/ExtensionArchitecture/HowTo/Events/Index.html

  1. Create an event listener class in my extension
# Classes/EventListener/ProductMaximumSizeUpdater.php

<?php

declare(strict_types=1);

namespace MyVendor\MyExtension\EventListener;

use \TYPO3\CMS\Extbase\Event\Persistence\EntityPersistedEvent;

use \Psr\Log\LoggerInterface;

final class ProductMaximumSizeUpdater
{
    public function __construct(
        private readonly LoggerInterface $logger,
    ) {}
    
    public function __invoke(EntityPersistedEvent $event): void
    {
        $entity = $event->getObject();
        $this->logger->info('test message');
    }
}
  1. Register the event in my extension
# Configuration/Services.yaml

services:
  _defaults:
    autowire: true
    autoconfigure: true
    public: false
  
  MyVendor\MyExtension\EventListener\ProductMaximumSizeUpdater:
    tags:
      - name: event.listener
        identifier: 'productMaximumSizeUpdater'
  1. I have configured the LoggingInterface such that it writes info logs from my EventListeners to /var/log/ReplicareWiwaEventListener.log:
$GLOBALS['TYPO3_CONF_VARS']['LOG']['MyVendor]['MyExtension']['EventListener']['writerConfiguration'] = [
    \TYPO3\CMS\Core\Log\LogLevel::INFO => [
        \TYPO3\CMS\Core\Log\Writer\FileWriter::class => [
            'logFile' => \TYPO3\CMS\Core\Core\Environment::getVarPath() . '/log/MyEventListener.log'
        ]
    ]
];

With this, I would expect, that whenever a new object is created and saved in the database in the backend of the web page, an info is logged to /var/log/MyEventListener.log. But actually, the log file has not even been created (meaning, there was no logging at all).

Am I getting something wrong about how to implement the EventListener? Or is the EntityPersistedEvent just not the right thing to use here?


Solution

  • Hi its Important to understand that TYPO3 Frontend and Backend use Different Methods for Persisting data.

    the Backend Uses the Datahandler uses the classic hooks https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/Events/Hooks/Index.html so you won't find any events for this right now.

    and even in the Frotenend are different Possiblities of Persisting data. most modern Extensions Use Extbase which comes with its onwn ORM Logic and fires the Events you mentioned int your question.

    but extensions might also use Querybuilder or other Database Classes. to modify Data directly. so to catch "all" updates you need to know the scope of your extension. does the editing only happen in the backend -> its most likly Datahandler hooks in the frontend the Evnents. if both you need to handle the logic for both.