Declare events

This page is for version 1.0. Read the current version.

For arlanix/notification 1.0.1

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.
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. The constant for the key is Arlanix\Notification\Model\Rule\MessageBuilder::SEVERITY.
  • 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.

If your module only suggests Multichannel Store Notifications, dispatch the arlanix_notification_emit event instead. This needs none of the module's classes:

$this->eventManager->dispatch('arlanix_notification_emit', [
    'code' => 'vendor_feed_failed',
    'data' => ['feed' => ['code' => $feed->getCode(), 'products' => 0]],
]);

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

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.