PackageInitializationEvent

New in version 13.0

The PSR-14 event \TYPO3\CMS\Core\Package\Event\PackageInitializationEvent allows listeners to execute custom functionality after an extension has been activated.

The event is being dispatched at several places, where extensions get activated. Those are, for example:

  • on extension installation by the extension manager
  • on calling the typo3 extension:setup command.

The main component dispatching the event is the \TYPO3\CMS\Core\Package\PackageActivationService.

Developers are able to listen to the new event before or after the TYPO3 Core listeners have been executed, using before and after in the listener registration. All listeners are able to store arbitrary data in the event using the addStorageEntry() method. This is also used by the Core listeners to store their result.

Example

EXT:my_extension/Classes/Package/EventListener/MyEventListener.php
<?php

namespace MyVendor\MyExtension\Package\EventListener;

use TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\Core\Package\Event\PackageInitializationEvent;
use TYPO3\CMS\Core\Package\Initialization\ImportExtensionDataOnPackageInitialization;

#[AsEventListener(
    identifier: 'my-extension/package-initialization',
    after: ImportExtensionDataOnPackageInitialization::class,
)]
final readonly class MyEventListener
{
    public function __invoke(PackageInitializationEvent $event): void
    {
        if ($event->getExtensionKey() === 'my_extension') {
            $event->addStorageEntry(__CLASS__, 'my result');
        }
    }
}
Copied!

New in version 13.0

The PHP attribute \TYPO3\CMS\Core\Attribute\AsEventListener has been introduced to tag a PHP class as an event listener. Alternatively, you can also register an event listener via the Configuration/Services.yaml file. Have a look into the section Implementing an event listener in your extension.

API

class \TYPO3\CMS\Core\Package\Event\ PackageInitializationEvent

Event that is triggered after a package has been activated (or required in composer mode), allowing listeners to execute initialization tasks, such as importing static data.

getExtensionKey ( )
returntype

string

getPackage ( )
returntype

TYPO3\CMS\Core\Package\PackageInterface

getContainer ( )
returntype

Psr\Container\ContainerInterface

getEmitter ( )
returntype

object

hasStorageEntry ( string $identifier)
param string $identifier

the identifier

returntype

bool

getStorageEntry ( string $identifier)
param string $identifier

the identifier

returntype

TYPO3\CMS\Core\Package\PackageInitializationResult

addStorageEntry ( string $identifier, mixed $data)
param string $identifier

the identifier

param mixed $data

the data

removeStorageEntry ( string $identifier)
param string $identifier

the identifier