Declare events

For arlanix/notification 1.0.0

Declare events

An event is something your module tells, such as a feed failed or a stock level fell, that the store's rules can answer: each rule chooses an event, conditions on its fields, a topic, and a message worded with the fields. Publishing yourself, as in Publish from your module, sends one message to one topic the store chose; declaring an event leaves what is sent, when and where to the store.

arlanix/module-notification-event (orders, customers, reviews) and arlanix/module-notification-monitoring (cron, errors, admin sign-ins, imports and exports) are both made this way, and are the examples to read.

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

Declare it

etc/arlanix_notification_events.xml, validated by urn:magento:module:Arlanix_Notification:etc/arlanix_notification_events.xsd:

<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>
Attribute
event/@code Unique across modules: a lower-case letter, then lower-case letters, digits and _, up to 64 characters. Prefix it with your module.
event/@label, @group How the rule form lists it, under its group.
event/@severity info, warning or error: a rule's message unless the rule sets another, or the data emitted says another under _severity (see Emit it).
event/@module A Magento module the event needs, such as Magento_Review: offered only while it is enabled.
event/@immediate true sends the rule's message past the queue, at once, as monitoring's Cron stopped must.
field/@path Where the value is in the data you emit, dotted: feed.code.
field/@type text (the default), number, boolean, or select with @source, a class with toOptionArray(): conditions offer its options, and the message shows the option's label.
field/@attribute true shows the field with every message of the event, under the text.
field/@personal true for data about a person: sent only when a rule's subject or body names it, never as a fact.
field/@sample What Preview and Publish test message use.
subject, body The message when a rule leaves its own empty, in the same template syntax as rules: {path}, `{path

Emit it

With arlanix/module-notification required, 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(),
        ],
    ]);
}
  • wants() says whether any active rule answers the event: ask first, and gather the data only when one does.
  • emit() never throws. The data is nested as the paths name it. Each active rule of the event whose conditions it meets publishes its message to its topic.
  • A rule left on As the event says takes the event's severity, unless the data says another under _severity (Arlanix\Notification\Model\Rule\MessageBuilder::SEVERITY): info, warning or error. An event that tells a failure and its recovery, as monitoring's Cron stopped does, emits '_severity' => 'error' for one and 'info' for the other. A rule with a severity of its own keeps it, and a value that is none of the three is ignored.
  • Inside a database transaction, the message waits for the commit and is dropped on a rollback: an order a checkout gives up on is never told. Emit from *_save_commit_after, or anywhere: the wait is done for you.

With the module only suggested, dispatch an event instead, which needs none of its classes:

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

The key is code, not event: Magento's own observers already take event.

Check it

Arlanix > Notifications > Rules > Add rule lists the event under its group; Variables lists its fields, and Preview renders its subject and body with the samples. bin/magento cache:clean config after a change to the XML.