Skip to content

Commit

Permalink
Add hint about live version to general event listener article
Browse files Browse the repository at this point in the history
  • Loading branch information
mitelg committed Dec 10, 2024
1 parent fd7f169 commit 258411d
Showing 1 changed file with 54 additions and 10 deletions.
64 changes: 54 additions & 10 deletions guides/plugins/plugins/plugin-fundamentals/listening-to-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)**.

Check warning on line 19 in guides/plugins/plugins/plugin-fundamentals/listening-to-events.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/plugin-fundamentals/listening-to-events.md#L19

If a new sentence starts here, add a space and start with an uppercase letter. (LC_AFTER_PERIOD[1]) Suggestions: ` Loaded`, ` loaded` Rule: https://community.languagetool.org/rule/show/LC_AFTER_PERIOD?lang=en-US&subId=1 Category: CASING
Raw output
guides/plugins/plugins/plugin-fundamentals/listening-to-events.md:19:59: If a new sentence starts here, add a space and start with an uppercase letter. (LC_AFTER_PERIOD[1])
 Suggestions: ` Loaded`, ` loaded`
 Rule: https://community.languagetool.org/rule/show/LC_AFTER_PERIOD?lang=en-US&subId=1
 Category: CASING

Check warning on line 19 in guides/plugins/plugins/plugin-fundamentals/listening-to-events.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/plugin-fundamentals/listening-to-events.md#L19

A comma may be missing after the conjunctive/linking adverb ‘Also’. (SENT_START_CONJUNCTIVE_LINKING_ADVERB_COMMA[1]) Suggestions: `Also,` URL: https://languagetool.org/insights/post/linking-words/ Rule: https://community.languagetool.org/rule/show/SENT_START_CONJUNCTIVE_LINKING_ADVERB_COMMA?lang=en-US&subId=1 Category: PUNCTUATION
Raw output
guides/plugins/plugins/plugin-fundamentals/listening-to-events.md:19:122: A comma may be missing after the conjunctive/linking adverb ‘Also’. (SENT_START_CONJUNCTIVE_LINKING_ADVERB_COMMA[1])
 Suggestions: `Also,`
 URL: https://languagetool.org/insights/post/linking-words/ 
 Rule: https://community.languagetool.org/rule/show/SENT_START_CONJUNCTIVE_LINKING_ADVERB_COMMA?lang=en-US&subId=1
 Category: PUNCTUATION
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.

Check warning on line 27 in guides/plugins/plugins/plugin-fundamentals/listening-to-events.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/plugin-fundamentals/listening-to-events.md#L27

Did you mean “loading”? Or maybe you should add a pronoun? In active voice, ‘require’ + ‘to’ takes an object, usually a pronoun. (ALLOW_TO[1]) Suggestions: `loading` URL: http://www.ef.com/english-resources/english-grammar/gerund-equals-infinitive/ Rule: https://community.languagetool.org/rule/show/ALLOW_TO?lang=en-US&subId=1 Category: GRAMMAR
Raw output
guides/plugins/plugins/plugin-fundamentals/listening-to-events.md:27:41: Did you mean “loading”? Or maybe you should add a pronoun? In active voice, ‘require’ + ‘to’ takes an object, usually a pronoun. (ALLOW_TO[1])
 Suggestions: `loading`
 URL: http://www.ef.com/english-resources/english-grammar/gerund-equals-infinitive/ 
 Rule: https://community.languagetool.org/rule/show/ALLOW_TO?lang=en-US&subId=1
 Category: GRAMMAR
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:

Expand All @@ -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
{
Expand All @@ -61,11 +67,49 @@ class MySubscriber implements EventSubscriberInterface

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.
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

Check warning on line 76 in guides/plugins/plugins/plugin-fundamentals/listening-to-events.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/plugin-fundamentals/listening-to-events.md#L76

File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `PHP` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING
Raw output
guides/plugins/plugins/plugin-fundamentals/listening-to-events.md:76:3: File types are normally capitalized. (FILE_EXTENSIONS_CASE[1])
 Suggestions: `PHP`
 URL: https://languagetool.org/insights/post/spelling-capital-letters/ 
 Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1
 Category: CASING
// <plugin root>/src/Subscriber/MySubscriber.php
<?php declare(strict_types=1);

namespace Swag\BasicExample\Subscriber;

use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class MySubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
ProductEvents::PRODUCT_WRITTEN_EVENT => '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
// <plugin root>/src/Resources/config/services.xml
Expand All @@ -83,4 +127,4 @@ Registering your subscriber to Shopware 6 is also as simple as it is in Symfony.
</container>
```

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.

0 comments on commit 258411d

Please sign in to comment.