Signals and Slots in TYPO3 Flow

Signals and slots in flow allows users to handle or execute any function based on an event. When an event is triggered a signal is emitted by using signal method and this is then handled by the slots listening to this signal. A slot can be any method defined in Flow and user can execute any code in the slot depending on his/her requirement

Defining and Using Signals

To define a signal, create a method which starts with emit and annotate it with a Signal annotation. Whatever parameter are defined in the signal will be handed over to any slots listening to that signal. The Signal annotation is picked up by the AOP framework and the method is filled with implementation code as needed.

/**
 * @param Product $product
 * @return void
 * @Flow\Signal
 */
protected function emitAddedNewProduct(Product $product) {}

In the above example the “Product $product” parameter will be available to the slots listening to the    
signal.

To emit a signal, simply call the signal method where ever it is required in the code based on the event:

/**
* @param Product $newProduct
* @return void
*/
public function addNewProductAction(Product $newProduct) {
        ...// code
        $this->emitAddedNewProduct($newProduct);
        ...// code
}

Defining Slots

Slot are any method defined in Flow. The only requirement is a matching signature between signal and slot, so that the parameters passed to the signal can be handed over to the slot without problems. Below are two examples of a slots and as you can see that it does not differ from any other normal methods.

/**
 * @param Product $product
 * @return void
 */
public function sendNewProductNotification(Product $product) {
..// code 
}

/**
 * @param Product $product
 * @return void
 */
public function addProductToCatalog(Product $product) {
..// code 
}

Wiring Signals and Slots

Which slot is actually listening for which signal is configured ("wired") in the bootstrap code of a package. Any package can of course freely wire its own signals to its own slots, but also wiring any other signal to any other slot is possible.

When TYPO3 Flow initializes, it runs the boot() method in a package's Package class. This is the place to wire signals to slots as needed for your package:

/**
 * Boot the package. Wiring signals to slots.
 *
 * @param \Neos\Flow\Core\Bootstrap $bootstrap The current bootstrap
 * @return void
 */
public function boot(\Neos\Flow\Core\Bootstrap $bootstrap) {
        $dispatcher = $bootstrap->getSignalSlotDispatcher();
        $dispatcher->connect(
                'Some\Package\Controller\ProductController', 'addedNewProduct',
                'Some\Package\Service\NotificationService', 'sendNewProductNotification'
        );
       $dispatcher->connect(
                'Some\Package\Controller\ProductController', 'addedNewProduct',
                'Some\Package\Service\CatalogService', 'addProductToCatalog'
        );
}

In the above example of wiring, the signal “emitAddedNewProduct” is wired to two slots “sendNewProductNotification” and “addProductToCatalog”. So when the signal is emitted, it will be handed to both the slots and then the user can use the slot method the execute code based on the requirement.  


Categories: Flow Framework
Tags: TYPO3Flow

Copyright © 2019 Lelesys Informatik GmbH, Deutschland. All Rights Reserved.