Signals and Slots¶
Changed in version 10.2: A number of signals have been replaced in the core with PSR-14 events. See changelogs:
Signals and Slots provide a way to extend TYPO3s Core functionality or the functionality of Extensions. Signals roughly follow the observer pattern.
Signals and Slots decouple the sender (sending a signal) and the receiver(s) (called slots). Hooks depend on directly calling functions in the implementing class.
Concept of Signals and Slots¶
Whenever the sender (i.e. a core class or the class of an extension) wants to send a signal it calls
Dispatcher. The sender does not have or need any information about the receivers (slots). (See
The receiver generates a slot by calling
connect on the SignalSlot
Dispatcher on startup. A slot always listens for
signals with name i.e. “afterExtensionUninstall” on a certain class, i.e. “InstallUtility::class”. (See
The function representing the slot will be called automatically by the SignalSlot
Dispatcher whenever a signal gets
dispatched. The slot will be called with one array parameter. If several slots registered for a
signal all of them will be called. However the order in which they are being called cannot be defined or depended upon.
The slot may provide an array as return value that may or may not be used be the dispatching class depending on its implementation. In the case of several slots being connected to the signal and one or all of them have return values the return value of the previous slot will be fed into the next slot as input data. As there is no way of knowing in which order the slots will be called, several slots manipulations of the same part of the array might override each other.
As the data returned by a slot will be used as input for the next slot it must forward the entire information from the input data plus the changes being made by this slot.
Slots should never take the content of the input data array for granted, because there is no programmatic way of ensuring the returned array contains all expected data, .
Emitting a signal is a mere call of the function
dispatch on the SignalSlot
# use TYPO3\CMS\Core\Utility\GeneralUtility; # use TYPO3\CMS\Extbase\Object\ObjectManager; # use TYPO3\CMS\Extbase\SignalSlot\Dispatcher; $signalSlotDispatcher = GeneralUtility::makeInstance(ObjectManager::class)->get(Dispatcher::class); $signalArguments = [ 'someData' => $someData, 'otherData' => $otherData, 'caller' => $this ]; $signalArguments = $signalSlotDispatcher->dispatch(__CLASS__, 'signalName', $signalArguments);
The data returned by the dispatch should not be taken for granted. Always perform sanity checks before using it.
To connect a slot to a signal, use the
This method accepts the following arguments:
$signalClassName: Name of the class containing the signal
$signalName: Name of the class containing the signal
$slotClassNameOrObject: Name of the class containing the slot or the instantiated class or a
$slotMethodName: Name of the method to be used as a slot. If
\Closureobject, this parameter is ignored and can be skipped
$passSignalInformation: If set to
true, the last argument passed to the slot will be information about the signal (
1 2 3 4 5 6 7 8
$signalSlotDispatcher = \TYPO3\CMS\Core\Utility\GeneralUtility:: makeInstance(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class); $signalSlotDispatcher->connect( \TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class, // Signal class name 'afterExtensionUninstall', // Signal name \TYPO3\CMS\Core\Core\ClassLoadingInformation::class, // Slot class name 'dumpClassLoadingInformation' // Slot name );
In this example, we define that we want to call the method
dumpClassLoadingInformation of the class
\TYPO3\CMS\Core\Core\ClassLoadingInformation::class when the signal
afterExtensionUninstall of the class
\TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class is dispatched.
To find out which parameters/variables are available, open the signal’s class and take a look at the dispatch call:
$this->signalSlotDispatcher->dispatch(__CLASS__, 'afterExtensionUninstall', [$extensionKey, $this]);
In this case, the
dumpClassLoadingInformation method will get the
extension key and an instance of the dispatching class as parameters.