The SignalSlot system provides a means for realizing loosely coupled dependencies in the sense that a code entity A can react on an event occurring in code entity B, without A and B knowing each other directly. This works by dispatching event information through a central third instance, the so called dispatcher:
In the shown schematics, object B and one other object are interested in a certain Signal. B is a so-called Slot that can be announced to be interested in receiving a Signal (indicated by the circular connector to the dispatcher). Object A now sends the corresponding Signal. The Dispatcher takes care of realizing the dependency and informs the Slot B (and one other Slot) about the occurrence of the Signal.
Signals roughly equal events, while Slots roughly equal event handlers. An arbitrary number (0…n) of Slots can listen for a specific Signal. Every object that receives the Dispatcher as a dependency can send Signals. However, the following conditions apply (that typically do not apply to event handling systems):
- A Slot cannot return anything to the object that issued a Signal
- It is not possible for a Slot to stop the propagation of a Signal, i.e. all listening Slots will eventually receive the Signal
Those two conditions allow the asynchronous processing of Slots. That means: It is possible to determine, by configuration, that a Slot must not receive a Signal in the very same moment it occurs, but to receive it on a later point in time, maybe after other Signals from a queue have been processed or even on a completely different server.
A Signal represents a specific event, e.g. that a content version has been published. It consists of information that is significant to the event, e.g. the content ID and version number. Therefore, a Signal is represented by an object of a class that is specific to the Signal and that extends from
eZ\Publish\Core\SignalSlot\Signal. The full qualified name of the Signal class is used to uniquely identify the Signal. For example, the class
eZ\Publish\Core\SignalSlot\Signal\ContentService\PublishVersionSignal identifies the example Signal.
In order to work properly with asynchronous processing, Signals must not consist of any logic and must not contain complex data structures (such as further objects and resources). Instead, they must be exportable using the
__set_state() method, so that it is possible to transfer a Signal to a different system.
Signals can theoretically be sent by any object that gets hold of a SignalDispatcher (
eZ\Publish\Core\SignalSlot\SignalDispatcher). However, at a first stage, Signals are only sent by special implementations of the Public API to indicate core events. These services must and will be used by default and will wrap the original service implementations.
A Slot extends the system by realizing functionality that is executed when a certain Signal occurs. To implement a Slot, you must create a class that derives from
eZ\Publish\Core\SignalSlot\Slot. The full qualified name of the Slot class is also used as the unique identifier of the Slot. The Slot base class requires you to implement the single method
receive(). When your Slot is configured to listen to a certain Signal and this Signal occurs, the
receive() method of your Slot is called.
receive() method of your Slot you can basically realize any kind of logic. However, it is recommended that you only dispatch the action to be triggered to a dedicated object. This allows you to trigger the same action from within multiple Slots and to re-use the implementation from a completely different context.
Note that, due to the nature of SignalSlot, the following requirements must be fulfilled by your Slot implementation:
- A Slot must not return anything to the Signal issuer
- A Slot must be aware that it is potentially executed delayed or even on a different server
Important: A single Slot should not take care of processing more than one Signal. Instead, if you need to trigger same or similar actions as different Signals occur. You should encapsulate these actions into a dedicated class, of which your Slots receive an instance to trigger this action.
Example: Updating URL aliases¶
Updating URL aliases is a typical process that can be realized through a SignalSlot extension for different reasons:
- The action must be triggered on basis of different events (e.g. content update, location move, etc.)
- Direct implementation would involve complex dependencies between otherwise unrelated services
- The action is not critical to be executed immediately, but can be made asynchronous, if necessary
As a first step it needs to be determined for which Signals we need to listen in order to update URL aliases. Some of them are:
There are of course additional Signals that trigger an update of URL aliases, but these are left out for simplicity here.
Now that we identified some Signals to react upon, we can start implementing Slots for these Signals. For the first Signal, which is issued as soon as a new version of Content is published, there exists a method in
eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler for exactly that purpose:
publishUrlAliasForLocation(). The Signal contains the ID of the Content item and its newly published version number. Using this information, the corresponding Slot can fulfill its purposes with the following steps:
- Load the corresponding content and its locations
- Call the URL alias creation method for each location
To achieve this, the Slot has 2 dependencies:
eZ\Publish\SPI\Persistence\Content\Handlerto load the content itself in order to retrieve the names
eZ\Publish\SPI\Persistence\Content\Location\Handlerto load the locations
eZ\Publish\SPI\Persistence\Content\UrlAlias\Handlerto create the aliases for each location
So, a stub for the implementation could look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
In order to make the newly created Slot react on the corresponding Signal, the following steps must be performed:
- Make the Slot available through the Symfony service container as a service
- Register the Slot to react to the Signal of type
For more information, see Listening to Core events.
Important note about template matching
Template matching will NOT work if your content contains a Field Type that is not supported by the Repository. It can be the case when you are in the process of a migration from eZ Publish 4.x, where custom datatypes have been developed.
In this case the Repository will throw an exception, which is caught in the
ViewController, and if you are using LegacyBridge it will end up doing a fallback to legacy kernel.
The list of Field Types supported out of the box is available here.
Listening to Core events¶
When you interact with the Public API, and with the content repository in particular, Signals may be sent out, allowing you to react to actions triggered by the repository. Those signals can be received by dedicated services called Slots.
This recipe will describe how to register a Slot for a dedicated Signal.
Registering a Slot for a given Signal¶
As described above, a Slot is the eZ Platform equivalent of a Symfony event listener and must extend
A typical implementation is the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
You now need to register
OnPublishSlot as a service in the ServiceContainer and identify it as a valid Slot.
services.yml (in your bundle):
1 2 3 4 5 6 7 8 9
ezpublish.api.slot identifies your service as a valid Slot.
The signal part (mandatory) says that this Slot is listening to
Internal signals emitted by Repository services are always relative to
You can register a Slot for any kind of signal by setting
* in the service tag.
Using a basic Symfony event listener¶
eZ Platform comes with a generic Slot that converts signals (including ones defined by user code) to regular event objects and exposes them via the EventDispatcher. This makes it possible to implement a simple event listener/subscriber if you're more comfortable with this approach.
All you need to do is to implement an event listener or subscriber and register it.
This very simple example will just log the received signal.
services.yml (in your bundle):
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34