Attention
TYPO3 v10 has reached end-of-life as of April 30th 2023 and is no longer being maintained. Use the version switcher on the top left of this page to select documentation for a supported version of TYPO3.
Need more time before upgrading? You can purchase Extended Long Term Support (ELTS) for TYPO3 v10 here: TYPO3 ELTS.
Signals and Slots (deprecated)¶
Warning
The extbase SignalSlotDispatcher and the concept of using Signals and Slots
has been superseded by PSR-14 events
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 dispatch on
the SignalSlot Dispatcher. The sender does not have or need any information about the receivers (slots). (See
Dispatching Signals)
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 Using Signals
<signals-basics>)
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, .
Dispatching Signals¶
Emitting a signal is a mere call of the function dispatch on the SignalSlot Dispatcher:
# 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.
Using Signals¶
To connect a slot to a signal, use the \TYPO3\CMS\Extbase\SignalSlot\Dispatcher::connect() method.
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\Closureobject$slotMethodName: Name of the method to be used as a slot. If$slotClassNameOrObjectis a\Closureobject, this parameter is ignored and can be skipped$passSignalInformation: If set totrue, the last argument passed to the slot will be information about the signal (EmitterClassName::signalName)
Usage example:
1$signalSlotDispatcher = `\TYPO3\CMS\Core\Utility\GeneralUtility::`
2makeInstance(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class);
3$signalSlotDispatcher->connect(
4 `\TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class, // Signal class name`
5 'afterExtensionUninstall', // Signal name
6 `\TYPO3\CMS\Core\Core\ClassLoadingInformation::class, // Slot class name`
7 'dumpClassLoadingInformation' // Slot name
8);
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.
Finding Signals¶
There is no complete list of signals available, but they are easily found by
searching the TYPO3 core or the extensions code for dispatch(.
For finding hooks, look in the Hooks Configuration.