Declare events
On this page

Declare events

An event is something that happens in your module, for example a feed failed or a stock level fell. When you declare an event, the store's rules can use it. A rule chooses an event, sets conditions on the event's fields, chooses a topic, and writes the message with the fields.

Compare this with Publish from your module. When you publish, your module sends one message to the one topic that the store chose. When you declare an event, the store decides what is sent, when, and to which topic.

arlanix/module-notification-event (orders, customers, reviews) and arlanix/module-notification-monitoring (cron, errors, admin sign-ins, imports and exports) declare their events this way. Read them as examples.

To add a field to another module's event, or a variable to every event, see Customise messages.

Declare it

Declare your events in etc/arlanix_notification_events.xml. Magento validates the file against urn:magento:module:Arlanix_Notification:etc/arlanix_notification_events.xsd. For example:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Arlanix_Notification:etc/arlanix_notification_events.xsd">
    <event code="vendor_feed_failed" label="Feed failed" group="Feeds" severity="error">
        <field path="feed.code" label="Feed" attribute="true" sample="google-us"/>
        <field path="feed.products" label="Products written" type="number" sample="0"/>
        <field path="feed.channel" label="Channel" type="select"
               source="Vendor\Module\Model\Source\Channels" sample="google"/>
        <subject>Feed {feed.code} failed</subject>
        <body>{feed.code} wrote {feed.products} products.</body>
    </event>
</config>

The file uses the following attributes and elements:

Attribute Description
event/@code Unique across all modules. It starts with a lower-case letter, followed by lower-case letters, digits and _, up to 64 characters. Start it with the name of your module.
event/@label, @group The rule form lists the event by its label, under its group.
event/@severity info, warning or error. This is the severity of a rule's message, unless the rule sets its own, or the emitted data sets _severity. See Emit it.
event/@module A Magento module that the event needs, for example Magento_Review. While that module is disabled, rules cannot choose the event, and it is not sent.
event/@immediate true sends the rule's message immediately, without the queue. Monitoring's Cron stopped needs this.
field/@path The dotted path of the value in the data that you emit, for example feed.code. Do not use message as a top-level key in data: {message.*} is reserved for the message's own variables.
field/@type text (the default), number, boolean or select. A select needs @source, a class with toOptionArray(). Conditions offer its options, and the message shows the option's label instead of the value.
field/@attribute true shows the field with every message of the event, below the body.
field/@personal true for data about a person. The field is sent only when a rule's subject or body names it. It is never shown with the message as an attribute.
field/@sample The value that Preview and Publish test message use.
subject, body The default message, used when a rule leaves its own subject or body empty. They use the same template syntax as rules: {path}, `{path

Emit it

If your module requires arlanix/module-notification, emit the event through Arlanix\Notification\Api\EmitterInterface:

public function __construct(private readonly \Arlanix\Notification\Api\EmitterInterface $emitter)
{
}

public function afterFailure(Feed $feed): void
{
    if (!$this->emitter->wants('vendor_feed_failed')) {
        return;
    }

    $this->emitter->emit('vendor_feed_failed', [
        'feed' => [
            'code' => $feed->getCode(),
            'products' => $feed->getProductCount(),
            'channel' => $feed->getChannel(),
        ],
    ]);
}

The two methods work as follows:

  • wants() returns whether any active rule uses the event. Call it first, and collect the data only when it returns true.
  • emit() never throws. Nest the data as the field paths name it. For example, feed.code is ['feed' => ['code' => …]]. Each active rule of the event whose conditions match the data publishes its message to its topic.

The severity of a rule's message works as follows:

  • A rule with the severity As the event says uses the event's severity.
  • The data can override it with the key _severity, set to info, warning or error. If your module requires Notification, you can use Arlanix\Notification\Model\Rule\MessageBuilder::SEVERITY for the key. If it only suggests Notification, use the string '_severity' or your own constant: Notification's classes are absent when it is not installed.
  • Use _severity for an event that reports both a failure and its recovery. For example, monitoring's Cron stopped emits '_severity' => 'error' when cron stops, and '_severity' => 'info' when cron runs again.
  • A rule with its own severity keeps it. Any other value of _severity is ignored.

If you emit inside a database transaction, the message waits for the commit. If the transaction is rolled back, nothing is sent. For example, no message is sent for an order that the checkout does not complete. So you can emit from *_save_commit_after, or from anywhere else.

With Notification only suggested

If your module works with Multichannel Store Notifications only when it is present, list arlanix/module-notification under suggest in your composer.json. Dispatch the arlanix_notification_emit event instead of using Notification's classes, so your module also works when Notification is not installed.

Wrap the dispatch in try/catch and log the failure, as the Arlanix modules do. Broken configuration in Notification or another extension can throw while Magento builds the observer and its dependencies, before Notification's own error handling starts. Put _severity inside data:

try {
    $this->eventManager->dispatch('arlanix_notification_emit', [
        'code' => 'vendor_feed_failed',
        'data' => [
            'feed' => ['code' => $feed->getCode(), 'products' => 0],
            '_severity' => 'error',
        ],
    ]);
} catch (\Throwable $e) {
    $this->logger->error('Feed notification could not be emitted.', ['exception' => $e]);
}

The key is code, not event, because Magento's own observers already use event.

The arlanix_notification_emit event, its code and data keys, the _severity key inside data, and the schema of etc/arlanix_notification_events.xml stay compatible in later Notification versions, including major ones. Your module needs no conflict against newer Notification versions for this integration.

Compatibility in the other direction depends on what your module uses. An addition to the events schema in a later 1.x release is unknown to an older Notification schema. Magento refuses the file when schema validation runs. If you use such an addition, set "conflict": {"arlanix/module-notification": "<1.N"} in your composer.json, replacing 1.N with the version that added it. Composer does not enforce versions written in suggest.

This compatibility promise does not cover the arlanix_notification_publish or arlanix_notification_topic_options events. If your module uses either, set "conflict": {"arlanix/module-notification": ">=2.0"}. See Publish from your module.

Check it

To check your event in the admin, follow these steps:

  1. After each change to the XML, run bin/magento cache:clean config.
  2. Open Arlanix > Notifications > Rules and click Add rule. The Event list shows your event under its group.
  3. Choose your event. Variables lists its fields.
  4. Click Preview. It shows the subject and body filled in with the samples.