Listening to events
A way to listen to events in Symfony projects is via an event subscriber, which is a class that defines one or more methods that listen to one or various events. It is thus the same in Shopware, so this article will guide you on how to create event subscriber in your Shopware extension.

Prerequisites

In order to build your own subscriber for your plugin, of course you first need a plugin as base. To create an own plugin, you can refer to the Plugin Base Guide.
Here's a live coding example on listening to events from our free online training "Backend Development".

Creating your own subscriber

Plugin base class

Registering a custom subscriber requires to load a services.xml file with your plugin. This is done by either placing a file with name services.xml into a directory called src/Resources/config/.
Basically, that's it already if you're familiar with Symfony subscribers. Don't worry, we got you covered here as well.

Creating your new subscriber class

To start creating a subscriber, we need to create a class first implementing EventSubscriberInterface. As mentioned above, such a subscriber for Shopware 6 looks exactly the same like in Symfony itself.
Therefore, this is how your subscriber could then look like:
<plugin root>/src/Subscriber/MySubscriber.php
1
<?php declare(strict_types=1);
2
​
3
namespace Swag\BasicExample\Subscriber;
4
​
5
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
6
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7
use Shopware\Core\Content\Product\ProductEvents;
8
​
9
class MySubscriber implements EventSubscriberInterface
10
{
11
public static function getSubscribedEvents(): array
12
{
13
// Return the events to listen to as array like this: <event to listen to> => <method to execute>
14
return [
15
ProductEvents::PRODUCT_LOADED_EVENT => 'onProductsLoaded'
16
];
17
}
18
​
19
public function onProductsLoaded(EntityLoadedEvent $event)
20
{
21
// Do something
22
// E.g. work with the loaded entities: $event->getEntities()
23
}
24
}
Copied!
In this example, the subscriber would be located in the <plugin root>/src/Subscriber directory.
The subscriber is now listening for the product.loaded event to trigger. Unfortunately, your subscriber is not even loaded yet - this will be done in the previously registered services.xml file.

Registering your subscriber via services.xml

Registering your subscriber to Shopware 6 is also as simple as it is in Symfony. You're simply registering your (subscriber) service by mentioning it in the services.xml. The only difference to a normal service is, that you need to add the kernel.event_subscriber tag to your subscriber for it to be recognized as such.
<plugin root>/src/Resources/config/services.xml
1
<?xml version="1.0" ?>
2
​
3
<container xmlns="http://symfony.com/schema/dic/services"
4
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6
​
7
<services>
8
<service id="Swag\BasicExample\Subscriber\MySubscriber">
9
<tag name="kernel.event_subscriber"/>
10
</service>
11
</services>
12
</container>
Copied!
That's it, your subscriber service is now automatically loaded at runtime and it should start listening to the mentioned events to be dispatched.
Last modified 4mo ago