From 258411d8e75f859d392cc78f6bb8f8c106b60fa3 Mon Sep 17 00:00:00 2001 From: Michael Telgmann Date: Tue, 10 Dec 2024 09:46:30 +0100 Subject: [PATCH] Add hint about live version to general event listener article --- .../listening-to-events.md | 64 ++++++++++++++++--- 1 file changed, 54 insertions(+), 10 deletions(-) diff --git a/guides/plugins/plugins/plugin-fundamentals/listening-to-events.md b/guides/plugins/plugins/plugin-fundamentals/listening-to-events.md index c86264371..d86399b8d 100644 --- a/guides/plugins/plugins/plugin-fundamentals/listening-to-events.md +++ b/guides/plugins/plugins/plugin-fundamentals/listening-to-events.md @@ -7,27 +7,33 @@ nav: # Listening to Events -A way to listen to events in Symfony projects is via an [event subscriber,](https://symfony.com/doc/current/event_dispatcher.html#creating-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. +A way to listen to events in Symfony projects is via an [event subscriber,](https://symfony.com/doc/current/event_dispatcher.html#creating-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](../plugin-base-guide). +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](../plugin-base-guide). ::: info -Refer to this video on **[Live coding example with product.loaded event.](https://www.youtube.com/watch?v=cJDaiuyjKJk)**. Also available on our free online training ["Shopware 6 Backend Development"](https://academy.shopware.com/courses/shopware-6-backend-development-with-jisse-reitsma). +Refer to this video on **[Live coding example with product.loaded event.](https://www.youtube.com/watch?v=cJDaiuyjKJk)**. +Also available on our free online training ["Shopware 6 Backend Development"](https://academy.shopware.com/courses/shopware-6-backend-development-with-jisse-reitsma). ::: ## 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/`. +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](https://symfony.com/doc/current/event_dispatcher.html#creating-an-event-subscriber). Don't worry, we got you covered here as well. +Basically, that's it already if you're familiar with [Symfony subscribers](https://symfony.com/doc/current/event_dispatcher.html#creating-an-event-subscriber). +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. +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 as in Symfony itself. Therefore, this is how your subscriber could then look like: @@ -37,9 +43,9 @@ Therefore, this is how your subscriber could then look like: namespace Swag\BasicExample\Subscriber; +use Shopware\Core\Content\Product\ProductEvents; use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; -use Shopware\Core\Content\Product\ProductEvents; class MySubscriber implements EventSubscriberInterface { @@ -61,11 +67,49 @@ class MySubscriber implements EventSubscriberInterface In this example, the subscriber would be located in the `/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. +The subscriber is now listening for the `product.loaded` event to trigger. + +Some entities, like orders or products, are versioned. +This means, that some events are dispatched multiple times for different versions, but they belong to the same entity. +Therefore, you can check the version of the context to make sure you're only reacting to the live version. + +```php +// /src/Subscriber/MySubscriber.php + 'onProductWritten' + ]; + } + + public function onProductWritten(EntityWrittenEvent $event) + { + if ($event->getContext()->getVersionId() !== Defaults::LIVE_VERSION) { + return; + } + // Do something + } +} +``` + +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. +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. ```php // /src/Resources/config/services.xml @@ -83,4 +127,4 @@ Registering your subscriber to Shopware 6 is also as simple as it is in Symfony. ``` -That's it, your subscriber service is now automatically loaded at runtime and it should start listening to the mentioned events to be dispatched. +That's it, your subscriber service is now automatically loaded at runtime, and it should start listening to the mentioned events to be dispatched.